reject invalid `for await` syntax (#4847)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 7 Apr 2021 14:37:15 +0000 (15:37 +0100)
committerGitHub <noreply@github.com>
Wed, 7 Apr 2021 14:37:15 +0000 (22:37 +0800)
lib/parse.js
test/input/invalid/for-await.js [new file with mode: 0644]
test/mocha/cli.js

index bc13b60..323769a 100644 (file)
@@ -1189,7 +1189,7 @@ function parse($TEXT, options) {
         var await = is("name", "await") && next();
         expect("(");
         var init = null;
-        if (!is("punc", ";")) {
+        if (await || !is("punc", ";")) {
             init = is("keyword", "const")
                 ? (next(), const_(true))
                 : is("keyword", "let")
diff --git a/test/input/invalid/for-await.js b/test/input/invalid/for-await.js
new file mode 100644 (file)
index 0000000..13e0760
--- /dev/null
@@ -0,0 +1 @@
+for await (; console.log(42););
index 4d4972e..5b84eba 100644 (file)
@@ -679,6 +679,20 @@ describe("bin/uglifyjs", function() {
             done();
         });
     });
+    it("Should throw syntax error (for-await)", function(done) {
+        var command = uglifyjscmd + " test/input/invalid/for-await.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/for-await.js:1,11",
+                "for await (; console.log(42););",
+                "           ^",
+                "ERROR: Unexpected token: punc «;»",
+            ].join("\n"));
+            done();
+        });
+    });
     it("Should throw syntax error (switch defaults)", function(done) {
         var command = uglifyjscmd + " test/input/invalid/switch.js";
         exec(command, function(err, stdout, stderr) {