diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-07-15 22:50:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-15 22:50:59 +0800 |
commit | 9e1da9235ea498760f45709848b76469f5b2a585 (patch) | |
tree | da81a0cca3c519b50f3cbb4b03f46247b2d66120 | |
parent | a5ffe2c23fdfaf13f3466a01d9dd9d590c5e8672 (diff) | |
download | tracifyjs-9e1da9235ea498760f45709848b76469f5b2a585.tar.gz tracifyjs-9e1da9235ea498760f45709848b76469f5b2a585.zip |
ensure `ie8` works with mangled properties (#2238)
fixes #2234
-rw-r--r-- | lib/compress.js | 13 | ||||
-rw-r--r-- | lib/output.js | 22 | ||||
-rw-r--r-- | test/compress/properties.js | 8 | ||||
-rw-r--r-- | 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(); }); }); |