diff --git a/lib/compress.js b/lib/compress.js index 388e2d43..ec083fbe 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -290,6 +290,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; @@ -405,10 +409,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/lib/parse.js b/lib/parse.js index 0fe9e0cf..9c678929 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1322,6 +1322,9 @@ function parse($TEXT, options) { if (in_statement && !name) unexpected(); + if (name && ctor !== AST_Accessor && !(name instanceof AST_SymbolDeclaration)) + unexpected(prev()); + var args = parameters(); var body = _function_body(true, is_generator || is_generator_property, is_async, name, args); return new ctor({ diff --git a/package.json b/package.json index f0633b46..bd69d53e 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "homepage": "https://github.com/mishoo/UglifyJS2/tree/harmony", "author": "Mihai Bazon (http://lisperator.net/)", "license": "BSD-2-Clause", - "version": "3.1.2", + "version": "3.1.3", "engines": { "node": ">=0.8.0" }, diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 7364cc4d..19099424 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" +} + issue_2090_1: { options = { evaluate: true, diff --git a/test/mocha/minify.js b/test/mocha/minify.js index 376b6a50..45f918a5 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};';