Skip to content

fix(parser): handle module as an identifier after class extends - #18541

Merged
JLHwung merged 2 commits into
babel:mainfrom
JLHwung:fix-parser-class-heritage-module-identifier
Nov 16, 2026
Merged

JLHwung merged 2 commits into
babel:mainfrom
JLHwung:fix-parser-class-heritage-module-identifier

Conversation

@JLHwung

@JLHwung JLHwung commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? Babel parser throws an error "Support for the experimental syntax 'moduleBlocks' isn't currently enabled" on valid ES class C extends module {} (REPL)
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
Need backport? No
License MIT

In this PR we fix a parser regression introduced in the implementation of module expression (previously module blocks proposal) (#12469): The current implementation interpreted module after class extends as a module expression and this renders code examples like class C extends module {} invalid.

The following valid cases are affected as well:

class A extends new module {}
class B extends new new module {}
class C extends module {}["foo"]
{}
class D extends module {}("foo")
{}
class E extends module {}`foo`
{}
class F extends new module {}("foo")
{}

They are all valid JS today, but Babel parser throws on all cases, expecting moduleBlocks plugin. With the moduleBlocks plugin, the first two cases are still failing and the other 4 cases are parsed as class declarations, different AST than they should have been parsed today: a list of (ClassDeclaration, ExpressionStatement, and BlockStatement).

So in this PR we hardened the parser and required that ModuleExpression within class heritage should be parenthesized if they starts from the class heritage, skipping optional new chains. I have also open an issue to the proposal repo: tc39/proposal-module-expressions#83.

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: parser Spec: Module Expressions 7.x: needs backport labels Nov 6, 2026
@babel-bot

babel-bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: 0781210

`module {` was always taken as the start of a module expression, so
`class C extends module {}` failed with or without the `moduleBlocks` plugin.

In a class heritage, optionally after `new`, `module {` is now an identifier
followed by the class body. `class A extends module {} {}` is ambiguous with
the plugin enabled, since a block statement can follow the class, and a module
is never a valid superclass anyway. Module expressions are still available
there in parentheses, so the generator now prints those parentheses.

Assisted-by: Claude Opus 5.5
@JLHwung
JLHwung force-pushed the fix-parser-class-heritage-module-identifier branch from 03ff450 to 0781210 Compare November 16, 2026 12:51
@JLHwung
JLHwung added this pull request to the merge queue Nov 16, 2026
Merged via the queue into babel:main with commit 9ab80fe Nov 16, 2026
40 checks passed
@JLHwung
JLHwung deleted the fix-parser-class-heritage-module-identifier branch November 16, 2026 16:17
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18635.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Module Expressions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants