Skip to content

Conversation

@JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Aug 25, 2025

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

Handle TS declaration parsing in parseStatementContent such that we can align to the tsc behaviour when parsing type as = or module as {}. In both cases tsc treats as as an identifier name, but previously Babel parser treated it as rational operator because the ts-only declaration parsing was delayed after a JS expression is formed.

This PR also fixes a similar case: module as {} should be interpreted as a TSModuleDeclaration rather than a TSAsExpression.

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: parser area: typescript labels Aug 25, 2025
@babel-bot
Copy link
Collaborator

babel-bot commented Aug 25, 2025

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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Aug 25, 2025

Open in StackBlitz

commit: 94f107d

}
case "global":
// `global { }` (with no `declare`) may appear inside an ambient module declaration.
// Would like to use tsParseAmbientExternalModuleDeclaration here, but already ran past "global".
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This todo item is also resolved now that the global module parsing has been moved to parseStatementContent when global is not yet consumed.

if (this.isContextual(tt._interface)) {
const result = this.tsParseInterfaceDeclaration(this.startNode());
if (result) return result;
if (!this.state.containsEsc) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This check is redundant for tt._const, however it is more convenient to group tt._const handling with other keyword-likes.

case tt._abstract:
case tt._declare: {
if (
this.nextTokenIsIdentifierAndNotTSRelationalOperatorOnSameLine()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a finite number of keyword tokens allowed to follow abstract / declare. In reality most input source is valid, to simplify the lookahead logic and exit early for valid code, we check all identifiers except in, instanceof, as and satisifies: If a declaration can not be parsed, it must be an invalid production in both JS and TS.

const node = this.startNode<N.TsTypeAliasDeclaration>();
this.next(); // eat 'type'
const result = this.tsParseTypeAliasDeclaration(node);
if (result) return result;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This check is redundant, tsParseTypeAliasDeclaration always returns a node.

Handle TS declaration parsing in parseStatementContent such that we can align to the tsc behaviour when parsing `type as =` or `module as {}`. In both cases tsc treats `as` as an identifier name, but previously Babel parser treated it as rational operator because the ts-only declaration parsing was delayed after a JS expression is formed.
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.

Great!

@JLHwung JLHwung merged commit 6378bc0 into babel:main Sep 7, 2025
131 checks passed
@JLHwung JLHwung deleted the fix-16048 branch September 7, 2025 13:05
@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 Dec 8, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 8, 2025
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.

[Bug]: Fails to parse type declarations like type satisfies = 0;

4 participants