From a98b869003765a895fe1eea61267c73aadcaa19e Mon Sep 17 00:00:00 2001 From: Josue Barros Date: Mon, 16 Nov 2026 16:13:17 -0300 Subject: [PATCH 1/2] Use an indirect call for wrapped imported callees 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. --- .../moduletransforms/commonjsmodule.go | 13 +-- .../commonJsIndirectCallWrappedCallee.js | 31 +++++++ .../commonJsIndirectCallWrappedCallee.symbols | 60 +++++++++++++ .../commonJsIndirectCallWrappedCallee.types | 85 +++++++++++++++++++ .../commonJsIndirectCallWrappedCallee.ts | 15 ++++ 5 files changed, 198 insertions(+), 6 deletions(-) create mode 100644 tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.js create mode 100644 tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.types create mode 100644 tsc/testdata/tests/cases/compiler/commonJsIndirectCallWrappedCallee.ts diff --git a/tsc/internal/transformers/moduletransforms/commonjsmodule.go b/tsc/internal/transformers/moduletransforms/commonjsmodule.go index 8def010365911..89317b78d46e3 100644 --- a/tsc/internal/transformers/moduletransforms/commonjsmodule.go +++ b/tsc/internal/transformers/moduletransforms/commonjsmodule.go @@ -1829,16 +1829,17 @@ func (tx *CommonJSModuleTransformer) visitCallExpression(node *ast.CallExpressio if needsRewrite { return tx.shimOrRewriteImportOrRequireCall(node.AsCallExpression()) } - if ast.IsIdentifier(node.Expression) { + if callee := ast.SkipOuterExpressions(node.Expression, ast.OEKAll); ast.IsIdentifier(callee) { // given: // import { f } from "mod"; // f(); + // (f as F)(); // emits: // const mod_1 = require("mod"); // (0, mod_1.f)(); // note: // the indirect call is applied by the printer by way of the `EFIndirectCall` emit flag. - expression := tx.visitExpressionIdentifier(node.Expression) + expression := tx.visitExpressionIdentifier(callee) updated := tx.Factory().UpdateCallExpression( node, expression, @@ -1847,7 +1848,7 @@ func (tx *CommonJSModuleTransformer) visitCallExpression(node *ast.CallExpressio tx.Visitor().VisitNodes(node.Arguments), node.Flags, ) - if !ast.IsIdentifier(expression) && !transformers.IsHelperName(tx.EmitContext(), node.Expression) { + if !ast.IsIdentifier(expression) && !transformers.IsHelperName(tx.EmitContext(), callee) { tx.EmitContext().AddEmitFlags(updated, printer.EFIndirectCall) } return updated @@ -2011,7 +2012,7 @@ func (tx *CommonJSModuleTransformer) shimOrRewriteImportOrRequireCall(node *ast. // Visits a tagged template expression that might reference an imported symbol and thus require an indirect call. func (tx *CommonJSModuleTransformer) visitTaggedTemplateExpression(node *ast.TaggedTemplateExpression) *ast.Node { - if ast.IsIdentifier(node.Tag) { + if tag := ast.SkipOuterExpressions(node.Tag, ast.OEKAll); ast.IsIdentifier(tag) { // given: // import { f } from "mod"; // f``; @@ -2021,7 +2022,7 @@ func (tx *CommonJSModuleTransformer) visitTaggedTemplateExpression(node *ast.Tag // note: // the indirect call is applied by the printer by way of the `EFIndirectCall` emit flag. - expression := tx.visitExpressionIdentifier(node.Tag) + expression := tx.visitExpressionIdentifier(tag) updated := tx.Factory().UpdateTaggedTemplateExpression( node, expression, @@ -2030,7 +2031,7 @@ func (tx *CommonJSModuleTransformer) visitTaggedTemplateExpression(node *ast.Tag tx.Visitor().VisitNode(node.Template), node.Flags, ) - if !ast.IsIdentifier(expression) && !transformers.IsHelperName(tx.EmitContext(), node.Tag) { + if !ast.IsIdentifier(expression) && !transformers.IsHelperName(tx.EmitContext(), tag) { tx.EmitContext().AddEmitFlags(updated, printer.EFIndirectCall) } return updated diff --git a/tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.js b/tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.js new file mode 100644 index 0000000000000..ec3bd2ad7c422 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.js @@ -0,0 +1,31 @@ +//// [tests/cases/compiler/commonJsIndirectCallWrappedCallee.ts] //// + +//// [a.ts] +export function f(this: unknown, ...args: any[]) { return this; } + +//// [index.ts] +import { f } from "./a"; +type F = () => unknown; +console.log(f() === undefined); +console.log((f)() === undefined); +console.log((f as F)() === undefined); +console.log((f!)() === undefined); +console.log((f satisfies F)() === undefined); +console.log((f)`x` === undefined); + + +//// [a.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.f = f; +function f(...args) { return this; } +//// [index.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const a_1 = require("./a"); +console.log((0, a_1.f)() === undefined); +console.log((0, a_1.f)() === undefined); +console.log((0, a_1.f)() === undefined); +console.log((0, a_1.f)() === undefined); +console.log((0, a_1.f)() === undefined); +console.log((0, a_1.f) `x` === undefined); diff --git a/tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.symbols b/tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.symbols new file mode 100644 index 0000000000000..623fb3826f384 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.symbols @@ -0,0 +1,60 @@ +//// [tests/cases/compiler/commonJsIndirectCallWrappedCallee.ts] //// + +=== /a.ts === +export function f(this: unknown, ...args: any[]) { return this; } +>f : Symbol(f, Decl(a.ts, 0, 0)) +>this : Symbol(this, Decl(a.ts, 0, 18)) +>args : Symbol(args, Decl(a.ts, 0, 32)) +>this : Symbol(this, Decl(a.ts, 0, 18)) + +=== /index.ts === +import { f } from "./a"; +>f : Symbol(f, Decl(index.ts, 0, 8)) + +type F = () => unknown; +>F : Symbol(F, Decl(index.ts, 0, 24)) + +console.log(f() === undefined); +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>f : Symbol(f, Decl(index.ts, 0, 8)) +>undefined : Symbol(undefined) + +console.log((f)() === undefined); +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>f : Symbol(f, Decl(index.ts, 0, 8)) +>undefined : Symbol(undefined) + +console.log((f as F)() === undefined); +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>f : Symbol(f, Decl(index.ts, 0, 8)) +>F : Symbol(F, Decl(index.ts, 0, 24)) +>undefined : Symbol(undefined) + +console.log((f!)() === undefined); +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>f : Symbol(f, Decl(index.ts, 0, 8)) +>undefined : Symbol(undefined) + +console.log((f satisfies F)() === undefined); +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>f : Symbol(f, Decl(index.ts, 0, 8)) +>F : Symbol(F, Decl(index.ts, 0, 24)) +>undefined : Symbol(undefined) + +console.log((f)`x` === undefined); +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>f : Symbol(f, Decl(index.ts, 0, 8)) +>undefined : Symbol(undefined) + diff --git a/tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.types b/tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.types new file mode 100644 index 0000000000000..14a7c706e2244 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.types @@ -0,0 +1,85 @@ +//// [tests/cases/compiler/commonJsIndirectCallWrappedCallee.ts] //// + +=== /a.ts === +export function f(this: unknown, ...args: any[]) { return this; } +>f : (this: unknown, ...args: any[]) => unknown +>this : unknown +>args : any[] +>this : unknown + +=== /index.ts === +import { f } from "./a"; +>f : (this: unknown, ...args: any[]) => unknown + +type F = () => unknown; +>F : F + +console.log(f() === undefined); +>console.log(f() === undefined) : void +>console.log : (...data: any[]) => void +>console : Console +>log : (...data: any[]) => void +>f() === undefined : boolean +>f() : unknown +>f : (this: unknown, ...args: any[]) => unknown +>undefined : undefined + +console.log((f)() === undefined); +>console.log((f)() === undefined) : void +>console.log : (...data: any[]) => void +>console : Console +>log : (...data: any[]) => void +>(f)() === undefined : boolean +>(f)() : unknown +>(f) : (this: unknown, ...args: any[]) => unknown +>f : (this: unknown, ...args: any[]) => unknown +>undefined : undefined + +console.log((f as F)() === undefined); +>console.log((f as F)() === undefined) : void +>console.log : (...data: any[]) => void +>console : Console +>log : (...data: any[]) => void +>(f as F)() === undefined : boolean +>(f as F)() : unknown +>(f as F) : F +>f as F : F +>f : (this: unknown, ...args: any[]) => unknown +>undefined : undefined + +console.log((f!)() === undefined); +>console.log((f!)() === undefined) : void +>console.log : (...data: any[]) => void +>console : Console +>log : (...data: any[]) => void +>(f!)() === undefined : boolean +>(f!)() : unknown +>(f!) : (this: unknown, ...args: any[]) => unknown +>f! : (this: unknown, ...args: any[]) => unknown +>f : (this: unknown, ...args: any[]) => unknown +>undefined : undefined + +console.log((f satisfies F)() === undefined); +>console.log((f satisfies F)() === undefined) : void +>console.log : (...data: any[]) => void +>console : Console +>log : (...data: any[]) => void +>(f satisfies F)() === undefined : boolean +>(f satisfies F)() : unknown +>(f satisfies F) : (this: unknown, ...args: any[]) => unknown +>f satisfies F : (this: unknown, ...args: any[]) => unknown +>f : (this: unknown, ...args: any[]) => unknown +>undefined : undefined + +console.log((f)`x` === undefined); +>console.log((f)`x` === undefined) : void +>console.log : (...data: any[]) => void +>console : Console +>log : (...data: any[]) => void +>(f)`x` === undefined : boolean +>(f)`x` : unknown +>(f) : (this: unknown, ...args: any[]) => unknown +>f : (this: unknown, ...args: any[]) => unknown +>`x` : "x" +>undefined : undefined + diff --git a/tsc/testdata/tests/cases/compiler/commonJsIndirectCallWrappedCallee.ts b/tsc/testdata/tests/cases/compiler/commonJsIndirectCallWrappedCallee.ts new file mode 100644 index 0000000000000..9540ce7fb9402 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/commonJsIndirectCallWrappedCallee.ts @@ -0,0 +1,15 @@ +// @module: commonjs +// @target: es2020 + +// @filename: /a.ts +export function f(this: unknown, ...args: any[]) { return this; } + +// @filename: /index.ts +import { f } from "./a"; +type F = () => unknown; +console.log(f() === undefined); +console.log((f)() === undefined); +console.log((f as F)() === undefined); +console.log((f!)() === undefined); +console.log((f satisfies F)() === undefined); +console.log((f)`x` === undefined); From febebd79913463d29a4e0e70fdc265464026aa27 Mon Sep 17 00:00:00 2001 From: Josue Barros Date: Mon, 16 Nov 2026 17:21:34 -0300 Subject: [PATCH 2/2] Keep the wrapped callee expressions around the indirect call 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. --- .../transformers/moduletransforms/commonjsmodule.go | 4 ++-- .../reference/compiler/commonJsIndirectCallWrappedCallee.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tsc/internal/transformers/moduletransforms/commonjsmodule.go b/tsc/internal/transformers/moduletransforms/commonjsmodule.go index 89317b78d46e3..668a9053978ee 100644 --- a/tsc/internal/transformers/moduletransforms/commonjsmodule.go +++ b/tsc/internal/transformers/moduletransforms/commonjsmodule.go @@ -1842,7 +1842,7 @@ func (tx *CommonJSModuleTransformer) visitCallExpression(node *ast.CallExpressio expression := tx.visitExpressionIdentifier(callee) updated := tx.Factory().UpdateCallExpression( node, - expression, + tx.Factory().RestoreOuterExpressions(node.Expression, expression, ast.OEKAll), node.QuestionDotToken, nil, /*typeArguments*/ tx.Visitor().VisitNodes(node.Arguments), @@ -2025,7 +2025,7 @@ func (tx *CommonJSModuleTransformer) visitTaggedTemplateExpression(node *ast.Tag expression := tx.visitExpressionIdentifier(tag) updated := tx.Factory().UpdateTaggedTemplateExpression( node, - expression, + tx.Factory().RestoreOuterExpressions(node.Tag, expression, ast.OEKAll), nil, /*questionDotToken*/ nil, /*typeArguments*/ tx.Visitor().VisitNode(node.Template), diff --git a/tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.js b/tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.js index ec3bd2ad7c422..f2f3c5f80c325 100644 --- a/tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.js +++ b/tsc/testdata/baselines/reference/compiler/commonJsIndirectCallWrappedCallee.js @@ -24,8 +24,8 @@ function f(...args) { return this; } Object.defineProperty(exports, "__esModule", { value: true }); const a_1 = require("./a"); console.log((0, a_1.f)() === undefined); +console.log((0, (a_1.f))() === undefined); console.log((0, a_1.f)() === undefined); +console.log((0, (a_1.f))() === undefined); console.log((0, a_1.f)() === undefined); -console.log((0, a_1.f)() === undefined); -console.log((0, a_1.f)() === undefined); -console.log((0, a_1.f) `x` === undefined); +console.log((0, (a_1.f)) `x` === undefined);