Skip to content
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

Throw errors for invalid left-hand side assignment #10416

Merged
merged 5 commits into from
Mar 29, 2021

Conversation

fisker
Copy link
Member

@fisker fisker commented Feb 27, 2021

Description

Fixes #10412

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@thorn0
Copy link
Member

thorn0 commented Feb 27, 2021

This is valid TypeScript. Looks like a bug in Babel.

Prettier pr-10416
Playground link

--parser babel-ts

Input:

(a as b) = c;

Output:

SyntaxError: Invalid parenthesized assignment pattern (1:1)
> 1 | (a as b) = c;
    | ^

4 |
> 5 | (this.configuration as any) = (this.editor as any) = (this
| ^
6 | .editorBody as any) = undefined;
Copy link
Member

Choose a reason for hiding this comment

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

This is not right.

Copy link
Member

Choose a reason for hiding this comment

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

Weird. Babel supports this since long ago: babel/babel#7098
Why do we get an error?

Copy link
Member

Choose a reason for hiding this comment

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

Turns out this is because of createParenthesizedExpressions: true.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Disabling createParenthesizedExpressions for babel-ts is a solution.

@thorn0
Copy link
Member

thorn0 commented Mar 29, 2021

Oops. This shouldn't parse:

Prettier pr-10416
Playground link

--parser babel-ts

Input:

a as any = x;

Output:

(a as any) = x;

@thorn0
Copy link
Member

thorn0 commented Mar 29, 2021

This should parse, but Prettier breaks it (AST isn't aligned with typescript-estree):

Prettier pr-10416
Playground link

--parser babel-ts

Input:

[a as any] = x;

Output:

[a: any] = x;

@thorn0
Copy link
Member

thorn0 commented Mar 29, 2021

Shouldn't parse.

Prettier pr-10416
Playground link

--parser babel-ts

Input:

({a: b as any = 2} = x);

Output:

({ a: b as any = 2 } = x);

Copy link
Member

@thorn0 thorn0 left a comment

Choose a reason for hiding this comment

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

The things I found are all separate issues in Babel or Prettier. This PR is good.

I opened babel/babel#13070 and #10635.

@thorn0 thorn0 merged commit 24d39a9 into prettier:main Mar 29, 2021
@fisker fisker deleted the invalid-lhs branch March 30, 2021 00:22
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Babel turn invalid code to valid code
3 participants