parse destructuring under strict mode correctly (#4429)

This commit is contained in:
Alex Lam S.L
2020-12-20 12:48:51 +00:00
committed by GitHub
parent 89198e0ad4
commit 7aefe97083
3 changed files with 29 additions and 8 deletions

View File

@@ -1551,8 +1551,7 @@ function parse($TEXT, options) {
next(); next();
return "" + tmp.value; return "" + tmp.value;
case "punc": case "punc":
if (tmp.value != "[") unexpected(); expect("[");
next();
var key = maybe_assign(); var key = maybe_assign();
expect("]"); expect("]");
return key; return key;
@@ -1616,21 +1615,21 @@ function parse($TEXT, options) {
// allow trailing comma // allow trailing comma
if (!options.strict && is("punc", "}")) break; if (!options.strict && is("punc", "}")) break;
var key_start = S.token; var key_start = S.token;
var key = as_property_key(); if (is("punc", "[") || is_token(peek(), "punc", ":")) {
if (!is("punc", ":") && key_start.type == "name") { var key = as_property_key();
expect(":");
a.push(new AST_DestructuredKeyVal({ a.push(new AST_DestructuredKeyVal({
start: key_start, start: key_start,
key: key, key: key,
value: _make_symbol(type, key_start), value: maybe_destructured(type),
end: prev(), end: prev(),
})); }));
continue; continue;
} }
expect(":");
a.push(new AST_DestructuredKeyVal({ a.push(new AST_DestructuredKeyVal({
start: key_start, start: key_start,
key: key, key: key_start.value,
value: maybe_destructured(type), value: as_symbol(type),
end: prev(), end: prev(),
})); }));
} }

View File

@@ -0,0 +1,8 @@
function f() {
var { eval } = null;
}
function g() {
"use strict";
var { eval } = 42;
}

View File

@@ -573,6 +573,20 @@ describe("bin/uglifyjs", function() {
done(); 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) { it("Should throw syntax error (else)", function(done) {
var command = uglifyjscmd + " test/input/invalid/else.js"; var command = uglifyjscmd + " test/input/invalid/else.js";
exec(command, function(err, stdout, stderr) { exec(command, function(err, stdout, stderr) {