Skip to content

Use an indirect call for wrapped imported callees - #65540

Closed
Josué Barros (joshbarros) wants to merge 2 commits into
microsoft:mainfrom
joshbarros:fix/65414-commonjs-wrapped-callee-this
Closed

Josué Barros (joshbarros) wants to merge 2 commits into
microsoft:mainfrom
joshbarros:fix/65414-commonjs-wrapped-callee-this

Conversation

@joshbarros

Copy link
Copy Markdown

Fixes #65414

The CommonJS transform only checked for a direct identifier callee or tag. A parenthesized, asserted, non-null or satisfies-wrapped imported function was visited without EFIndirectCall, so it was emitted as a method call and received the exports object as this instead of undefined.

This skips outer expressions before the identifier check so the wrapped forms also emit (0, mod.f)(...), matching the bare call.

The new compiler test covers f(), (f)(), (f as F)(), (f!)(), (f satisfies F)() and (f)`x`. On the unfixed compiler only the bare call is indirect. The full compiler suite passes with the change.

Validation:

  • go test ./tsc/internal/testrunner -run TestLocal/commonJsIndirectCallWrappedCallee -count=1: fails without the change, passes with it.
  • go test ./tsc/internal/testrunner -run TestLocal -count=1: pass.
  • gofmt: clean.

Drafted with assistance from an AI assistant. I reviewed the diff and ran the tests above.

The CommonJS transform only checked for a direct identifier callee or tag. A parenthesized, asserted, non-null or satisfies-wrapped imported function was visited without EFIndirectCall, so it was emitted as a method call and received the exports object as this instead of undefined.

Skip outer expressions before the identifier check so the wrapped forms also emit (0, mod.f)(...), matching the bare call. Adds a compiler test for the six forms from the report; the full compiler suite passes.
Copilot AI balanced review requested due to automatic review settings November 16, 2026 19:17
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 16, 2026
@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Nov 16, 2026

Copilot AI 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.

🟡 Changes recommended

Callee substitution drops wrapper-owned comments and source-map ranges in both calls and tagged templates, requiring restoration of the original outer expressions.

1 open issue
What changed in this PR

This PR addresses #65414 in the native TypeScript compiler’s CommonJS transform. Wrapped imported functions now receive indirect-call treatment, preserving the intended this value.

Changes:

  • Detect identifiers inside wrapper expressions for calls and tagged templates.
  • Add regression coverage for bare, parenthesized, asserted, non-null, and satisfies calls, plus a parenthesized tag.
  • Record JavaScript, type, and symbol baselines.
File Description
tsc/​testdata/​tests/​cases/​compiler/​commonJsIndirectCallWrappedCallee.ts Adds regression cases for wrapped imported callees.
tsc/​testdata/​baselines/​reference/​compiler/​commonJsIndirectCallWrappedCallee.types Records expected expression types.
tsc/​testdata/​baselines/​reference/​compiler/​commonJsIndirectCallWrappedCallee.symbols Records expected symbol bindings.
tsc/​testdata/​baselines/​reference/​compiler/​commonJsIndirectCallWrappedCallee.js Records expected indirect-call output.
tsc/​internal/​transformers/​moduletransforms/​commonjsmodule.go Recognizes wrapped identifiers when deciding indirect-call emission.

Review effort: Balanced

Restore the original outer expressions after substituting the identifier so wrapper-owned comments and source-map ranges are preserved. The indirect-call check still runs on the inner identifier.
@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

CommonJS emit gives an imported function the wrong this when the callee is wrapped

3 participants