aboutsummaryrefslogtreecommitdiff
path: root/lib/compress.js
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2019-04-21 09:49:07 +0800
committerGitHub <noreply@github.com>2019-04-21 09:49:07 +0800
commitf01cc1e413990a2da37716fbe8627d651f489fff (patch)
tree9ff84e40e7fabfb16d0fd63af9d4ead6bee72d02 /lib/compress.js
parent338dd144b8fedd91f0fd33c068984877527614cc (diff)
downloadtracifyjs-f01cc1e413990a2da37716fbe8627d651f489fff.tar.gz
tracifyjs-f01cc1e413990a2da37716fbe8627d651f489fff.zip
unwind IIFE class patterns (#3373)
fixes #2332
Diffstat (limited to 'lib/compress.js')
-rw-r--r--lib/compress.js48
1 files changed, 29 insertions, 19 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 2fd36095..87082807 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1287,7 +1287,9 @@ merge(Compressor.prototype, {
if (node instanceof AST_Try) return true;
if (node instanceof AST_With) return true;
if (replace_all) return false;
- return node instanceof AST_SymbolRef && !node.is_declared(compressor);
+ return node instanceof AST_SymbolRef
+ && !node.is_declared(compressor)
+ && !(parent instanceof AST_Assign && parent.left === node);
}
function in_conditional(node, parent) {
@@ -5102,7 +5104,7 @@ merge(Compressor.prototype, {
})) {
return false;
}
- } else if (line instanceof AST_EmptyStatement) {
+ } else if (line instanceof AST_Defun || line instanceof AST_EmptyStatement) {
continue;
} else if (stat) {
return false;
@@ -5113,16 +5115,15 @@ merge(Compressor.prototype, {
return return_value(stat);
}
+ function var_exists(catches, name) {
+ return catches[name] || identifier_atom[name] || scope.var_names()[name];
+ }
+
function can_inject_args(catches, safe_to_inject) {
for (var i = 0; i < fn.argnames.length; i++) {
var arg = fn.argnames[i];
if (arg.__unused) continue;
- if (!safe_to_inject
- || catches[arg.name]
- || identifier_atom[arg.name]
- || scope.var_names()[arg.name]) {
- return false;
- }
+ if (!safe_to_inject || var_exists(catches, arg.name)) return false;
if (in_loop) in_loop.push(arg.definition());
}
return true;
@@ -5131,15 +5132,15 @@ merge(Compressor.prototype, {
function can_inject_vars(catches, safe_to_inject) {
for (var i = 0; i < fn.body.length; i++) {
var stat = fn.body[i];
+ if (stat instanceof AST_Defun) {
+ if (!safe_to_inject || var_exists(catches, stat.name.name)) return false;
+ continue;
+ }
if (!(stat instanceof AST_Var)) continue;
if (!safe_to_inject) return false;
for (var j = stat.definitions.length; --j >= 0;) {
var name = stat.definitions[j].name;
- if (catches[name.name]
- || identifier_atom[name.name]
- || scope.var_names()[name.name]) {
- return false;
- }
+ if (var_exists(catches, name.name)) return false;
if (in_loop) in_loop.push(name.definition());
}
}
@@ -5243,12 +5244,21 @@ merge(Compressor.prototype, {
flatten_args(decls, expressions);
flatten_vars(decls, expressions);
expressions.push(value);
- if (decls.length) {
- i = scope.body.indexOf(compressor.parent(level - 1)) + 1;
- scope.body.splice(i, 0, make_node(AST_Var, fn, {
- definitions: decls
- }));
- }
+ var args = fn.body.filter(function(stat) {
+ if (stat instanceof AST_Defun) {
+ var def = stat.name.definition();
+ scope.functions.set(def.name, def);
+ scope.variables.set(def.name, def);
+ scope.enclosed.push(def);
+ scope.var_names()[def.name] = true;
+ return true;
+ }
+ });
+ args.unshift(scope.body.indexOf(compressor.parent(level - 1)) + 1, 0);
+ if (decls.length) args.push(make_node(AST_Var, fn, {
+ definitions: decls
+ }));
+ [].splice.apply(scope.body, args);
return expressions;
}
});