Repository navigation
fix(parser): allow in in array literals in a for statement initializer - #65330
Jemmy (JemmyIndex) wants to merge 1 commit into
Conversation
The [~In] of a for statement head does not reach into array literals (ElementList : AssignmentExpression[+In]), but parseArrayLiteralExpression parsed its elements in the caller's context, so the DisallowInContext flag leaked into the element list. Clear it the same way parseArgumentExpression and object literal property values already do.
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the parser change allowing in inside array literals in for initializers.
No open issues 🎉
What changed in this PR
Fixes #65311 by allowing the in operator inside array literals in for initializers, aligning the native TypeScript parser with JavaScript grammar.
Changes:
- Clears
DisallowInContextwhile parsing array elements, then restores the surrounding context. - Adds regression coverage for array expressions,
for-ofdestructuring defaults, and ordinaryfor-inparsing. - Adds expected JavaScript, type, and symbol baselines.
| File | Description |
|---|---|
tsc/testdata/tests/cases/compiler/arrayLiteralInOperatorInForInitializer.ts |
Adds regression cases for in in loop-head arrays. |
tsc/testdata/baselines/reference/compiler/arrayLiteralInOperatorInForInitializer.types |
Records expected expression types. |
tsc/testdata/baselines/reference/compiler/arrayLiteralInOperatorInForInitializer.symbols |
Records expected symbol bindings. |
tsc/testdata/baselines/reference/compiler/arrayLiteralInOperatorInForInitializer.js |
Records expected JavaScript output. |
tsc/internal/parser/parser.go |
Allows in while parsing array literal elements. |
Review effort: Balanced
|
TypeScript Bot (@typescript-bot) test it |
|
Hey Ryan Cavanaugh (@RyanCavanaugh), the results of running the DT tests are ready. Everything looks the same! |
|
Ryan Cavanaugh (@RyanCavanaugh) Here are the results of running the user tests with tsc comparing Everything looks good! |
|
Ryan Cavanaugh (@RyanCavanaugh) Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Ryan Cavanaugh (@RyanCavanaugh) Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
Fixes #65311.
The
[~In]parameter of aforstatement head does not reach into array literals —ElementList : AssignmentExpression[+In]— butparseArrayLiteralExpressionparsed its elements in the caller's context, so theDisallowInContextflag set for aforinitializer leaked into the element list:The neighbouring productions already clear the flag — object literal property values (
parseObjectLiteralExpression→doInContext(DisallowInContext, false, …)) and call arguments (parseArgumentExpression) — so this does the same for array literal elements. That also fixes the destructuring-assignment form in afor-ofhead, which test262 covers inlanguage/statements/for-of/dstr/array-elem-init-in.js.Only the tokens between
[and]are affected: the flag is restored once the]is consumed, sofor (x in y)andfor ([a, b] in y)parse exactly as before (and still report TS2491), and emit is unchanged.Added
tests/cases/compiler/arrayLiteralInOperatorInForInitializer.ts, covering a plainforinitializer, comma and nested elements, an array pattern in afor-ofhead, and the unchangedfor-inbehavior.Verification:
go test ./internal/parser/...and the fullgo test ./internal/testrunner(compiler + conformance baselines) pass.AI assistance
This PR was written with AI assistance, and the change is deliberately scoped to clearing the
DisallowInContextflag while parsing array literal elements. I verified the behavior manually: both repros from the issue parse cleanly,for (x in y)andfor ([a, b] in y)still report TS2491, and emit is unchanged.go test ./internal/parser/...and the fullgo test ./internal/testrunnerpass locally. I understand the implementation and am happy to answer questions about it.