From 1eea65e82e23e1405ad1f7f266d4922f7b18c3c5 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Wed, 25 Oct 2017 02:33:45 +0800 Subject: [PATCH] fix non-string property key avoid unused variable --- lib/compress.js | 10 +++--- test/compress/hoist_props.js | 68 +++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index dbba272a..9f410718 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2693,7 +2693,10 @@ merge(Compressor.prototype, { self.enclosed.forEach(function(def) { var_names[def.name] = true; }); - var tt = new TreeTransformer(function(node) { + self.variables.each(function(def, name) { + var_names[name] = true; + }); + return self.transform(new TreeTransformer(function(node) { if (node instanceof AST_VarDef) { var sym = node.name, def, value; if (sym.scope === self @@ -2731,7 +2734,7 @@ merge(Compressor.prototype, { } function make_sym(key) { - var prefix = sym.name + "_" + key.replace(/[^a-z_$]+/ig, "_"); + var prefix = sym.name + "_" + key.toString().replace(/[^a-z_$]+/ig, "_"); var name = prefix; for (var i = 0; var_names[name]; i++) name = prefix + "$" + i; var new_var = make_node(sym.CTOR, sym, { @@ -2744,8 +2747,7 @@ merge(Compressor.prototype, { var_names[name] = true; return new_var; } - }); - return self.transform(tt); + })); }); // drop_side_effect_free() diff --git a/test/compress/hoist_props.js b/test/compress/hoist_props.js index 85a5c573..6396a7a8 100644 --- a/test/compress/hoist_props.js +++ b/test/compress/hoist_props.js @@ -184,7 +184,7 @@ single_use: { } } -name_collision: { +name_collision_1: { options = { reduce_vars: true, hoist_props: true, @@ -220,3 +220,69 @@ name_collision: { } expect_stdout: "1 3 4 5 6 7" } + +name_collision_2: { + options = { + reduce_vars: true, + hoist_props: true, + toplevel: true, + } + input: { + var o = { + p: 1, + 0: function(x) { + return x; + }, + 1: function(x) { + return x + 1; + } + }, o__$0 = 2, o__$1 = 3; + console.log(o.p === o.p, o[0](4), o[1](5), o__$0, o__$1); + } + expect: { + var o_p = 1, + o__ = function(x) { + return x; + }, + o__$2 = function(x) { + return x + 1; + }, + o__$0 = 2, + o__$1 = 3; + console.log(o_p === o_p, o__(4), o__$2(5), o__$0, o__$1); + } + expect_stdout: "true 4 6 2 3" +} + +name_collision_3: { + options = { + reduce_vars: true, + hoist_props: true, + toplevel: true, + } + input: { + var o = { + p: 1, + 0: function(x) { + return x; + }, + 1: function(x) { + return x + 1; + } + }, o__$0 = 2, o__$1 = 3; + console.log(o.p === o.p, o[0](4), o[1](5)); + } + expect: { + var o_p = 1, + o__ = function(x) { + return x; + }, + o__$2 = function(x) { + return x + 1; + }, + o__$0 = 2, + o__$1 = 3; + console.log(o_p === o_p, o__(4), o__$2(5)); + } + expect_stdout: "true 4 6" +}