Don't allow with statements in strict mode
authorAnthony Van de Gejuchte <anthonyvdgent@gmail.com>
Sun, 12 Jun 2016 16:58:20 +0000 (18:58 +0200)
committerAnthony Van de Gejuchte <anthonyvdgent@gmail.com>
Sun, 12 Jun 2016 17:08:16 +0000 (19:08 +0200)
lib/parse.js
test/mocha/with.js [new file with mode: 0644]

index 4530c2d..2c00796 100644 (file)
@@ -928,6 +928,9 @@ function parse($TEXT, options) {
                 return tmp = const_(), semicolon(), tmp;
 
               case "with":
+                if (S.input.has_directive("use strict")) {
+                    croak("Strict mode may not include a with statement");
+                }
                 return new AST_With({
                     expression : parenthesised(),
                     body       : statement()
diff --git a/test/mocha/with.js b/test/mocha/with.js
new file mode 100644 (file)
index 0000000..d284f1c
--- /dev/null
@@ -0,0 +1,16 @@
+var assert = require("assert");
+var uglify = require("../../");
+
+describe("With", function() {
+    it ("Should throw syntaxError when using with statement in strict mode", function() {
+        var code = '"use strict";\nthrow NotEarlyError;\nwith ({}) { }';
+        var test = function() {
+            uglify.parse(code);
+        }
+        var error = function(e) {
+            return e instanceof uglify.JS_Parse_Error &&
+                e.message === "Strict mode may not include a with statement";
+        }
+        assert.throws(test, error);
+    });
+});
\ No newline at end of file