Skip to content

Commit 2623fe7

Browse files
Andaristevanw
andauthored
Fixed false positive syntax errors with in inside for (#54801)
Co-authored-by: Evan Wallace <[email protected]>
1 parent e607c8e commit 2623fe7

15 files changed

+262
-2
lines changed

src/compiler/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7487,15 +7487,15 @@ namespace Parser {
74877487
function parseObjectBindingPattern(): ObjectBindingPattern {
74887488
const pos = getNodePos();
74897489
parseExpected(SyntaxKind.OpenBraceToken);
7490-
const elements = parseDelimitedList(ParsingContext.ObjectBindingElements, parseObjectBindingElement);
7490+
const elements = allowInAnd(() => parseDelimitedList(ParsingContext.ObjectBindingElements, parseObjectBindingElement));
74917491
parseExpected(SyntaxKind.CloseBraceToken);
74927492
return finishNode(factory.createObjectBindingPattern(elements), pos);
74937493
}
74947494

74957495
function parseArrayBindingPattern(): ArrayBindingPattern {
74967496
const pos = getNodePos();
74977497
parseExpected(SyntaxKind.OpenBracketToken);
7498-
const elements = parseDelimitedList(ParsingContext.ArrayBindingElements, parseArrayBindingElement);
7498+
const elements = allowInAnd(() => parseDelimitedList(ParsingContext.ArrayBindingElements, parseArrayBindingElement));
74997499
parseExpected(SyntaxKind.CloseBracketToken);
75007500
return finishNode(factory.createArrayBindingPattern(elements), pos);
75017501
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
parserForInStatement8.ts(3,10): error TS2461: Type 'string' is not an array type.
2+
parserForInStatement8.ts(3,10): error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern.
3+
parserForInStatement8.ts(4,10): error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern.
4+
parserForInStatement8.ts(4,11): error TS2339: Property 'x' does not exist on type 'String'.
5+
6+
7+
==== parserForInStatement8.ts (4 errors) ====
8+
// repro from https://github.com/microsoft/TypeScript/issues/54769
9+
10+
for (let [x = 'a' in {}] in { '': 0 }) console.log(x)
11+
~~~~~~~~~~~~~~~
12+
!!! error TS2461: Type 'string' is not an array type.
13+
~~~~~~~~~~~~~~~
14+
!!! error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern.
15+
for (let {x = 'a' in {}} in { '': 0 }) console.log(x)
16+
~~~~~~~~~~~~~~~
17+
!!! error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern.
18+
~
19+
!!! error TS2339: Property 'x' does not exist on type 'String'.
20+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement8.ts] ////
2+
3+
//// [parserForInStatement8.ts]
4+
// repro from https://github.com/microsoft/TypeScript/issues/54769
5+
6+
for (let [x = 'a' in {}] in { '': 0 }) console.log(x)
7+
for (let {x = 'a' in {}} in { '': 0 }) console.log(x)
8+
9+
10+
//// [parserForInStatement8.js]
11+
// repro from https://github.com/microsoft/TypeScript/issues/54769
12+
for (var _a = (void 0)[0], x = _a === void 0 ? 'a' in {} : _a in { '': 0 })
13+
console.log(x);
14+
for (var _b = (void 0).x, x = _b === void 0 ? 'a' in {} : _b in { '': 0 })
15+
console.log(x);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement8.ts] ////
2+
3+
=== parserForInStatement8.ts ===
4+
// repro from https://github.com/microsoft/TypeScript/issues/54769
5+
6+
for (let [x = 'a' in {}] in { '': 0 }) console.log(x)
7+
>x : Symbol(x, Decl(parserForInStatement8.ts, 2, 10))
8+
>'' : Symbol('', Decl(parserForInStatement8.ts, 2, 29))
9+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
10+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
11+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
12+
>x : Symbol(x, Decl(parserForInStatement8.ts, 2, 10))
13+
14+
for (let {x = 'a' in {}} in { '': 0 }) console.log(x)
15+
>x : Symbol(x, Decl(parserForInStatement8.ts, 3, 10))
16+
>'' : Symbol('', Decl(parserForInStatement8.ts, 3, 29))
17+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
18+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
19+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
20+
>x : Symbol(x, Decl(parserForInStatement8.ts, 3, 10))
21+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement8.ts] ////
2+
3+
=== parserForInStatement8.ts ===
4+
// repro from https://github.com/microsoft/TypeScript/issues/54769
5+
6+
for (let [x = 'a' in {}] in { '': 0 }) console.log(x)
7+
>x : any
8+
>'a' in {} : boolean
9+
>'a' : "a"
10+
>{} : {}
11+
>{ '': 0 } : { '': number; }
12+
>'' : number
13+
>0 : 0
14+
>console.log(x) : void
15+
>console.log : (...data: any[]) => void
16+
>console : Console
17+
>log : (...data: any[]) => void
18+
>x : any
19+
20+
for (let {x = 'a' in {}} in { '': 0 }) console.log(x)
21+
>x : any
22+
>'a' in {} : boolean
23+
>'a' : "a"
24+
>{} : {}
25+
>{ '': 0 } : { '': number; }
26+
>'' : number
27+
>0 : 0
28+
>console.log(x) : void
29+
>console.log : (...data: any[]) => void
30+
>console : Console
31+
>log : (...data: any[]) => void
32+
>x : any
33+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
parserForOfStatement25.ts(4,11): error TS2339: Property 'x' does not exist on type '{}'.
2+
3+
4+
==== parserForOfStatement25.ts (1 errors) ====
5+
// repro from https://github.com/microsoft/TypeScript/issues/54769
6+
7+
for (let [x = 'a' in {}] of [[]]) console.log(x)
8+
for (let {x = 'a' in {}} of [{}]) console.log(x)
9+
~
10+
!!! error TS2339: Property 'x' does not exist on type '{}'.
11+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement25.ts] ////
2+
3+
//// [parserForOfStatement25.ts]
4+
// repro from https://github.com/microsoft/TypeScript/issues/54769
5+
6+
for (let [x = 'a' in {}] of [[]]) console.log(x)
7+
for (let {x = 'a' in {}} of [{}]) console.log(x)
8+
9+
10+
//// [parserForOfStatement25.js]
11+
// repro from https://github.com/microsoft/TypeScript/issues/54769
12+
for (let [x = 'a' in {}] of [[]])
13+
console.log(x);
14+
for (let { x = 'a' in {} } of [{}])
15+
console.log(x);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement25.ts] ////
2+
3+
=== parserForOfStatement25.ts ===
4+
// repro from https://github.com/microsoft/TypeScript/issues/54769
5+
6+
for (let [x = 'a' in {}] of [[]]) console.log(x)
7+
>x : Symbol(x, Decl(parserForOfStatement25.ts, 2, 10))
8+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
9+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
10+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
11+
>x : Symbol(x, Decl(parserForOfStatement25.ts, 2, 10))
12+
13+
for (let {x = 'a' in {}} of [{}]) console.log(x)
14+
>x : Symbol(x, Decl(parserForOfStatement25.ts, 3, 10))
15+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
16+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
17+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
18+
>x : Symbol(x, Decl(parserForOfStatement25.ts, 3, 10))
19+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement25.ts] ////
2+
3+
=== parserForOfStatement25.ts ===
4+
// repro from https://github.com/microsoft/TypeScript/issues/54769
5+
6+
for (let [x = 'a' in {}] of [[]]) console.log(x)
7+
>x : boolean
8+
>'a' in {} : boolean
9+
>'a' : "a"
10+
>{} : {}
11+
>[[]] : undefined[][]
12+
>[] : undefined[]
13+
>console.log(x) : void
14+
>console.log : (...data: any[]) => void
15+
>console : Console
16+
>log : (...data: any[]) => void
17+
>x : boolean
18+
19+
for (let {x = 'a' in {}} of [{}]) console.log(x)
20+
>x : any
21+
>'a' in {} : boolean
22+
>'a' : "a"
23+
>{} : {}
24+
>[{}] : {}[]
25+
>{} : {}
26+
>console.log(x) : void
27+
>console.log : (...data: any[]) => void
28+
>console : Console
29+
>log : (...data: any[]) => void
30+
>x : any
31+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement9.ts] ////
2+
3+
//// [parserForStatement9.ts]
4+
// repro from https://github.com/microsoft/TypeScript/issues/54769
5+
6+
for (let [x = 'a' in {}] = []; !x; x = !x) console.log(x)
7+
for (let {x = 'a' in {}} = {}; !x; x = !x) console.log(x)
8+
9+
10+
//// [parserForStatement9.js]
11+
// repro from https://github.com/microsoft/TypeScript/issues/54769
12+
for (var _a = [][0], x = _a === void 0 ? 'a' in {} : _a; !x; x = !x)
13+
console.log(x);
14+
for (var _b = {}.x, x = _b === void 0 ? 'a' in {} : _b; !x; x = !x)
15+
console.log(x);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement9.ts] ////
2+
3+
=== parserForStatement9.ts ===
4+
// repro from https://github.com/microsoft/TypeScript/issues/54769
5+
6+
for (let [x = 'a' in {}] = []; !x; x = !x) console.log(x)
7+
>x : Symbol(x, Decl(parserForStatement9.ts, 2, 10))
8+
>x : Symbol(x, Decl(parserForStatement9.ts, 2, 10))
9+
>x : Symbol(x, Decl(parserForStatement9.ts, 2, 10))
10+
>x : Symbol(x, Decl(parserForStatement9.ts, 2, 10))
11+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
12+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
13+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
14+
>x : Symbol(x, Decl(parserForStatement9.ts, 2, 10))
15+
16+
for (let {x = 'a' in {}} = {}; !x; x = !x) console.log(x)
17+
>x : Symbol(x, Decl(parserForStatement9.ts, 3, 10))
18+
>x : Symbol(x, Decl(parserForStatement9.ts, 3, 10))
19+
>x : Symbol(x, Decl(parserForStatement9.ts, 3, 10))
20+
>x : Symbol(x, Decl(parserForStatement9.ts, 3, 10))
21+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
22+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
23+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
24+
>x : Symbol(x, Decl(parserForStatement9.ts, 3, 10))
25+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//// [tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement9.ts] ////
2+
3+
=== parserForStatement9.ts ===
4+
// repro from https://github.com/microsoft/TypeScript/issues/54769
5+
6+
for (let [x = 'a' in {}] = []; !x; x = !x) console.log(x)
7+
>x : boolean
8+
>'a' in {} : boolean
9+
>'a' : "a"
10+
>{} : {}
11+
>[] : []
12+
>!x : boolean
13+
>x : boolean
14+
>x = !x : boolean
15+
>x : boolean
16+
>!x : boolean
17+
>x : boolean
18+
>console.log(x) : void
19+
>console.log : (...data: any[]) => void
20+
>console : Console
21+
>log : (...data: any[]) => void
22+
>x : boolean
23+
24+
for (let {x = 'a' in {}} = {}; !x; x = !x) console.log(x)
25+
>x : boolean
26+
>'a' in {} : boolean
27+
>'a' : "a"
28+
>{} : {}
29+
>{} : { x?: boolean; }
30+
>!x : boolean
31+
>x : boolean
32+
>x = !x : boolean
33+
>x : boolean
34+
>!x : boolean
35+
>x : boolean
36+
>console.log(x) : void
37+
>console.log : (...data: any[]) => void
38+
>console : Console
39+
>log : (...data: any[]) => void
40+
>x : boolean
41+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// repro from https://github.com/microsoft/TypeScript/issues/54769
2+
3+
for (let [x = 'a' in {}] in { '': 0 }) console.log(x)
4+
for (let {x = 'a' in {}} in { '': 0 }) console.log(x)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// repro from https://github.com/microsoft/TypeScript/issues/54769
2+
3+
for (let [x = 'a' in {}] = []; !x; x = !x) console.log(x)
4+
for (let {x = 'a' in {}} = {}; !x; x = !x) console.log(x)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @target: esnext
2+
3+
// repro from https://github.com/microsoft/TypeScript/issues/54769
4+
5+
for (let [x = 'a' in {}] of [[]]) console.log(x)
6+
for (let {x = 'a' in {}} of [{}]) console.log(x)

0 commit comments

Comments
 (0)