Skip to content

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

Closed
@evanw

Description

@evanw

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?).

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions