Skip to content

Fixed false positive syntax errors with in inside for #54801

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

Merged
merged 1 commit into from
Jul 20, 2023

Conversation

Andarist
Copy link
Contributor

fixes #54769

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Jun 27, 2023
Comment on lines +2 to +3
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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those errors are not correct but that's a separate issue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How challenging is that to fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm looking into this but it's the first time that I even opened the files related to downleveling syntax here so it might take a moment (if I don't lose interest in this, that is).

  1. the error has to be likely removed completely (I removed it from one location but there is another one and I didn't even look into it yet)
  2. this undefined shouldn't be emitted (at least in this case)
  3. and the declaration list can't be "replaced" in place, since downleveling binding patterns requires creating temp bindings and for-in only supports a single binding (Uncaught SyntaxError: Invalid left-hand side in for-in loop: Must have a single binding.)
  4. to fix 2 and 3 it's likely the easiest to move the binding "expansion" to the loop's body, this is what Babel does:
for (var _ref3 in {
  '': 0
}) {
  var _ref2 = _toArray(_ref3);
  var _ref2$ = _ref2[0];
  var x = _ref2$ === void 0 ? 'a' in {} : _ref2$;
  var kaka = _ref2[1];
  var rest = _ref2.slice(2);
  console.log(x);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s legal to use destructuring in for..in? What would that even mean since the value is always a string (or symbol)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strings are iterable so u could read its characters through an array pattern, or some prototype methods/properties through an object pattern.

Is it useful? Not rly. Is it legal? Apparently yes :P

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just close the loop on this one, if you don't think you'll get around to fixing this in this PR, can you add a note to the test case that says "hey, this is wrong" and file an issue, or similar?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er, actually, is #54853 that PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is.

Gif saying 'You taught me that, Saul. No loose ends'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I guess we can just leave the PR as-is?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, i think so.

Comment on lines +12 to +15
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't the correct output but since binding patterns on the left side of for-in are not supported by TS today, I think that it's fine to leave it as is for now

@sandersn sandersn self-requested a review July 19, 2023 16:51
Comment on lines +2 to +3
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just close the loop on this one, if you don't think you'll get around to fixing this in this PR, can you add a note to the test case that says "hey, this is wrong" and file an issue, or similar?

@jakebailey jakebailey merged commit 2623fe7 into microsoft:main Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Valid JavaScript with in inside for produces syntax errors and invalid JavaScript output
5 participants