aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-12-20 12:48:51 +0000
committerGitHub <noreply@github.com>2020-12-20 20:48:51 +0800
commit7aefe97083c63660cf0708e549aebce66248796b (patch)
treeb7bde3b849be0303ab6ca7118677a95767b3af68
parent89198e0ad4e302546e0ec8b6653ee1b92ce09b1a (diff)
downloadtracifyjs-7aefe97083c63660cf0708e549aebce66248796b.tar.gz
tracifyjs-7aefe97083c63660cf0708e549aebce66248796b.zip
parse destructuring under strict mode correctly (#4429)
-rw-r--r--lib/parse.js15
-rw-r--r--test/input/invalid/destructured_var.js8
-rw-r--r--test/mocha/cli.js14
3 files changed, 29 insertions, 8 deletions
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) {