Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Babel 8]: Ensure parseExpression with tokens: true returns the exported tokens#17666

Merged
nicolo-ribaudo merged 2 commits intobabel:mainbabel/babel:mainfrom
JLHwung:babel-parser-cleanupJLHwung/babel:babel-parser-cleanupCopy head branch name to clipboard
Dec 19, 2025
Merged

[Babel 8]: Ensure parseExpression with tokens: true returns the exported tokens#17666
nicolo-ribaudo merged 2 commits intobabel:mainbabel/babel:mainfrom
JLHwung:babel-parser-cleanupJLHwung/babel:babel-parser-cleanupCopy head branch name to clipboard

Conversation

@JLHwung
Copy link
Copy Markdown
Contributor

@JLHwung JLHwung commented Dec 15, 2025

Q                       A
Fixed Issues? Cleanup Babel parser for Babel 8
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

The only breaking change introduced in this PR is that the parser.parseExpression(?, { tokens: true }) API now returns exported token rather than internal tokens. For example

parserExpression("42", { tokens: true }).tokens[0]
// Babel 7
{ type: <Number representing the internal token>, value: 42, ... }
// Babel 8
{ type: { label: "num", beforeExpr: false, ... }, value: 42, ... }

The Babel 8 behaviour aligns with the behaviour of the more popular API parse().

Since the number representing the internal token is never meant to be part of the return result -- The number might change in different Babel minor releases as we keep adding new tokens. I am open to backporting this change to Babel 7.

@JLHwung JLHwung added the PR: Breaking Change 💥 A type of pull request used for our changelog categories for next major release label Dec 15, 2025
@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Dec 15, 2025

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

expr.errors = this.state.errors;
if (this.optionFlags & OptionFlags.Tokens) {
expr.tokens = this.tokens;
expr.tokens = createExportedTokens(this.tokens);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This change stems from the improved ExportedTokens typing, which clearly indicates that previously we passed the internal tokens to the parser return result.

// TODO: Remove this in Babel 8
bracketBarR: createToken("|]"),
braceL: createToken("{", { beforeExpr, startsExpr }),
// TODO: Remove this in Babel 8
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Although there was a todo item here. It turns out the braceBarL token is also used in Flow object type annotation. Therefore the item is invalid and thus removed.

| VoidPattern
)[];
returnType: TsTypeAnnotation | undefined | null;
// TODO(Babel 8): Remove
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Although there was a todo item that parameters should be removed, it turns out the TSIndexSignature still uses parameters. Therefore we create a new TsIndexSignatureBase type hosting the parameters property.

Copy link
Copy Markdown
Member

@nicolo-ribaudo nicolo-ribaudo Dec 19, 2025

Choose a reason for hiding this comment

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

There is both params and parameters on TSIndexSignature?

Copy link
Copy Markdown
Contributor Author

@JLHwung JLHwung Dec 19, 2025

Choose a reason for hiding this comment

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

No. Here is the current situation:

Babel 7:

TSIndexSignature { parameters }
TSConstructSignatureDeclaration { parameters }
TSMethodSignature { parameters }
TSFunctionType { parameters }
TSConstructorType { parameters }

Babel 8:

TSIndexSignature { parameters }
TSConstructSignatureDeclaration { params }
TSMethodSignature { params }
TSFunctionType { params }
TSConstructorType { params }

The TSIndexSignature still holds parameters property, while other nodes hold params property. The other nodes inherit TsSignatureDeclarationBase type, therefore I moved params to TsSignatureDeclarationBase and created TsIndexSignatureBase for parameters.

The Babel 8 AST aligns to the current ts-eslint AST. This PR does not change the TSIndexSignature AST, but rather just fixes typing errors.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Dec 15, 2025

Open in StackBlitz

commit: 6317a8a

@JLHwung JLHwung changed the title [Babel 8]: Ensure parseExpression with tokens: true return the exported tokens [Babel 8]: Ensure parseExpression with tokens: true returns the exported tokens Dec 16, 2025
@nicolo-ribaudo nicolo-ribaudo merged commit 9cd86af into babel:main Dec 19, 2025
54 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the babel-parser-cleanup branch December 19, 2025 20:51
This was referenced Feb 5, 2026
This was referenced Feb 12, 2026
@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 Mar 21, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 21, 2026
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 PR: Breaking Change 💥 A type of pull request used for our changelog categories for next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.