You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We incorrectly throw a syntax error when declaring a top level for-loop iteration variable the same as a parameter
https://bugs.webkit.org/show_bug.cgi?id=171041
<rdar://problem/32082516>
Reviewed by Yusuke Suzuki.
JSTests:
* stress/lexical-scoping-for-loop.js: Added.
(assert):
(test1):
(test2):
(test3):
(test4):
(test5):
(test6):
(let.test7):
(let.test8):
(let.test9):
(let.test10):
(let.test11):
(let.test12):
Source/JavaScriptCore:
We were treating a for-loop variable declaration potentially as a top
level statement, e.g, in a program like this:
```
function foo() {
for (let variable of expr) { }
}
```
But we should not be. This had the consequence of making this type of program
throw a syntax error:
```
function foo(arg) {
for (let arg of expr) { }
}
```
even though it should not. The fix is simple, we just need to increment the
statement depth before parsing anything inside the for loop.
* parser/Parser.cpp:
(JSC::Parser<LexerType>::parseForStatement):
LayoutTests:
* js/parser-syntax-check-expected.txt:
* js/script-tests/parser-syntax-check.js:
Canonical link: https://commits.webkit.org/189327@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@217200 268f45cc-cd09-0410-ab3c-d52691b4dbfc
Copy file name to clipboardExpand all lines: LayoutTests/js/parser-syntax-check-expected.txt
+48Lines changed: 48 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -556,6 +556,54 @@ PASS Invalid: "for (const {i} = 20 in b) { }". Produced the following syntax err
556
556
PASS Invalid: "function f() { for (const {i} = 20 in b) { } }". Produced the following syntax error: "SyntaxError: Cannot assign to the loop variable inside a for-in loop header."
557
557
PASS Invalid: "for (let {i} = 20 in b) { }". Produced the following syntax error: "SyntaxError: Cannot assign to the loop variable inside a for-in loop header."
558
558
PASS Invalid: "function f() { for (let {i} = 20 in b) { } }". Produced the following syntax error: "SyntaxError: Cannot assign to the loop variable inside a for-in loop header."
559
+
PASS Valid: "function x(i) { for (let i in {}) { } }"
560
+
PASS Valid: "function f() { function x(i) { for (let i in {}) { } } }"
561
+
PASS Valid: "function x(i) { for (let i of []) { } }"
562
+
PASS Valid: "function f() { function x(i) { for (let i of []) { } } }"
563
+
PASS Valid: "function x(i) { for (let i of []) { } }"
564
+
PASS Valid: "function f() { function x(i) { for (let i of []) { } } }"
PASS Valid: "function x(i) { for (const i in {}) { } }"
584
+
PASS Valid: "function f() { function x(i) { for (const i in {}) { } } }"
585
+
PASS Valid: "function x(i) { for (const i of []) { } }"
586
+
PASS Valid: "function f() { function x(i) { for (const i of []) { } } }"
587
+
PASS Valid: "function x(i) { for (const i of []) { } }"
588
+
PASS Valid: "function f() { function x(i) { for (const i of []) { } } }"
589
+
PASS Valid: "function x(i) { for (const i = 20; false; ) { } }"
590
+
PASS Valid: "function f() { function x(i) { for (const i = 20; false; ) { } } }"
591
+
PASS Valid: "let f = (i) => { for (const i in {}) { } }"
592
+
PASS Valid: "function f() { let f = (i) => { for (const i in {}) { } } }"
593
+
PASS Valid: "let f = (i) => { for (const i of []) { } }"
594
+
PASS Valid: "function f() { let f = (i) => { for (const i of []) { } } }"
595
+
PASS Valid: "let f = (i) => { for (const i of []) { } }"
596
+
PASS Valid: "function f() { let f = (i) => { for (const i of []) { } } }"
597
+
PASS Valid: "let f = (i) => { for (const i = 20; false; ) { } }"
598
+
PASS Valid: "function f() { let f = (i) => { for (const i = 20; false; ) { } } }"
599
+
PASS Valid: "function* x(i) { for (const i in {}) { } }"
600
+
PASS Valid: "function f() { function* x(i) { for (const i in {}) { } } }"
601
+
PASS Valid: "function* x(i) { for (const i of []) { } }"
602
+
PASS Valid: "function f() { function* x(i) { for (const i of []) { } } }"
603
+
PASS Valid: "function* x(i) { for (const i of []) { } }"
604
+
PASS Valid: "function f() { function* x(i) { for (const i of []) { } } }"
605
+
PASS Valid: "function* x(i) { for (const i = 20; false; ) { } }"
606
+
PASS Valid: "function f() { function* x(i) { for (const i = 20; false; ) { } } }"
559
607
try statement
560
608
PASS Invalid: "try { break } catch(e) {}". Produced the following syntax error: "SyntaxError: 'break' is only valid inside a switch or loop statement."
561
609
PASS Invalid: "function f() { try { break } catch(e) {} }". Produced the following syntax error: "SyntaxError: 'break' is only valid inside a switch or loop statement."
0 commit comments