Skip to content

Keep the receiver of TS-wrapped super and private member calls - #18576

Merged
nicolo-ribaudo merged 1 commit into
babel:mainfrom
brunoborta:fix/ts-wrapped-member-call
Nov 11, 2026
Merged

nicolo-ribaudo merged 1 commit into
babel:mainfrom
brunoborta:fix/ts-wrapped-member-call

Conversation

@brunoborta

Copy link
Copy Markdown
Contributor

Skip transparent expression wrappers (type assertions, non-null assertions, parentheses) between a member and its call, so that (super.m as any)(), super.m!() and this.#m!() are lowered as calls with the correct this instead of plain reads.

Fixes #18347

Assisted-by: Claude Opus 5.5

Q A
Fixed Issues? Fixes #18347
Patch: Bug Fix? Yes
Major: Breaking Change? No
Minor: New Feature? No
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes? Yes, @babel/helper-member-expression-to-functions now depends on @babel/helper-skip-transparent-expression-wrappers
Need backport? No
License MIT

The problem

When a super method call or a private method call has a TypeScript wrapper around the member, the method is called without the correct this:

const obj = {
  __proto__: { m() { return this; } },
  run() { return (super.m as any)(); },
};
obj.run() === obj; // false, should be true

The same happens with super.m!(), (super.m satisfies Function)() and this.#m!().

The cause

@babel/helper-member-expression-to-functions decides whether the member is being called by looking only at its direct parent. In (super.m as any)(), the parent is the TSAsExpression, not the CallExpression. The helper treats the access as a plain read and generates _superPropGet(...)(), losing the receiver.

The fix

Before checking if the member is the callee of a CallExpression or OptionalCallExpression, the helper now skips the transparent wrappers (as, !, satisfies, <T>, parentheses and Flow's type cast), using isTransparentExprWrapper from @babel/helper-skip-transparent-expression-wrappers. This way, (super.m as any)() generates the same output as super.m().

Since the helper is shared, the fix applies to super (transform-object-super and transform-classes) as well as private members (transform-private-methods).

Tests

I added transparent-expr-wrappers fixtures to transform-object-super, transform-classes and transform-private-methods, following the same pattern as the ones in transform-exponentiation-operator. The exec tests fail without the fix.

Out of scope

zloirock pointed out in the issue that writes through wrappers (this.#x! = 1, super.x! += 1, ++this.#x!, destructuring) also break. These go through different code paths in the helper and the fix is bigger, so I'd rather handle them in a separate PR. I also didn't touch the case of an optional chain inside a wrapper ((a?.#b as any)()).

Skip transparent expression wrappers (type assertions, non-null
assertions, parentheses) between a member and its call, so that
`(super.m as any)()`, `super.m!()` and `this.#m!()` are lowered as
calls with the correct `this` instead of plain reads.

Fixes babel#18347

Assisted-by: Claude Opus 5.5
@babel-bot

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 10, 2026

Copy link
Copy Markdown

Open in StackBlitz

commit: 15a91a2

@JLHwung JLHwung left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks.

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories 7.x: needs backport labels Nov 10, 2026

@nicolo-ribaudo nicolo-ribaudo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@nicolo-ribaudo
nicolo-ribaudo added this pull request to the merge queue Nov 11, 2026
Merged via the queue into babel:main with commit 41d8278 Nov 11, 2026
60 checks passed
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18595.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeScript assertion around a super method call loses its receiver

4 participants