From 9e1da9235ea498760f45709848b76469f5b2a585 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 15 Jul 2017 22:50:59 +0800 Subject: [PATCH] ensure `ie8` works with mangled properties (#2238) fixes #2234 --- lib/compress.js | 13 ++----------- lib/output.js | 22 +++++++++++++++------- test/compress/properties.js | 8 ++++++-- test/mocha/let.js | 35 +++++++++++++++++++++++++++-------- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 6dc7b725..7a16ba86 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4395,7 +4395,7 @@ merge(Compressor.prototype, { var prop = self.property; if (prop instanceof AST_String && compressor.option("properties")) { prop = prop.getValue(); - if (RESERVED_WORDS(prop) ? !compressor.option("ie8") : is_identifier_string(prop)) { + if (is_identifier_string(prop)) { return make_node(AST_Dot, self, { expression : self.expression, property : prop @@ -4432,19 +4432,10 @@ merge(Compressor.prototype, { if (def) { return def.optimize(compressor); } - var prop = self.property; - if (RESERVED_WORDS(prop) && compressor.option("ie8")) { - return make_node(AST_Sub, self, { - expression : self.expression, - property : make_node(AST_String, self, { - value: prop - }) - }).optimize(compressor); - } if (compressor.option("unsafe") && self.expression instanceof AST_Object) { var values = self.expression.properties; for (var i = values.length; --i >= 0;) { - if (values[i].key === prop) { + if (values[i].key === self.property) { var value = values[i].value; if (value instanceof AST_Function ? !value.contains_this() : !value.has_side_effects(compressor)) { var obj = self.expression.clone(); diff --git a/lib/output.js b/lib/output.js index 6ee96b31..edb8d182 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1146,15 +1146,23 @@ function OutputStream(options) { DEFPRINT(AST_Dot, function(self, output){ var expr = self.expression; expr.print(output); - if (expr instanceof AST_Number && expr.getValue() >= 0) { - if (!/[xa-f.)]/i.test(output.last())) { - output.print("."); + var prop = self.property; + if (output.option("ie8") && RESERVED_WORDS(prop)) { + output.print("["); + output.add_mapping(self.end); + output.print_string(prop); + output.print("]"); + } else { + if (expr instanceof AST_Number && expr.getValue() >= 0) { + if (!/[xa-f.)]/i.test(output.last())) { + output.print("."); + } } + output.print("."); + // the name after dot would be mapped about here. + output.add_mapping(self.end); + output.print_name(prop); } - output.print("."); - // the name after dot would be mapped about here. - output.add_mapping(self.end); - output.print_name(self.property); }); DEFPRINT(AST_Sub, function(self, output){ self.expression.print(output); diff --git a/test/compress/properties.js b/test/compress/properties.js index a5527de3..dda2e74f 100644 --- a/test/compress/properties.js +++ b/test/compress/properties.js @@ -13,8 +13,10 @@ keep_properties: { dot_properties: { options = { properties: true, + } + beautify = { ie8: true, - }; + } input: { a["foo"] = "bar"; a["if"] = "if"; @@ -36,8 +38,10 @@ dot_properties: { dot_properties_es5: { options = { properties: true, + } + beautify = { ie8: false, - }; + } input: { a["foo"] = "bar"; a["if"] = "if"; diff --git a/test/mocha/let.js b/test/mocha/let.js index 23909986..8685746b 100644 --- a/test/mocha/let.js +++ b/test/mocha/let.js @@ -2,16 +2,17 @@ var Uglify = require('../../'); var assert = require("assert"); describe("let", function() { - it("Should not produce reserved keywords as variable name in mangle", function(done) { - this.timeout(10000); - + this.timeout(30000); + it("Should not produce reserved keywords as variable name in mangle", function() { // Produce a lot of variables in a function and run it through mangle. var s = '"dddddeeeeelllllooooottttt"; function foo() {'; for (var i = 0; i < 18000; i++) { s += "var v" + i + "=0;"; } s += '}'; - var result = Uglify.minify(s, {compress: false}); + var result = Uglify.minify(s, { + compress: false + }).code; // Verify that select keywords and reserved keywords not produced [ @@ -19,7 +20,7 @@ describe("let", function() { "let", "var", ].forEach(function(name) { - assert.strictEqual(result.code.indexOf("var " + name + "="), -1); + assert.strictEqual(result.indexOf("var " + name + "="), -1); }); // Verify that the variable names that appeared immediately before @@ -30,9 +31,27 @@ describe("let", function() { "eet", "fet", "rar", "oar", ].forEach(function(name) { - assert.ok(result.code.indexOf("var " + name + "=") >= 0); + assert.notStrictEqual(result.indexOf("var " + name + "="), -1); + }); + }); + it("Should quote mangled properties that are reserved keywords", function() { + var s = '"rrrrrnnnnniiiiiaaaaa";'; + for (var i = 0; i < 18000; i++) { + s += "v.b" + i + ";"; + } + var result = Uglify.minify(s, { + compress: false, + ie8: true, + mangle: { + properties: true, + } + }).code; + [ + "in", + "var", + ].forEach(function(name) { + assert.notStrictEqual(result.indexOf(name), -1); + assert.notStrictEqual(result.indexOf('v["' + name + '"]'), -1); }); - - done(); }); }); -- 2.34.1