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

Simplifiy tracking of valid JSX positions #13891

Merged
merged 6 commits into from Nov 4, 2021

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Oct 28, 2021

Q                       A
Tests Added + Pass? Yes
License MIT

This PR can be reviewed by commits.

The first commit removes state.inPropertyName. The intend of inPropertyName is to disallow JSX tag forming after a property name, which would have been a method with type parameters. We already have state.exprAllowed for this purpose so we can set state.exprAllowed after the property name is consumed.

The second commit renames exprAllowed to canStartJSXElement for clarity.

The third commit handles the canStartJSXElement in parseIdentifierName: when we see a liberal keyword, we mark the token type as tt.name but did not update the canStartJSXElement.

In the fourth commit we add a new replaceToken method which only updates current token and token context, so we don't have to care canStartJSXElement in the base parser, they will be handled by the JSX plugin

Because parseIdentifierName is a popular path, the last commit ensures that we call replaceToken only when we have to do so.

@JLHwung JLHwung added PR: Internal 🏠 A type of pull request used for our changelog categories pkg: parser labels Oct 28, 2021
@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 28, 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 5267395:

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

@babel-bot
Copy link
Collaborator

babel-bot commented Oct 28, 2021

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

nicolo-ribaudo
nicolo-ribaudo previously approved these changes Nov 1, 2021
this.state.type = type;
// the prevType of updateContext is required
// only when the new type is tt.slash/tt.jsxTagEnd
// $FlowIgnore
Copy link
Member

Choose a reason for hiding this comment

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

We might consider marking it as optional?

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 am working on completely removing updateContext() in base parser and jsx parser, at least currently we don't need updateContext in the base parser so hopefully replaceToken can be moved to the jsx plugin.

@nicolo-ribaudo nicolo-ribaudo dismissed their stale review November 1, 2021 21:10

I'm dropping my approval not because I have some questions

if (liberal) {
// If the current token is not used as a keyword, set its type to "tt.name".
// This will prevent this.next() from throwing about unexpected escapes.
this.state.type = tt.name;
if (tokenIsKeyword) {
this.replaceToken(tt.name);
Copy link
Member

Choose a reason for hiding this comment

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

Is updateContext needed when replacing a keyword with tt.name? (I don't think it is).
Because we could just to this.state.type = tt.name here (and maybe this.state.exprAllowed = false, even if I don't see why it would be needed) and keep the prevType handling all in the JSX plugin (to avoid needing the type error suppression comment).

Copy link
Member

Choose a reason for hiding this comment

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

Uh, or maybe it would break this:

foo.else
<X>2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The updateContext is currently required by the jsx plugin, to disable JSX element forming in the following situations, where a keyword token was re-interpreted as an identifier token and thus it can not start a JSX element:

x.case <jsx ...
x?.case <jsx ...
class C { case <jsx ...

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Approving since updateContext will be removed anyway - #13891 (comment)

@nicolo-ribaudo
Copy link
Member

The CI failure is fixed in #13923

@nicolo-ribaudo nicolo-ribaudo changed the title Remove state.inPropertyName and simplifies state.canStartJSXElement tracking Simplifiy tracking of valid JSX positions Nov 4, 2021
@nicolo-ribaudo nicolo-ribaudo merged commit 7250d25 into babel:main Nov 4, 2021
@nicolo-ribaudo nicolo-ribaudo deleted the parser-state-cleanup branch November 4, 2021 16:12
@nicolo-ribaudo
Copy link
Member

I rewrote the commit title to fit the max length; the original title is in the commit description.

@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 Feb 4, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 4, 2022
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: Internal 🏠 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants