Skip to content

Commit

Permalink
parse destructuring under strict mode correctly (#4429)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Dec 20, 2020
1 parent 89198e0 commit 7aefe97
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
}));
}
Expand Down
8 changes: 8 additions & 0 deletions test/input/invalid/destructured_var.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function f() {
var { eval } = null;
}

function g() {
"use strict";
var { eval } = 42;
}
14 changes: 14 additions & 0 deletions test/mocha/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7aefe97

Please sign in to comment.