diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-02-26 20:16:14 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-27 04:16:14 +0800 |
commit | ac26993b5a1546e790d93603d0d7a05740566b07 (patch) | |
tree | 1aa4e1a945a847db8314a5966764c357134cd1d1 /lib | |
parent | ea52339502ac0436532e55a6980df0785736c8fb (diff) | |
download | tracifyjs-ac26993b5a1546e790d93603d0d7a05740566b07.tar.gz tracifyjs-ac26993b5a1546e790d93603d0d7a05740566b07.zip |
fix corner cases with block-scoped functions (#4695)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 36 | ||||
-rw-r--r-- | lib/scope.js | 7 |
2 files changed, 30 insertions, 13 deletions
diff --git a/lib/compress.js b/lib/compress.js index 16d86d90..0e698804 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1509,11 +1509,22 @@ merge(Compressor.prototype, { return stat instanceof AST_Const || stat instanceof AST_DefClass || stat instanceof AST_Let; } + function safe_to_trim(stat) { + if (stat instanceof AST_LambdaDefinition) { + var def = stat.name.definition(); + return def.scope === stat.name.scope || all(def.references, function(ref) { + var scope = ref.scope; + do { + if (scope === stat.name.scope) return true; + } while (scope = scope.parent_scope); + }); + } + return !is_lexical_definition(stat); + } + function as_statement_array(thing) { if (thing === null) return []; - if (thing instanceof AST_BlockStatement) return all(thing.body, function(stat) { - return !is_lexical_definition(stat); - }) ? thing.body : [ thing ]; + if (thing instanceof AST_BlockStatement) return all(thing.body, safe_to_trim) ? thing.body : [ thing ]; if (thing instanceof AST_EmptyStatement) return []; if (is_statement(thing)) return [ thing ]; throw new Error("Can't convert thing to statement array"); @@ -2732,9 +2743,7 @@ merge(Compressor.prototype, { for (var i = 0; i < statements.length;) { var stat = statements[i]; if (stat instanceof AST_BlockStatement) { - if (all(stat.body, function(stat) { - return !is_lexical_definition(stat); - })) { + if (all(stat.body, safe_to_trim)) { CHANGED = true; eliminate_spurious_blocks(stat.body); [].splice.apply(statements, [i, 1].concat(stat.body)); @@ -2972,15 +2981,16 @@ merge(Compressor.prototype, { } function extract_functions() { - var tail = statements.slice(i + 1); - statements.length = i + 1; - return tail.filter(function(stat) { - if (stat instanceof AST_Defun) { - statements.push(stat); + var defuns = []; + var tail = statements.splice(i + 1).filter(function(stat) { + if (stat instanceof AST_LambdaDefinition) { + defuns.push(stat); return false; } return true; }); + [].push.apply(all(tail, safe_to_trim) ? statements : tail, defuns); + return tail; } function as_statement_array_with_return(node, ab) { @@ -3439,7 +3449,7 @@ merge(Compressor.prototype, { function push(node) { if (block) { block.push(node); - if (is_lexical_definition(node)) block.required = true; + if (!safe_to_trim(node)) block.required = true; } else { target.push(node); } @@ -5118,7 +5128,7 @@ merge(Compressor.prototype, { return in_list ? List.skip : make_node(AST_EmptyStatement, node); case 1: var stat = node.body[0]; - if (is_lexical_definition(stat)) return node; + if (!safe_to_trim(stat)) return node; if (parent instanceof AST_IterationStatement && stat instanceof AST_LambdaDefinition) return node; return stat; } diff --git a/lib/scope.js b/lib/scope.js index 799b0dc6..600a6869 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -327,6 +327,13 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) { self.uses_eval = true; } } + if (sym.init instanceof AST_LambdaDefinition && sym.scope !== sym.init.name.scope) { + var scope = node.scope; + do { + if (scope === sym.init.name.scope) break; + } while (scope = scope.parent_scope); + if (!scope) sym.init = undefined; + } node.thedef = sym; node.reference(options); return true; |