Skip to content

Commit

Permalink
Fixed false positive syntax errors with in inside for (microsoft#54801)
Browse files Browse the repository at this point in the history
Co-authored-by: Evan Wallace <[email protected]>
  • Loading branch information
Andarist and evanw authored Jul 20, 2023
1 parent e607c8e commit 2623fe7
Show file tree
Hide file tree
Showing 15 changed files with 262 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7487,15 +7487,15 @@ namespace Parser {
function parseObjectBindingPattern(): ObjectBindingPattern {
const pos = getNodePos();
parseExpected(SyntaxKind.OpenBraceToken);
const elements = parseDelimitedList(ParsingContext.ObjectBindingElements, parseObjectBindingElement);
const elements = allowInAnd(() => parseDelimitedList(ParsingContext.ObjectBindingElements, parseObjectBindingElement));
parseExpected(SyntaxKind.CloseBraceToken);
return finishNode(factory.createObjectBindingPattern(elements), pos);
}

function parseArrayBindingPattern(): ArrayBindingPattern {
const pos = getNodePos();
parseExpected(SyntaxKind.OpenBracketToken);
const elements = parseDelimitedList(ParsingContext.ArrayBindingElements, parseArrayBindingElement);
const elements = allowInAnd(() => parseDelimitedList(ParsingContext.ArrayBindingElements, parseArrayBindingElement));
parseExpected(SyntaxKind.CloseBracketToken);
return finishNode(factory.createArrayBindingPattern(elements), pos);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/baselines/reference/parserForInStatement8.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
parserForInStatement8.ts(3,10): error TS2461: Type 'string' is not an array type.
parserForInStatement8.ts(3,10): error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern.
parserForInStatement8.ts(4,10): error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern.
parserForInStatement8.ts(4,11): error TS2339: Property 'x' does not exist on type 'String'.


==== parserForInStatement8.ts (4 errors) ====
// repro from https://github.com/microsoft/TypeScript/issues/54769

for (let [x = 'a' in {}] in { '': 0 }) console.log(x)
~~~~~~~~~~~~~~~
!!! error TS2461: Type 'string' is not an array type.
~~~~~~~~~~~~~~~
!!! error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern.
for (let {x = 'a' in {}} in { '': 0 }) console.log(x)
~~~~~~~~~~~~~~~
!!! error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern.
~
!!! error TS2339: Property 'x' does not exist on type 'String'.

15 changes: 15 additions & 0 deletions tests/baselines/reference/parserForInStatement8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement8.ts] ////

//// [parserForInStatement8.ts]
// repro from https://github.com/microsoft/TypeScript/issues/54769

for (let [x = 'a' in {}] in { '': 0 }) console.log(x)
for (let {x = 'a' in {}} in { '': 0 }) console.log(x)


//// [parserForInStatement8.js]
// repro from https://github.com/microsoft/TypeScript/issues/54769
for (var _a = (void 0)[0], x = _a === void 0 ? 'a' in {} : _a in { '': 0 })
console.log(x);
for (var _b = (void 0).x, x = _b === void 0 ? 'a' in {} : _b in { '': 0 })
console.log(x);
21 changes: 21 additions & 0 deletions tests/baselines/reference/parserForInStatement8.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement8.ts] ////

=== parserForInStatement8.ts ===
// repro from https://github.com/microsoft/TypeScript/issues/54769

for (let [x = 'a' in {}] in { '': 0 }) console.log(x)
>x : Symbol(x, Decl(parserForInStatement8.ts, 2, 10))
>'' : Symbol('', Decl(parserForInStatement8.ts, 2, 29))
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>x : Symbol(x, Decl(parserForInStatement8.ts, 2, 10))

for (let {x = 'a' in {}} in { '': 0 }) console.log(x)
>x : Symbol(x, Decl(parserForInStatement8.ts, 3, 10))
>'' : Symbol('', Decl(parserForInStatement8.ts, 3, 29))
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>x : Symbol(x, Decl(parserForInStatement8.ts, 3, 10))

33 changes: 33 additions & 0 deletions tests/baselines/reference/parserForInStatement8.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement8.ts] ////

=== parserForInStatement8.ts ===
// repro from https://github.com/microsoft/TypeScript/issues/54769

for (let [x = 'a' in {}] in { '': 0 }) console.log(x)
>x : any
>'a' in {} : boolean
>'a' : "a"
>{} : {}
>{ '': 0 } : { '': number; }
>'' : number
>0 : 0
>console.log(x) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>x : any

for (let {x = 'a' in {}} in { '': 0 }) console.log(x)
>x : any
>'a' in {} : boolean
>'a' : "a"
>{} : {}
>{ '': 0 } : { '': number; }
>'' : number
>0 : 0
>console.log(x) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>x : any

11 changes: 11 additions & 0 deletions tests/baselines/reference/parserForOfStatement25.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
parserForOfStatement25.ts(4,11): error TS2339: Property 'x' does not exist on type '{}'.


==== parserForOfStatement25.ts (1 errors) ====
// repro from https://github.com/microsoft/TypeScript/issues/54769

for (let [x = 'a' in {}] of [[]]) console.log(x)
for (let {x = 'a' in {}} of [{}]) console.log(x)
~
!!! error TS2339: Property 'x' does not exist on type '{}'.

15 changes: 15 additions & 0 deletions tests/baselines/reference/parserForOfStatement25.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement25.ts] ////

//// [parserForOfStatement25.ts]
// repro from https://github.com/microsoft/TypeScript/issues/54769

for (let [x = 'a' in {}] of [[]]) console.log(x)
for (let {x = 'a' in {}} of [{}]) console.log(x)


//// [parserForOfStatement25.js]
// repro from https://github.com/microsoft/TypeScript/issues/54769
for (let [x = 'a' in {}] of [[]])
console.log(x);
for (let { x = 'a' in {} } of [{}])
console.log(x);
19 changes: 19 additions & 0 deletions tests/baselines/reference/parserForOfStatement25.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement25.ts] ////

=== parserForOfStatement25.ts ===
// repro from https://github.com/microsoft/TypeScript/issues/54769

for (let [x = 'a' in {}] of [[]]) console.log(x)
>x : Symbol(x, Decl(parserForOfStatement25.ts, 2, 10))
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>x : Symbol(x, Decl(parserForOfStatement25.ts, 2, 10))

for (let {x = 'a' in {}} of [{}]) console.log(x)
>x : Symbol(x, Decl(parserForOfStatement25.ts, 3, 10))
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>x : Symbol(x, Decl(parserForOfStatement25.ts, 3, 10))

31 changes: 31 additions & 0 deletions tests/baselines/reference/parserForOfStatement25.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement25.ts] ////

=== parserForOfStatement25.ts ===
// repro from https://github.com/microsoft/TypeScript/issues/54769

for (let [x = 'a' in {}] of [[]]) console.log(x)
>x : boolean
>'a' in {} : boolean
>'a' : "a"
>{} : {}
>[[]] : undefined[][]
>[] : undefined[]
>console.log(x) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>x : boolean

for (let {x = 'a' in {}} of [{}]) console.log(x)
>x : any
>'a' in {} : boolean
>'a' : "a"
>{} : {}
>[{}] : {}[]
>{} : {}
>console.log(x) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>x : any

15 changes: 15 additions & 0 deletions tests/baselines/reference/parserForStatement9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement9.ts] ////

//// [parserForStatement9.ts]
// repro from https://github.com/microsoft/TypeScript/issues/54769

for (let [x = 'a' in {}] = []; !x; x = !x) console.log(x)
for (let {x = 'a' in {}} = {}; !x; x = !x) console.log(x)


//// [parserForStatement9.js]
// repro from https://github.com/microsoft/TypeScript/issues/54769
for (var _a = [][0], x = _a === void 0 ? 'a' in {} : _a; !x; x = !x)
console.log(x);
for (var _b = {}.x, x = _b === void 0 ? 'a' in {} : _b; !x; x = !x)
console.log(x);
25 changes: 25 additions & 0 deletions tests/baselines/reference/parserForStatement9.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement9.ts] ////

=== parserForStatement9.ts ===
// repro from https://github.com/microsoft/TypeScript/issues/54769

for (let [x = 'a' in {}] = []; !x; x = !x) console.log(x)
>x : Symbol(x, Decl(parserForStatement9.ts, 2, 10))
>x : Symbol(x, Decl(parserForStatement9.ts, 2, 10))
>x : Symbol(x, Decl(parserForStatement9.ts, 2, 10))
>x : Symbol(x, Decl(parserForStatement9.ts, 2, 10))
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>x : Symbol(x, Decl(parserForStatement9.ts, 2, 10))

for (let {x = 'a' in {}} = {}; !x; x = !x) console.log(x)
>x : Symbol(x, Decl(parserForStatement9.ts, 3, 10))
>x : Symbol(x, Decl(parserForStatement9.ts, 3, 10))
>x : Symbol(x, Decl(parserForStatement9.ts, 3, 10))
>x : Symbol(x, Decl(parserForStatement9.ts, 3, 10))
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>x : Symbol(x, Decl(parserForStatement9.ts, 3, 10))

41 changes: 41 additions & 0 deletions tests/baselines/reference/parserForStatement9.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//// [tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement9.ts] ////

=== parserForStatement9.ts ===
// repro from https://github.com/microsoft/TypeScript/issues/54769

for (let [x = 'a' in {}] = []; !x; x = !x) console.log(x)
>x : boolean
>'a' in {} : boolean
>'a' : "a"
>{} : {}
>[] : []
>!x : boolean
>x : boolean
>x = !x : boolean
>x : boolean
>!x : boolean
>x : boolean
>console.log(x) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>x : boolean

for (let {x = 'a' in {}} = {}; !x; x = !x) console.log(x)
>x : boolean
>'a' in {} : boolean
>'a' : "a"
>{} : {}
>{} : { x?: boolean; }
>!x : boolean
>x : boolean
>x = !x : boolean
>x : boolean
>!x : boolean
>x : boolean
>console.log(x) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>x : boolean

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// repro from https://github.com/microsoft/TypeScript/issues/54769

for (let [x = 'a' in {}] in { '': 0 }) console.log(x)
for (let {x = 'a' in {}} in { '': 0 }) console.log(x)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// repro from https://github.com/microsoft/TypeScript/issues/54769

for (let [x = 'a' in {}] = []; !x; x = !x) console.log(x)
for (let {x = 'a' in {}} = {}; !x; x = !x) console.log(x)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @target: esnext

// repro from https://github.com/microsoft/TypeScript/issues/54769

for (let [x = 'a' in {}] of [[]]) console.log(x)
for (let {x = 'a' in {}} of [{}]) console.log(x)

0 comments on commit 2623fe7

Please sign in to comment.