ソースを参照

Make `for (var x = 0 in ...` a syntax error in strict mode

Fixes #1340 and is the result of
https://github.com/tc39/ecma262/pull/614
Ian Halliday 9 年 前
コミット
724767c9d1
3 ファイル変更26 行追加2 行削除
  1. 1 1
      lib/Parser/Parse.cpp
  2. 2 0
      test/ControlFlow/forInMisc.baseline
  3. 23 1
      test/ControlFlow/forInMisc.js

+ 1 - 1
lib/Parser/Parse.cpp

@@ -8737,7 +8737,7 @@ ParseNodePtr Parser::ParseVariableDeclaration(
                 {
                     Error(ERRUnexpectedDefault);
                 }
-                if (pfForInOk && (declarationType == tkLET || declarationType == tkCONST))
+                if (pfForInOk && (declarationType == tkLET || declarationType == tkCONST || IsStrictMode()))
                 {
                     *pfForInOk = FALSE;
                 }

+ 2 - 0
test/ControlFlow/forInMisc.baseline

@@ -1,3 +1,5 @@
+testForInInitializer strict: success: e = SyntaxError: for-in loop head declarations cannot have an initializer
+testForInInitializer non-strict: success: i = 0
 enumerated index: 0
 enumerated index: 1
 enumerated index: 2

+ 23 - 1
test/ControlFlow/forInMisc.js

@@ -5,10 +5,32 @@
 
 var echo = WScript.Echo;
 
+// https://github.com/Microsoft/ChakraCore/issues/1340
+function testForInInitializer() {
+    try {
+        eval('(function () { "use strict"; for (var i = 0 in { }) { } })');
+        print('testForInInitializer strict: failure: did not throw');
+    } catch (e) {
+        var m = '' + e;
+        var result = m === 'SyntaxError: for-in loop head declarations cannot have an initializer' ? 'success' : 'failure';
+        print('testForInInitializer strict: ' + result + ': e = ' + m);
+    }
+
+    try {
+        var f = eval('(function () { for (var i = 0 in { }) { } return i; })');
+        var i = f();
+        var result = i === 0 ? 'success' : 'failure';
+        print('testForInInitializer non-strict: ' + result + ': i = ' + i);
+    } catch (e) {
+        print('testForInInitializer non-strict: failure: e = ' + e);
+    }
+}
+testForInInitializer();
+
 // regress WOOB 1143623
 function find(arr, value) {
     var result = -1;
-    
+
     for(var i in arr)
     {
         echo("enumerated index:", i);