Skip to content

fix(class-static-block): skip class body path with stale ancestry - #18563

Merged
JLHwung merged 1 commit into
babel:mainfrom
JLHwung:fix-arrow-this-before-super-static-block
Nov 11, 2026
Merged

JLHwung merged 1 commit into
babel:mainfrom
JLHwung:fix-arrow-this-before-super-static-block

Conversation

@JLHwung

@JLHwung JLHwung commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? Fixes #18558
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
Need backport? 7.x
License MIT

This bug is very interesting. What's happening here is that when static blocks are run through _staticBlock() after a class expression, the ClassBody visitor wraps the class in a sequence expression. Then the replaceWith re-points the class path to the wrapped sequence expression, so other plugins visiting the class members in the same traversal saw no class above the constructor: the arrow functions lowering (used by async-to-generator) then treated a derived constructor as a base one and read this before super().

So in this PR we provide a hotfix for this case: we skip the class body path with stale ancestry given that it will be revisited later in the sequence expression with correct ancestry. This bug is an example of why we should avoid replacing parent path in the visitors.

I am still exploring if we can fix this issue on the replaceWith side: That it will check if one of the children becomes stale and it is pushed to a new location then we can automatically remove it from the current traverse queue. This approach does not require an explicit path.skip() in such cases but do incurs ~2% performance impact. Anyway such change will be a bigger PR so I don't want to hold off the fix for a more general one.

Assisted-by: Claude Opus 5.5

When static blocks are run through `_staticBlock()` after a class
expression, the ClassBody visitor wraps the class in a sequence
expression. `replaceWith` re-points the class path to the sequence, so
other plugins visiting the class members in the same traversal saw no
class above the constructor: the arrow functions lowering (used by
async-to-generator) then treated a derived constructor as a base one and
read `this` before `super()`.

Skip the class body: the requeued sequence expression visits it again
with the right ancestry.

Fixes babel#18558

Assisted-by: Claude Opus 5.5
@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Class Static Block 7.x: needs backport labels Nov 10, 2026
@babel-bot

Copy link
Copy Markdown
Collaborator

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

@JLHwung JLHwung changed the title fix(class-static-block): skip class body path with stale parents fix(class-static-block): skip class body path with stale ancestry Nov 10, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 10, 2026

Copy link
Copy Markdown

Open in StackBlitz

commit: e24d089

@JLHwung
JLHwung added this pull request to the merge queue Nov 11, 2026
Merged via the queue into babel:main with commit e8535f5 Nov 11, 2026
67 checks passed
@JLHwung
JLHwung deleted the fix-arrow-this-before-super-static-block branch November 11, 2026 02:52
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18579.

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

Labels

PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Class Static Block

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: async-to-generator and class-static-block transforms access this before super()

3 participants