Skip to content

[ts] Check if param is assignable when parsing arrow return type#13581

Merged
nicolo-ribaudo merged 3 commits intobabel:mainfrom
nicolo-ribaudo:bug-11038
Jul 26, 2021
Merged

[ts] Check if param is assignable when parsing arrow return type#13581
nicolo-ribaudo merged 3 commits intobabel:mainfrom
nicolo-ribaudo:bug-11038

Conversation

@nicolo-ribaudo
Copy link
Copy Markdown
Member

@nicolo-ribaudo nicolo-ribaudo commented Jul 20, 2021

@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: parser area: typescript labels Jul 20, 2021
return node.properties.every((prop, i) => {
return (
prop.type !== "ObjectMethod" &&
(i === last || prop.type !== "SpreadElement") &&
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The bug that caused the regression was that this was using === "SpreadElement" instead of !==.

This comment was marked as resolved.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why use 'every' ? It's slow vs a native for-loop

@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Jul 20, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/47484/

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci bot commented Jul 20, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 24c017a:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@nicolo-ribaudo
Copy link
Copy Markdown
Member Author

The failure is a regression.


NOTE: There is a corresponding "isAssignable" method in flow.js.
NOTE: There is a corresponding "isAssignable" method.
When this one is updated, please check if also that one needs to be updated.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This note can be removed then since it is moved to base parser.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I actually like this comment: even if they are in the same file, it's still easy to miss isAssignable when updating toAssignable.

case "ObjectPattern":
case "ArrayPattern":
case "AssignmentPattern":
case "RestElement":
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@nicolo-ribaudo What about a lookup table? This switch is slow because of 1) string comparison 2) many cases 3) hard to optimize in v8

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think this switch relies on string comparisons: these strings are always constants in our code so V8 will atomize them and just compare them as pointers (but @JLHwung knows more than me about this)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

v8 will convert this switch to a lookup, so it's faster if you do it.

@nicolo-ribaudo nicolo-ribaudo merged commit 4a56387 into babel:main Jul 26, 2021
@nicolo-ribaudo nicolo-ribaudo deleted the bug-11038 branch July 26, 2021 15:58
@KFlash
Copy link
Copy Markdown

KFlash commented Jul 26, 2021

Merged, but performance not improved. What's the reason?

@nicolo-ribaudo
Copy link
Copy Markdown
Member Author

That change doesn't have an observable impact on the normal @babel/parser usage.

@cederom
Copy link
Copy Markdown

cederom commented Aug 23, 2021

Thank you guys :-) Then this release will land into npm? 7.15.0 is still the latest one over there :-)

@JLHwung
Copy link
Copy Markdown
Contributor

JLHwung commented Aug 23, 2021

It is fixed in @babel/parser 7.15.3, @babel/parser is a dependency of @babel/core.

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Nov 23, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: typescript outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

typescript preset: incompatible with += operator inside arrow functions, nested in ternary

6 participants