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

refactor: add parse*Literal parser routines #13333

Merged
merged 8 commits into from May 19, 2021

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented May 18, 2021

Q                       A
Fixed Issues? Fixes #13329
Patch: Bug Fix? Y
Tests Added + Pass? Yes
License MIT

This PR should be reviewed by commits.

Currently we handle the StringLiteral -> Literal conversion in parseExprAtom, however, in the scenarios like module string names or import assertions, we can not call parseExprAtom since only a limited set of PrimaryExpression (namely StringLiteral and Identifier) are accepted in the grammar, thus the StringLiteral in such places is not converted to a Literal as expected from the estree plugin.

Thus the goal of this PR is to break the conversion logic of parseExprAtom to more granular parseSomeLiteral, so whenever a string literal is created from parseStringLiteral, the estree plugin can handle it via subclassed parseStringLiteral method.

This PR also simplifies the interface of parseLiteral: It turns out the optional position input is only used by Flow plugin when it parses -1 as a dedicated NumberTypeAnnotation, thus we can remove the branch logic of parseLiteral.

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: parser labels May 18, 2021
@babel-bot
Copy link
Collaborator

babel-bot commented May 18, 2021

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 18, 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 4205780:

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

"type": "File",
"start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}},
"errors": [
"SyntaxError: 'import' and 'export' may appear only with 'sourceType: \"module\"' (1:0)"
Copy link
Member

Choose a reason for hiding this comment

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

Are we testing this error here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, I forgot to add sourceType module to options.json😬

packages/babel-parser/src/plugins/flow/index.js Outdated Show resolved Hide resolved
JLHwung and others added 2 commits May 19, 2021 09:15
Co-authored-by: Federico Ciardi <fed.ciardi@gmail.com>
@JLHwung JLHwung force-pushed the fix-estree-parser-literal branch from 00dbbfe to 7214721 Compare May 19, 2021 13:15
return this.finishNode<T>(node, type);
}

parseLiteral<T: N.Literal>(value: any, type: /*T["kind"]*/ string): T {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe one of these works:

Suggested change
parseLiteral<T: N.Literal>(value: any, type: /*T["kind"]*/ string): T {
parseLiteral<T: N.Literal>(value: any, type: $PropertyType<T, "kind">): T {
Suggested change
parseLiteral<T: N.Literal>(value: any, type: /*T["kind"]*/ string): T {
parseLiteral<T: N.Literal>(value: any, type: $ElementType<T, "kind">): T {

@JLHwung JLHwung merged commit 461ba25 into babel:main May 19, 2021
@JLHwung JLHwung deleted the fix-estree-parser-literal branch May 19, 2021 20:00
@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 Aug 19, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
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.

[Bug]: Unexpected StringLiteral node in @babel/eslint-parser
4 participants