Skip to content

Commit f0a7f99

Browse files
committed
Indirect calls for imported functions
1 parent 5be0d71 commit f0a7f99

File tree

175 files changed

+10922
-333
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

175 files changed

+10922
-333
lines changed

package-lock.json

Lines changed: 10547 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/compiler/emitter.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,7 +2473,17 @@ namespace ts {
24732473
}
24742474

24752475
function emitCallExpression(node: CallExpression) {
2476+
const indirectCall = getEmitFlags(node) & EmitFlags.IndirectCall;
2477+
if (indirectCall) {
2478+
writePunctuation("(");
2479+
writeLiteral("0");
2480+
writePunctuation(",");
2481+
writeSpace();
2482+
}
24762483
emitExpression(node.expression, parenthesizer.parenthesizeLeftSideOfAccess);
2484+
if (indirectCall) {
2485+
writePunctuation(")");
2486+
}
24772487
emit(node.questionDotToken);
24782488
emitTypeArguments(node, node.typeArguments);
24792489
emitExpressionList(node, node.arguments, ListFormat.CallExpressionArguments, parenthesizer.parenthesizeExpressionForDisallowedComma);
@@ -2488,7 +2498,17 @@ namespace ts {
24882498
}
24892499

24902500
function emitTaggedTemplateExpression(node: TaggedTemplateExpression) {
2501+
const indirectCall = getEmitFlags(node) & EmitFlags.IndirectCall;
2502+
if (indirectCall) {
2503+
writePunctuation("(");
2504+
writeLiteral("0");
2505+
writePunctuation(",");
2506+
writeSpace();
2507+
}
24912508
emitExpression(node.tag, parenthesizer.parenthesizeLeftSideOfAccess);
2509+
if (indirectCall) {
2510+
writePunctuation(")");
2511+
}
24922512
emitTypeArguments(node, node.typeArguments);
24932513
writeSpace();
24942514
emitExpression(node.template);

src/compiler/transformers/module/module.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ namespace ts {
3333
const previousOnEmitNode = context.onEmitNode;
3434
context.onSubstituteNode = onSubstituteNode;
3535
context.onEmitNode = onEmitNode;
36+
context.enableSubstitution(SyntaxKind.CallExpression); // Substitute calls to imported/exported symbols to avoid incorrect `this`.
37+
context.enableSubstitution(SyntaxKind.TaggedTemplateExpression); // Substitute calls to imported/exported symbols to avoid incorrect `this`.
3638
context.enableSubstitution(SyntaxKind.Identifier); // Substitutes expression identifiers with imported/exported symbols.
3739
context.enableSubstitution(SyntaxKind.BinaryExpression); // Substitutes assignments to exported symbols.
3840
context.enableSubstitution(SyntaxKind.PrefixUnaryExpression); // Substitutes updates to exported symbols.
@@ -47,6 +49,8 @@ namespace ts {
4749
let currentModuleInfo: ExternalModuleInfo; // The ExternalModuleInfo for the current file.
4850
let noSubstitution: boolean[]; // Set of nodes for which substitution rules should be ignored.
4951
let needUMDDynamicImportHelper: boolean;
52+
const zeroLiteral = factory.createNumericLiteral(0);
53+
setEmitFlags(zeroLiteral, EmitFlags.Immutable);
5054

5155
return chainBundle(context, transformSourceFile);
5256

@@ -1741,6 +1745,10 @@ namespace ts {
17411745
switch (node.kind) {
17421746
case SyntaxKind.Identifier:
17431747
return substituteExpressionIdentifier(node as Identifier);
1748+
case SyntaxKind.CallExpression:
1749+
return substituteCallExpression(node as CallExpression);
1750+
case SyntaxKind.TaggedTemplateExpression:
1751+
return substituteTaggedTemplateExpression(node as TaggedTemplateExpression);
17441752
case SyntaxKind.BinaryExpression:
17451753
return substituteBinaryExpression(node as BinaryExpression);
17461754
case SyntaxKind.PostfixUnaryExpression:
@@ -1751,6 +1759,43 @@ namespace ts {
17511759
return node;
17521760
}
17531761

1762+
function substituteCallExpression(node: CallExpression) {
1763+
if (isIdentifier(node.expression)) {
1764+
const expression = substituteExpressionIdentifier(node.expression);
1765+
noSubstitution[getNodeId(expression)] = true;
1766+
if (!isIdentifier(expression)) {
1767+
return addEmitFlags(
1768+
factory.updateCallExpression(node,
1769+
expression,
1770+
/*typeArguments*/ undefined,
1771+
node.arguments
1772+
),
1773+
EmitFlags.IndirectCall
1774+
);
1775+
1776+
}
1777+
}
1778+
return node;
1779+
}
1780+
1781+
function substituteTaggedTemplateExpression(node: TaggedTemplateExpression) {
1782+
if (isIdentifier(node.tag)) {
1783+
const tag = substituteExpressionIdentifier(node.tag);
1784+
noSubstitution[getNodeId(tag)] = true;
1785+
if (!isIdentifier(tag)) {
1786+
return addEmitFlags(
1787+
factory.updateTaggedTemplateExpression(node,
1788+
tag,
1789+
/*typeArguments*/ undefined,
1790+
node.template
1791+
),
1792+
EmitFlags.IndirectCall
1793+
);
1794+
}
1795+
}
1796+
return node;
1797+
}
1798+
17541799
/**
17551800
* Substitution for an Identifier expression that may contain an imported or exported
17561801
* symbol.

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6728,6 +6728,7 @@ namespace ts {
67286728
/*@internal*/ NeverApplyImportHelper = 1 << 26, // Indicates the node should never be wrapped with an import star helper (because, for example, it imports tslib itself)
67296729
/*@internal*/ IgnoreSourceNewlines = 1 << 27, // Overrides `printerOptions.preserveSourceNewlines` to print this node (and all descendants) with default whitespace.
67306730
/*@internal*/ Immutable = 1 << 28, // Indicates a node is a singleton intended to be reused in multiple locations. Any attempt to make further changes to the node will result in an error.
6731+
/*@internal*/ IndirectCall = 1 << 29, // Emit CallExpression as an indirect call: `(0, f)()`
67316732
}
67326733

67336734
export interface EmitHelperBase {

tests/baselines/reference/allowJscheckJsTypeParameterNoCrash.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ exports.__esModule = true;
3030
exports.a = void 0;
3131
var func_1 = require("./func");
3232
// hover on vextend
33-
exports.a = func_1.vextend({
33+
exports.a = (0, func_1.vextend)({
3434
watch: {
3535
data1: function (val) {
3636
this.data2 = 1;

tests/baselines/reference/allowSyntheticDefaultImportsCanPaintCrossModuleDeclaration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ exports.__esModule = true;
2323
exports.__esModule = true;
2424
exports.A = void 0;
2525
var file1_1 = require("./file1");
26-
exports.A = file1_1.styled();
26+
exports.A = (0, file1_1.styled)();
2727

2828

2929
//// [color.d.ts]

tests/baselines/reference/ambientDeclarationsPatterns.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ foo(fileText);
3737
exports.__esModule = true;
3838
///<reference path="declarations.d.ts" />
3939
var foobarbaz_1 = require("foobarbaz");
40-
foobarbaz_1.foo(foobarbaz_1.baz);
40+
(0, foobarbaz_1.foo)(foobarbaz_1.baz);
4141
var foosball_1 = require("foosball");
42-
foobarbaz_1.foo(foosball_1.foos);
42+
(0, foobarbaz_1.foo)(foosball_1.foos);
4343
// Works with relative file name
4444
var file_text_1 = require("./file!text");
45-
foobarbaz_1.foo(file_text_1["default"]);
45+
(0, foobarbaz_1.foo)(file_text_1["default"]);

tests/baselines/reference/ambientShorthand.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ exports.__esModule = true;
2020
var jquery_1 = require("jquery");
2121
var baz = require("fs");
2222
var boom = require("jquery");
23-
jquery_1["default"](jquery_1.bar, baz, boom);
23+
(0, jquery_1["default"])(jquery_1.bar, baz, boom);

tests/baselines/reference/ambientShorthand_reExport.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ exports.__esModule = true;
4949
var reExportX_1 = require("./reExportX");
5050
var $ = require("./reExportAll");
5151
// '$' is not callable, it is an object.
52-
reExportX_1.x($);
52+
(0, reExportX_1.x)($);

tests/baselines/reference/amdDeclarationEmitNoExtraDeclare.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ define("Class", ["require", "exports", "Configurable"], function (require, expor
7272
return _super !== null && _super.apply(this, arguments) || this;
7373
}
7474
return ActualClass;
75-
}(Configurable_1.Configurable(HiddenClass)));
75+
}((0, Configurable_1.Configurable)(HiddenClass)));
7676
exports.ActualClass = ActualClass;
7777
});
7878

0 commit comments

Comments
 (0)