Skip to content

Simplify the printing logic for ( before ambiguous tokens#16651

Merged
nicolo-ribaudo merged 1 commit intobabel:mainfrom
nicolo-ribaudo:improve-parens-logic
Jul 24, 2024
Merged

Simplify the printing logic for ( before ambiguous tokens#16651
nicolo-ribaudo merged 1 commit intobabel:mainfrom
nicolo-ribaudo:improve-parens-logic

Conversation

@nicolo-ribaudo
Copy link
Member

Q                       A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

See #16648. With this change we don't need to manually list cases where ( is needed anymore, and we do not need to walk up in the tree.

@nicolo-ribaudo nicolo-ribaudo added PR: Internal 🏠 A type of pull request used for our changelog categories pkg: generator labels Jul 16, 2024
@babel-bot
Copy link
Collaborator

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

Copy link
Member

@liuxingbaoyu liuxingbaoyu left a comment

Choose a reason for hiding this comment

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

Amazing!
I ran a benchmark and in Babel 7 it's just as fast as before.
And in Babel 8 we can enable babelPluginInlineConstNumericObjects for generator to make it even faster!

Comment on lines +535 to +538
// @ts-expect-error todo(flow->ts) can this be removed? `.optional` looks to be not existing property
node.optional
) {
this.token("?");

Choose a reason for hiding this comment

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

This looks like the case for:

const ast = Babel.packages.parser.parse("type x = ?    number; /**/", {plugins: ['flow']});
Babel.packages.generator.default(ast);
// Outputs: {code: 'type x = ?number; /**/', decodedMap: undefined}

(but I can't test it easily through debugging or stepping into the code, since the repl build is helplessly minified - would be nice if a PR preview was readable/debuggable)

Copy link
Member

Choose a reason for hiding this comment

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

https://astexplorer.net/#/gist/65e561e94ad90a154646cfd21be92f52/26ebbf2128f469b1469f574a3fb76b167b53f8e2
You can use it.
Also I remember that the repl has source maps, so debugging should be possible. :)

Choose a reason for hiding this comment

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

@liuxingbaoyu Thank you! Good point, it works very nicely. Somehow I went through babeljs.io in source tab and not output.circle-artifacts.com... now that I could set a breakpoint I realized my code snippet wasn't for that case (so the comment may be correct lol).

Only strange thing is it's called optional in different places too, even though it's just nullable (optional in JS means some type or undefined).

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the investigation, I'll re-check and see what we need to do here :)

@nicolo-ribaudo nicolo-ribaudo merged commit 78efc9b into babel:main Jul 24, 2024
@nicolo-ribaudo nicolo-ribaudo deleted the improve-parens-logic branch July 24, 2024 12:32
@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 Oct 24, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 24, 2024
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: generator 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.

5 participants

Comments