Skip to content

Commit 13eff9c

Browse files
committed
Move indirect call substitution to printer
1 parent 3b06ef1 commit 13eff9c

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

src/compiler/emitter.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,8 +2417,26 @@ namespace ts {
24172417
emitTokenWithComment(SyntaxKind.CloseBracketToken, node.argumentExpression.end, writePunctuation, node);
24182418
}
24192419

2420+
function maybeEmitIndirectCallPrefix(node: CallExpression | TaggedTemplateExpression) {
2421+
const indirect = getEmitFlags(node) & EmitFlags.IndirectCall;
2422+
if (indirect) {
2423+
writeTokenText(SyntaxKind.OpenParenToken, writePunctuation);
2424+
writeLiteral("0");
2425+
writeTokenText(SyntaxKind.CommaToken, writePunctuation);
2426+
writeSpace();
2427+
return true;
2428+
}
2429+
return false;
2430+
}
2431+
2432+
function emitIndirectCallSuffix() {
2433+
writeTokenText(SyntaxKind.CloseParenToken, writePunctuation);
2434+
}
2435+
24202436
function emitCallExpression(node: CallExpression) {
2437+
const indirect = maybeEmitIndirectCallPrefix(node);
24212438
emit(node.expression);
2439+
if (indirect) emitIndirectCallSuffix();
24222440
emit(node.questionDotToken);
24232441
emitTypeArguments(node, node.typeArguments);
24242442
emitList(node, node.arguments, ListFormat.CallExpressionArguments);
@@ -2433,7 +2451,9 @@ namespace ts {
24332451
}
24342452

24352453
function emitTaggedTemplateExpression(node: TaggedTemplateExpression) {
2454+
const indirect = maybeEmitIndirectCallPrefix(node);
24362455
emit(node.tag);
2456+
if (indirect) emitIndirectCallSuffix();
24372457
emitTypeArguments(node, node.typeArguments);
24382458
writeSpace();
24392459
emit(node.template);

src/compiler/transformers/module/module.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,27 +1813,14 @@ namespace ts {
18131813

18141814
function substituteCallExpression(node: CallExpression) {
18151815
if (isIdentifier(node.expression) && getImportOrExportBindingReference(node.expression, /*removeEntry*/ false)) {
1816-
return isCallChain(node) ?
1817-
factory.updateCallChain(node,
1818-
setTextRange(factory.createComma(factory.createNumericLiteral(0), node.expression), node.expression),
1819-
node.questionDotToken,
1820-
/*typeArguments*/ undefined,
1821-
node.arguments) :
1822-
factory.updateCallExpression(node,
1823-
setTextRange(factory.createComma(factory.createNumericLiteral(0), node.expression), node.expression),
1824-
/*typeArguments*/ undefined,
1825-
node.arguments);
1816+
addEmitFlags(node, EmitFlags.IndirectCall);
18261817
}
18271818
return node;
18281819
}
18291820

18301821
function substituteTaggedTemplateExpression(node: TaggedTemplateExpression) {
18311822
if (isIdentifier(node.tag) && getImportOrExportBindingReference(node.tag, /*removeEntry*/ false)) {
1832-
return factory.updateTaggedTemplateExpression(
1833-
node,
1834-
setTextRange(factory.createComma(factory.createNumericLiteral(0), node.tag), node.tag),
1835-
/*typeArguments*/ undefined,
1836-
node.template);
1823+
addEmitFlags(node, EmitFlags.IndirectCall);
18371824
}
18381825
return node;
18391826
}

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6624,6 +6624,7 @@ namespace ts {
66246624
/*@internal*/ TypeScriptClassWrapper = 1 << 25, // The node is an IIFE class wrapper created by the ts transform.
66256625
/*@internal*/ NeverApplyImportHelper = 1 << 26, // Indicates the node should never be wrapped with an import star helper (because, for example, it imports tslib itself)
66266626
/*@internal*/ IgnoreSourceNewlines = 1 << 27, // Overrides `printerOptions.preserveSourceNewlines` to print this node (and all descendants) with default whitespace.
6627+
/*@internal*/ IndirectCall = 1 << 28, // Write `(0,` before the expression and `)` after the expression of a call to ensure the call is indirect (i.e., no `this` binding)
66276628
}
66286629

66296630
export interface EmitHelperBase {

0 commit comments

Comments
 (0)