Correctly raise a parse exception with a missing loop body (#1585)
authorMichael Mior <michael.mior@gmail.com>
Thu, 9 Mar 2017 19:08:43 +0000 (14:08 -0500)
committerAlex Lam S.L <alexlamsl@gmail.com>
Thu, 9 Mar 2017 19:08:43 +0000 (03:08 +0800)
lib/parse.js
test/input/invalid/loop-no-body.js [new file with mode: 0644]
test/mocha/cli.js

index 9aadc9f..5e14086 100644 (file)
@@ -787,6 +787,8 @@ function parse($TEXT, options) {
         return function() {
             var start = S.token;
             var expr = parser();
+            if (!expr) croak("Expected expression");
+
             var end = prev();
             expr.start = start;
             expr.end = end;
diff --git a/test/input/invalid/loop-no-body.js b/test/input/invalid/loop-no-body.js
new file mode 100644 (file)
index 0000000..07b2742
--- /dev/null
@@ -0,0 +1 @@
+for (var i = 0; i < 1; i++) 
index e8e07cb..0b4fe00 100644 (file)
@@ -238,4 +238,17 @@ describe("bin/uglifyjs", function () {
             done();
         });
     });
+    it("Should fail with a missing loop body", function(done) {
+        var command = uglifyjscmd + ' test/input/invalid/loop-no-body.js';
+
+        exec(command, function (err, stdout, stderr) {
+            assert.ok(err);
+            var lines = stderr.split(/\n/);
+            assert.strictEqual(lines[0], "Parse error at test/input/invalid/loop-no-body.js:2,0");
+            assert.strictEqual(lines[1], "for (var i = 0; i < 1; i++) ");
+            assert.strictEqual(lines[2], "                            ^");
+            assert.strictEqual(lines[3], "SyntaxError: Expected expression");
+            done();
+        });
+    });
 });