From 2dcc552ce0404db3cdabb1dd02c8fff5c8dfc4f9 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 1 Oct 2017 02:10:41 +0800 Subject: [PATCH 1/4] trap invalid use of reserved words (#2338) fixes #2337 --- lib/parse.js | 2 ++ test/mocha/minify.js | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/lib/parse.js b/lib/parse.js index e2dd04b6..099fc49a 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1054,6 +1054,8 @@ function parse($TEXT, options) { var name = is("name") ? as_symbol(in_statement ? AST_SymbolDefun : AST_SymbolLambda) : null; if (in_statement && !name) unexpected(); + if (name && ctor !== AST_Accessor && !(name instanceof AST_SymbolDeclaration)) + unexpected(prev()); expect("("); var argnames = []; for (var first = true; !is("punc", ")");) { diff --git a/test/mocha/minify.js b/test/mocha/minify.js index fc7332fb..aed8f915 100644 --- a/test/mocha/minify.js +++ b/test/mocha/minify.js @@ -73,6 +73,12 @@ describe("minify", function() { assert.strictEqual(run_code(compressed), run_code(original)); }); + it("should not parse invalid use of reserved words", function() { + assert.strictEqual(Uglify.minify("function enum(){}").error, undefined); + assert.strictEqual(Uglify.minify("function static(){}").error, undefined); + assert.strictEqual(Uglify.minify("function this(){}").error.message, "Unexpected token: name (this)"); + }); + describe("keep_quoted_props", function() { it("Should preserve quotes in object literals", function() { var js = 'var foo = {"x": 1, y: 2, \'z\': 3};'; From dd71639264f201902e08d078fcef1946eaad2ef3 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 1 Oct 2017 03:01:50 +0800 Subject: [PATCH 2/4] enhance `reduce_vars` for `AST_Accessor` (#2339) fixes #2336 --- lib/compress.js | 9 ++++++--- test/compress/reduce_vars.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 9e516a89..d4a72d74 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -285,6 +285,10 @@ merge(Compressor.prototype, { AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan) { var reduce_vars = rescan && compressor.option("reduce_vars"); + // Stack of look-up tables to keep track of whether a `SymbolDef` has been + // properly assigned before use: + // - `push()` & `pop()` when visiting conditional branches + // - backup & restore via `save_ids` when visiting out-of-order sections var safe_ids = Object.create(null); var suppressor = new TreeWalker(function(node) { if (!(node instanceof AST_Symbol)) return; @@ -391,10 +395,9 @@ merge(Compressor.prototype, { return true; } if (node instanceof AST_Accessor) { - var save_ids = safe_ids; - safe_ids = Object.create(null); + push(); descend(); - safe_ids = save_ids; + pop(); return true; } if (node instanceof AST_Binary diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 556bcad2..4e096d9d 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -2549,7 +2549,7 @@ issue_1922_2: { expect_stdout: "1" } -accessor: { +accessor_1: { options = { evaluate: true, reduce_vars: true, @@ -2578,6 +2578,33 @@ accessor: { expect_stdout: "1 1" } +accessor_2: { + options = { + collapse_vars: true, + evaluate: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + var A = 1; + var B = { + get c() { + console.log(A); + } + }; + B.c; + } + expect: { + ({ + get c() { + console.log(1); + } + }).c; + } + expect_stdout: "1" +} + for_in_prop: { options = { reduce_vars: true, From 6920e898d1a89a035b54e62b83c4ad0411a0ff2e Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 1 Oct 2017 12:36:07 +0800 Subject: [PATCH 3/4] v3.1.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0dd71612..da570ada 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "homepage": "http://lisperator.net/uglifyjs", "author": "Mihai Bazon (http://lisperator.net/)", "license": "BSD-2-Clause", - "version": "3.1.2", + "version": "3.1.3", "engines": { "node": ">=0.8.0" }, From 744032755d18df1d0135aeeac89fb7a83fecf90e Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Sun, 1 Oct 2017 12:48:26 +0800 Subject: [PATCH 4/4] add tests for #2336 & #2337 --- test/compress/reduce_vars.js | 56 ++++++++++++++++++++++++++++++++++++ test/mocha/minify.js | 1 + 2 files changed, 57 insertions(+) diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 19099424..994c0aee 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -2605,6 +2605,62 @@ accessor_2: { expect_stdout: "1" } +method_1: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + } + input: { + var a = 1; + console.log(new class { + a() { + a = 2; + return a; + } + }().a(), a); + } + expect: { + var a = 1; + console.log(new class { + a() { + a = 2; + return a; + } + }().a(), a); + } + expect_stdout: "2 2" + node_version: ">=6" +} + +method_2: { + options = { + collapse_vars: true, + evaluate: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + var A = 1; + var B = class { + c() { + console.log(A); + } + }; + new B().c(); + } + expect: { + new class { + c() { + console.log(1); + } + }().c(); + } + expect_stdout: "1" + node_version: ">=6" +} + issue_2090_1: { options = { evaluate: true, diff --git a/test/mocha/minify.js b/test/mocha/minify.js index 45f918a5..db32739b 100644 --- a/test/mocha/minify.js +++ b/test/mocha/minify.js @@ -76,6 +76,7 @@ describe("minify", function() { it("should not parse invalid use of reserved words", function() { assert.strictEqual(Uglify.minify("function enum(){}").error, undefined); assert.strictEqual(Uglify.minify("function static(){}").error, undefined); + assert.strictEqual(Uglify.minify("function super(){}").error.message, "Unexpected token: name (super)"); assert.strictEqual(Uglify.minify("function this(){}").error.message, "Unexpected token: name (this)"); });