Repository navigation
Use an indirect call for wrapped imported callees - #65540
Closed
Josué Barros (joshbarros) wants to merge 2 commits into
Closed
Josué Barros (joshbarros) wants to merge 2 commits into
Josué Barros (joshbarros) wants to merge 2 commits into
Conversation
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.
Contributor
There was a problem hiding this comment.
🟡 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
satisfiescalls, 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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 asthisinstead ofundefined.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.