From e0461dc3c8c5459ff11ec868f6fb8dd2d7be99f0 Mon Sep 17 00:00:00 2001 From: kzc Date: Mon, 22 Jan 2018 16:45:02 -0500 Subject: [PATCH] fix for-in/of regression with let or const loop variable (#2840) fixes #2835 --- lib/compress.js | 6 +- test/compress/issue-1466.js | 177 +++++++++++++++++++++++++++++++++++- 2 files changed, 179 insertions(+), 4 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 2db9e03a..ad4c28ab 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1759,7 +1759,7 @@ merge(Compressor.prototype, { var stat = null; for (var i = 0, len = block.body.length; i < len; i++) { var line = block.body[i]; - if (line instanceof AST_Definitions && declarations_only(line)) { + if (line instanceof AST_Var && declarations_only(line)) { decls.push(line); } else if (stat) { return false; @@ -1803,7 +1803,9 @@ merge(Compressor.prototype, { } } } else if (stat instanceof AST_ForIn) { - stat.object = cons_seq(stat.object); + if (!(stat.init instanceof AST_Const) && !(stat.init instanceof AST_Let)) { + stat.object = cons_seq(stat.object); + } } else if (stat instanceof AST_If) { stat.condition = cons_seq(stat.condition); } else if (stat instanceof AST_Switch) { diff --git a/test/compress/issue-1466.js b/test/compress/issue-1466.js index 2f9ba975..3deaa0bb 100644 --- a/test/compress/issue-1466.js +++ b/test/compress/issue-1466.js @@ -38,6 +38,7 @@ same_variable_in_multiple_for_loop: { } } expect_stdout: true + node_version: ">=6" } same_variable_in_multiple_forOf: { @@ -79,7 +80,7 @@ same_variable_in_multiple_forOf: { } } expect_stdout: true - reminify: false // FIXME - regression https://github.com/mishoo/UglifyJS2/issues/2835 + node_version: ">=6" } same_variable_in_multiple_forIn: { @@ -121,7 +122,7 @@ same_variable_in_multiple_forIn: { } } expect_stdout: true - reminify: false // FIXME - regression https://github.com/mishoo/UglifyJS2/issues/2835 + node_version: ">=6" } different_variable_in_multiple_for_loop: { @@ -164,6 +165,7 @@ different_variable_in_multiple_for_loop: { } } expect_stdout: true + node_version: ">=6" } different_variable_in_multiple_forOf: { @@ -205,6 +207,7 @@ different_variable_in_multiple_forOf: { } } expect_stdout: true + node_version: ">=6" } different_variable_in_multiple_forIn: { @@ -246,6 +249,175 @@ different_variable_in_multiple_forIn: { } } expect_stdout: true + node_version: ">=6" +} + +same_variable_in_multiple_forOf_sequences_let: { + options = { + hoist_funs: true, + dead_code: true, + conditionals: true, + comparisons: true, + evaluate: true, + booleans: true, + loops: true, + unused: true, + keep_fargs: true, + if_return: true, + join_vars: true, + sequences: true, + side_effects: true, + collapse_vars: true, + } + mangle = {} + input: { + var test = [ "a", "b", "c" ]; + for (let tmp of test) { + console.log(tmp); + let dd; + dd = [ "e", "f", "g" ]; + for (let tmp of dd) { + console.log(tmp); + } + } + } + expect: { + var test = [ "a", "b", "c" ]; + for (let o of test) { + let e; + console.log(o), e = [ "e", "f", "g" ]; + for (let o of e) + console.log(o); + } + } + expect_stdout: true + node_version: ">=6" +} + +same_variable_in_multiple_forOf_sequences_const: { + options = { + hoist_funs: true, + dead_code: true, + conditionals: true, + comparisons: true, + evaluate: true, + booleans: true, + loops: true, + unused: true, + keep_fargs: true, + if_return: true, + join_vars: true, + sequences: true, + side_effects: true, + collapse_vars: true, + } + mangle = {} + input: { + var test = [ "a", "b", "c" ]; + for (const tmp of test) { + console.log(tmp); + let dd; + dd = [ "e", "f", "g" ]; + for (const tmp of dd) { + console.log(tmp); + } + } + } + expect: { + var test = [ "a", "b", "c" ]; + for (const o of test) { + let t; + console.log(o), t = [ "e", "f", "g" ]; + for (const o of t) + console.log(o); + } + } + expect_stdout: true + node_version: ">=6" +} + +same_variable_in_multiple_forIn_sequences_let: { + options = { + hoist_funs: true, + dead_code: true, + conditionals: true, + comparisons: true, + evaluate: true, + booleans: true, + loops: true, + unused: false, + keep_fargs: true, + if_return: true, + join_vars: true, + sequences: true, + side_effects: true, + collapse_vars: true, + } + mangle = {} + input: { + var test = [ "a", "b", "c" ]; + for (let tmp in test) { + console.log(tmp); + let dd; + dd = [ "e", "f", "g" ]; + for (let tmp in test) { + console.log(tmp); + } + } + } + expect: { + var test = [ "a", "b", "c" ]; + for (let e in test) { + let t; + console.log(e), t = [ "e", "f", "g" ]; + for (let e in test) + console.log(e); + } + } + expect_stdout: true + node_version: ">=6" +} + +same_variable_in_multiple_forIn_sequences_const: { + options = { + hoist_funs: true, + dead_code: true, + conditionals: true, + comparisons: true, + evaluate: true, + booleans: true, + loops: true, + unused: false, + keep_fargs: true, + if_return: true, + join_vars: true, + sequences: true, + side_effects: true, + collapse_vars: true, + } + mangle = {} + input: { + var test = [ "a", "b", "c" ]; + for (const tmp in test) { + console.log(tmp); + let dd; + dd = [ "e", "f", "g" ]; + for (const tmp in test) { + console.log(tmp); + } + } + } + expect: { + var test = [ "a", "b", "c" ]; + for (const o in test) { + let t; + console.log(o), t = [ "e", "f", "g" ]; + for (const o in test) + console.log(o); + } + } + expect_stdout: true + node_version: ">=6" } more_variable_in_multiple_for: { @@ -283,4 +455,5 @@ more_variable_in_multiple_for: { } } expect_stdout: true + node_version: ">=6" }