From d3df2f985d478a96332c728f7065285e812e7158 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 25 Jul 2017 22:07:21 +0800 Subject: [PATCH] extend `collapse_vars` to `let` and `const` (#2252) fixes #2250 --- lib/ast.js | 2 +- lib/compress.js | 1 + lib/scope.js | 2 +- test/compress/collapse_vars.js | 68 ++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/lib/ast.js b/lib/ast.js index d2be08a8..ab4b2897 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -697,7 +697,7 @@ var AST_Export = DEFNODE("Export", "exported_definition exported_value is_defaul var AST_VarDef = DEFNODE("VarDef", "name value", { $documentation: "A variable declaration; only appears in a AST_Definitions node", $propdoc: { - name: "[AST_SymbolVar|AST_SymbolConst|AST_Destructuring] name of the variable", + name: "[AST_Destructuring|AST_SymbolConst|AST_SymbolLet|AST_SymbolVar] name of the variable", value: "[AST_Node?] initializer, or null of there's no initializer" }, _walk: function(visitor) { diff --git a/lib/compress.js b/lib/compress.js index 78e48c96..a6aef068 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -302,6 +302,7 @@ merge(Compressor.prototype, { if (reduce_vars) { if (node instanceof AST_Toplevel) node.globals.each(reset_def); if (node instanceof AST_Scope) node.variables.each(reset_def); + if (node.block_scope) node.block_scope.variables.each(reset_def); if (node instanceof AST_SymbolRef) { var d = node.definition(); d.references.push(node); diff --git a/lib/scope.js b/lib/scope.js index 1beeaa64..e7e6283e 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -116,7 +116,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){ var tw = new TreeWalker(function(node, descend){ if (node.is_block_scope()) { var save_scope = scope; - scope = new AST_Scope(node); + node.block_scope = scope = new AST_Scope(node); scope.init_scope_vars(save_scope); if (!(node instanceof AST_Scope)) { scope.uses_with = save_scope.uses_with; diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 5b64c6a3..3316042e 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -2548,3 +2548,71 @@ duplicate_argname: { } expect_stdout: "PASS" } + +issue_2250_1: { + options = { + collapse_vars: true, + conditionals: true, + reduce_vars: true, + } + input: { + function f(x) { + if (x) { + const a = foo(); + x(a); + } + } + function g(x) { + if (x) { + let a = foo(); + x(a); + } + } + function h(x) { + if (x) { + var a = foo(); + x(a); + } + } + } + expect: { + function f(x) { + x && x(foo()); + } + function g(x) { + x && x(foo()); + } + function h(x) { + x && x(foo()); + } + } +} + +issue_2250_2: { + options = { + collapse_vars: true, + passes: 2, + reduce_vars: true, + side_effects: true, + toplevel: true, + } + input: { + { + const foo = function(){}; + foo(bar()); + } + { + let foo = function(){}; + foo(bar()); + } + { + var foo = function(){}; + foo(bar()); + } + } + expect: { + bar(); + bar(); + bar(); + } +}