parse destructuring under strict mode correctly (#4429)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 20 Dec 2020 12:48:51 +0000 (12:48 +0000)
committerGitHub <noreply@github.com>
Sun, 20 Dec 2020 12:48:51 +0000 (20:48 +0800)
lib/parse.js
test/input/invalid/destructured_var.js [new file with mode: 0644]
test/mocha/cli.js

index 27fa042..111be3b 100644 (file)
@@ -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 (file)
index 0000000..8e882f7
--- /dev/null
@@ -0,0 +1,8 @@
+function f() {
+    var { eval } = null;
+}
+
+function g() {
+    "use strict";
+    var { eval } = 42;
+}
index 034c844..c050b71 100644 (file)
@@ -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) {