Skip to content

Valid JavaScript with in inside for produces syntax errors and invalid JavaScript output #54769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
evanw opened this issue Jun 25, 2023 · 2 comments Β· Fixed by #54801
Closed

Valid JavaScript with in inside for produces syntax errors and invalid JavaScript output #54769

evanw opened this issue Jun 25, 2023 · 2 comments Β· Fixed by #54801
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@evanw
Copy link
Contributor

evanw commented Jun 25, 2023

Bug Report

πŸ”Ž Search Terms

for in syntax error

πŸ•— Version & Regression Information

  • This is the behavior in every version available in the playground

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

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

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

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

πŸ™ Actual behavior

When run, this code should print false six times. Each false is the value of x which has been default-initialized.

πŸ™‚ Expected behavior

TypeScript incorrectly generates syntax errors and generates the following invalid JavaScript output:

"use strict";
for (let [x = 'a'] in {})
    ;
of[[]];
console.log(x);
for (let { x = 'a', in: {} } of [{}])
    console.log(x);
for (let [x = 'a'] in {})
    ;
 in { '': 0 };
console.log(x);
for (let { x = 'a', in: {} } in { '': 0 })
    console.log(x);
for (let [x = 'a'] in {})
    ;
[];
!x;
x = !x;
console.log(x);
for (let { x = 'a', in: {} } = {}; !x; x = !x)
    console.log(x);

This came up because I was using VSCode to look at some JavaScript language tests and it was incorrectly showing syntax errors. I'm reporting this because it doesn't seem ideal that VSCode's JavaScript language support would treat valid JavaScript as invalid, and also because I believe the fix for this is relatively straightforward (probably something to do with DisallowInContext in the parser?).

@MartinJohns
Copy link
Contributor

MartinJohns commented Jun 25, 2023

Sounds like a duplicate of #9978. Used search terms: in for in:title label:"Won't Fix"

Similar issues occasionally pop up and get closed as Won't Fix. There's really no point in spending time to make something work that's realistically never used.

@evanw
Copy link
Contributor Author

evanw commented Jun 25, 2023

I believe this issue is different because a) TypeScript is incorrectly generating syntax errors in valid JavaScript files in this case but not in #9978 and b) TypeScript generates garbled JavaScript output in this case but not in #9978. I think it's also worth fixing because the fix is pretty trivial:

diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts
index f49b48bb44..c70bf754d1 100644
--- a/src/compiler/parser.ts
+++ b/src/compiler/parser.ts
@@ -7411,7 +7411,7 @@ 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);
     }
@@ -7419,7 +7419,7 @@ namespace Parser {
     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);
     }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants