From 7aefe97083c63660cf0708e549aebce66248796b Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 20 Dec 2020 12:48:51 +0000 Subject: [PATCH] parse destructuring under strict mode correctly (#4429) --- lib/parse.js | 15 +++++++-------- test/input/invalid/destructured_var.js | 8 ++++++++ test/mocha/cli.js | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 test/input/invalid/destructured_var.js diff --git a/lib/parse.js b/lib/parse.js index 27fa0424..111be3b5 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1551,8 +1551,7 @@ function parse($TEXT, options) { next(); return "" + tmp.value; case "punc": - if (tmp.value != "[") unexpected(); - next(); + expect("["); var key = maybe_assign(); expect("]"); return key; @@ -1616,21 +1615,21 @@ function parse($TEXT, options) { // allow trailing comma if (!options.strict && is("punc", "}")) break; var key_start = S.token; - var key = as_property_key(); - if (!is("punc", ":") && key_start.type == "name") { + if (is("punc", "[") || is_token(peek(), "punc", ":")) { + var key = as_property_key(); + expect(":"); a.push(new AST_DestructuredKeyVal({ start: key_start, key: key, - value: _make_symbol(type, key_start), + value: maybe_destructured(type), end: prev(), })); continue; } - expect(":"); a.push(new AST_DestructuredKeyVal({ start: key_start, - key: key, - value: maybe_destructured(type), + key: key_start.value, + value: as_symbol(type), end: prev(), })); } diff --git a/test/input/invalid/destructured_var.js b/test/input/invalid/destructured_var.js new file mode 100644 index 00000000..8e882f7e --- /dev/null +++ b/test/input/invalid/destructured_var.js @@ -0,0 +1,8 @@ +function f() { + var { eval } = null; +} + +function g() { + "use strict"; + var { eval } = 42; +} diff --git a/test/mocha/cli.js b/test/mocha/cli.js index 034c8440..c050b717 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -573,6 +573,20 @@ describe("bin/uglifyjs", function() { done(); }); }); + it("Should throw syntax error (var { eval })", function(done) { + var command = uglifyjscmd + " test/input/invalid/destructured_var.js"; + exec(command, function(err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/destructured_var.js:7,10", + " var { eval } = 42;", + " ^", + "ERROR: Unexpected eval in strict mode" + ].join("\n")); + done(); + }); + }); it("Should throw syntax error (else)", function(done) { var command = uglifyjscmd + " test/input/invalid/else.js"; exec(command, function(err, stdout, stderr) { -- 2.34.1