Skip to content

Commit

Permalink
fix receiver of imported and exported functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff committed Dec 27, 2019
1 parent 0f5ddd2 commit ec2ef0b
Show file tree
Hide file tree
Showing 94 changed files with 220 additions and 192 deletions.
28 changes: 28 additions & 0 deletions src/compiler/transformers/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace ts {
context.onEmitNode = onEmitNode;
context.enableSubstitution(SyntaxKind.Identifier); // Substitutes expression identifiers with imported/exported symbols.
context.enableSubstitution(SyntaxKind.BinaryExpression); // Substitutes assignments to exported symbols.
context.enableSubstitution(SyntaxKind.CallExpression); // Substitutes expression identifiers with imported/exported symbols.
context.enableSubstitution(SyntaxKind.TaggedTemplateExpression); // Substitutes expression identifiers with imported/exported symbols.
context.enableSubstitution(SyntaxKind.PrefixUnaryExpression); // Substitutes updates to exported symbols.
context.enableSubstitution(SyntaxKind.PostfixUnaryExpression); // Substitutes updates to exported symbols.
context.enableSubstitution(SyntaxKind.ShorthandPropertyAssignment); // Substitutes shorthand property assignments for imported/exported symbols.
Expand Down Expand Up @@ -1654,6 +1656,10 @@ namespace ts {
return substituteExpressionIdentifier(<Identifier>node);
case SyntaxKind.BinaryExpression:
return substituteBinaryExpression(<BinaryExpression>node);
case SyntaxKind.CallExpression:
return substituteCallExpression(<CallExpression>node);
case SyntaxKind.TaggedTemplateExpression:
return substituteTaggedTemplateExpression(<TaggedTemplateExpression>node);
case SyntaxKind.PostfixUnaryExpression:
case SyntaxKind.PrefixUnaryExpression:
return substituteUnaryExpression(<PrefixUnaryExpression | PostfixUnaryExpression>node);
Expand All @@ -1662,6 +1668,28 @@ namespace ts {
return node;
}

function substituteCallExpression(node: CallExpression) {
if (!isIdentifier(node.expression)) {
return node;
}
const newExpression = substituteExpressionIdentifier(node.expression);
if (newExpression !== node.expression) {
return updateCall(node, setTextRange(createBinary(createNumericLiteral("0"), SyntaxKind.CommaToken, newExpression), node.expression), /*typeArguments*/ undefined, node.arguments);
}
return node;
}

function substituteTaggedTemplateExpression(node: TaggedTemplateExpression) {
if (!isIdentifier(node.tag)) {
return node;
}
const newTag = substituteExpressionIdentifier(node.tag);
if (newTag !== node.tag) {
return updateTaggedTemplate(node, setTextRange(createBinary(createNumericLiteral("0"), SyntaxKind.CommaToken, newTag), node.tag), /*typeArguments*/ undefined, node.template);
}
return node;
}

/**
* Substitution for an Identifier expression that may contain an imported or exported
* symbol.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ exports.fn3 = fn3;`;
content: `"use strict";
exports.__esModule = true;
var fns_1 = require("../decls/fns");
fns_1.fn1();
fns_1.fn2();
(0, fns_1.fn1)();
(0, fns_1.fn2)();
${appendJs}`
}]
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exports.vextend = extend;
exports.__esModule = true;
var func_1 = require("./func");
// hover on vextend
exports.a = func_1.vextend({
exports.a = (0, func_1.vextend)({
watch: {
data1: function (val) {
this.data2 = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exports.__esModule = true;
"use strict";
exports.__esModule = true;
var file1_1 = require("./file1");
exports.A = file1_1.styled();
exports.A = (0, file1_1.styled)();


//// [color.d.ts]
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/ambientDeclarationsPatterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ foo(fileText);
exports.__esModule = true;
///<reference path="declarations.d.ts" />
var foobarbaz_1 = require("foobarbaz");
foobarbaz_1.foo(foobarbaz_1.baz);
(0, foobarbaz_1.foo)(foobarbaz_1.baz);
var foosball_1 = require("foosball");
foobarbaz_1.foo(foosball_1.foos);
(0, foobarbaz_1.foo)(foosball_1.foos);
// Works with relative file name
var file_text_1 = require("./file!text");
foobarbaz_1.foo(file_text_1["default"]);
(0, foobarbaz_1.foo)(file_text_1["default"]);
2 changes: 1 addition & 1 deletion tests/baselines/reference/ambientShorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ exports.__esModule = true;
var jquery_1 = require("jquery");
var baz = require("fs");
var boom = require("jquery");
jquery_1["default"](jquery_1.bar, baz, boom);
(0, jquery_1["default"])(jquery_1.bar, baz, boom);
2 changes: 1 addition & 1 deletion tests/baselines/reference/ambientShorthand_reExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ exports.__esModule = true;
var reExportX_1 = require("./reExportX");
var $ = require("./reExportAll");
// '$' is not callable, it is an object.
reExportX_1.x($);
(0, reExportX_1.x)($);
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ define("Class", ["require", "exports", "Configurable"], function (require, expor
return _super !== null && _super.apply(this, arguments) || this;
}
return ActualClass;
}(Configurable_1.Configurable(HiddenClass)));
}((0, Configurable_1.Configurable)(HiddenClass)));
exports.ActualClass = ActualClass;
});

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/anonClassDeclarationEmitIsAnon.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ var __extends = (this && this.__extends) || (function () {
})();
exports.__esModule = true;
var wrapClass_1 = require("./wrapClass");
exports["default"] = wrapClass_1.wrapClass(0);
exports["default"] = (0, wrapClass_1.wrapClass)(0);
// Simple class
var User = /** @class */ (function () {
function User() {
Expand All @@ -106,7 +106,7 @@ var TimestampedUser = /** @class */ (function (_super) {
return _super.call(this) || this;
}
return TimestampedUser;
}(wrapClass_1.Timestamped(User)));
}((0, wrapClass_1.Timestamped)(User)));
exports.TimestampedUser = TimestampedUser;


Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/commonjsSafeImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports.Foo = Foo;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var _10_lib_1 = require("./10_lib");
_10_lib_1.Foo();
(0, _10_lib_1.Foo)();


//// [10_lib.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ export const y = bar();
exports.__esModule = true;
var foo_1 = require("foo");
var root_1 = require("root");
exports.x = foo_1.foo();
exports.y = root_1.bar();
exports.x = (0, foo_1.foo)();
exports.y = (0, root_1.bar)();
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exports.__esModule = true;
var a_1 = require("./a");
function q() { }
exports.q = q;
q.val = a_1.f();
q.val = (0, a_1.f)();


//// [a.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Point.zero = (): Point => Point(0, 0);
exports.__esModule = true;
exports.Point = function (x, y) { return ({ x: x, y: y }); };
exports.Rect = function (a, b) { return ({ a: a, b: b }); };
exports.Point.zero = function () { return exports.Point(0, 0); };
exports.Point.zero = function () { return (0, exports.Point)(0, 0); };


//// [declarationEmitExpandoWithGenericConstraint.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.bar = bar;
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("./utils");
exports.bar = utils_1.bar;
utils_1.foo();
(0, utils_1.foo)();
var obj;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const a: import("typescript-fsa").A;
"use strict";
exports.__esModule = true;
var typescript_fsa_1 = require("typescript-fsa");
exports.a = typescript_fsa_1.getA();
exports.a = (0, typescript_fsa_1.getA)();


//// [index.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const a: import("typescript-fsa").A;
"use strict";
exports.__esModule = true;
var typescript_fsa_1 = require("typescript-fsa");
exports.a = typescript_fsa_1.getA();
exports.a = (0, typescript_fsa_1.getA)();


//// [index.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exports.createNamed = createNamed;
"use strict";
exports.__esModule = true;
var b_1 = require("./b");
exports.Value = b_1.createNamed();
exports.Value = (0, b_1.createNamed)();


//// [b.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function b(text: string) {
exports.__esModule = true;
var a_1 = require("@ts-bug/a");
function b(text) {
return a_1.a(text);
return (0, a_1.a)(text);
}
exports.b = b;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
var scalar_1 = require("../lib/operators/scalar");
exports.default = {
get xs() {
return scalar_1.scalar("14px");
return (0, scalar_1.scalar)("14px");
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ define("settings/spacing", ["require", "exports", "lib/operators/scalar"], funct
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = {
get xs() {
return scalar_1.scalar("14px");
return (0, scalar_1.scalar)("14px");
}
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export const fun2 = create<Q>();
"use strict";
exports.__esModule = true;
var bbb_1 = require("./bbb");
exports.fun = bbb_1.create();
exports.fun2 = bbb_1.create();
exports.fun = (0, bbb_1.create)();
exports.fun2 = (0, bbb_1.create)();


//// [index.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports.foo = foo;
exports.__esModule = true;
var file2_1 = require("./file2");
function bar() {
return file2_1.foo();
return (0, file2_1.foo)();
}
exports.bar = bar;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ exports.testRecFun = function (parent) {
return {
result: parent,
deeper: function (child) {
return exports.testRecFun(__assign(__assign({}, parent), child));
return (0, exports.testRecFun)(__assign(__assign({}, parent), child));
}
};
};
var p1 = exports.testRecFun({ one: '1' });
var p1 = (0, exports.testRecFun)({ one: '1' });
void p1.result.one;
var p2 = p1.deeper({ two: '2' });
void p2.result.one;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exports["default"] = Something;
"use strict";
exports.__esModule = true;
var root_1 = require("./root");
exports.instance = root_1.getSomething();
exports.instance = (0, root_1.getSomething)();


//// [root.d.ts]
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/defaultExportsCannotMerge01.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ exports.default = Decl;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var m1_1 = require("m1");
m1_1.default();
(0, m1_1.default)();
var x;
var y;
m1_1.default.x;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/defaultExportsCannotMerge02.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ exports.default = Decl;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var m1_1 = require("m1");
m1_1.default();
(0, m1_1.default)();
var x;
var y;
var z = new m1_1.default();
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/defaultExportsCannotMerge03.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ exports.default = Decl;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var m1_1 = require("m1");
m1_1.default();
(0, m1_1.default)();
var x;
var y;
var z = new m1_1.default();
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/duplicatePackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ exports.__esModule = true;
var a_1 = require("a");
var b_1 = require("b");
var c_1 = require("c");
a_1.a(b_1.b); // Works
a_1.a(c_1.c); // Error, these are from different versions of the library.
(0, a_1.a)(b_1.b); // Works
(0, a_1.a)(c_1.c); // Error, these are from different versions of the library.
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ use(o);
exports.__esModule = true;
var use_1 = require("foo/use");
var a_1 = require("a");
use_1.use(a_1.o);
(0, use_1.use)(a_1.o);
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ use(o);
exports.__esModule = true;
var use_1 = require("@foo/bar/use");
var a_1 = require("a");
use_1.use(a_1.o);
(0, use_1.use)(a_1.o);
2 changes: 1 addition & 1 deletion tests/baselines/reference/es3defaultAliasIsQuoted.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ exports["default"] = assert;
"use strict";
exports.__esModule = true;
var es3defaultAliasQuoted_file0_1 = require("./es3defaultAliasQuoted_file0");
es3defaultAliasQuoted_file0_1["default"](es3defaultAliasQuoted_file0_1.Foo.CONSTANT === "Foo");
(0, es3defaultAliasQuoted_file0_1["default"])(es3defaultAliasQuoted_file0_1.Foo.CONSTANT === "Foo");
4 changes: 2 additions & 2 deletions tests/baselines/reference/es5-importHelpersAsyncFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export declare function __generator(body: Function): any;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
function foo() {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
return (0, tslib_1.__generator)(this, function (_a) {
return [2 /*return*/];
});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/esModuleInterop.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ var hybrid_1 = require("./hybrid");
var path_1 = __importDefault(require("./path"));
var fs = __importStar(require("./fs"));
path_1["default"];
hybrid_1.sayHello();
(0, hybrid_1.sayHello)();
fs;
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.username = () => 'username';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
tslib_1.__exportStar(require("./username"), exports);
(0, tslib_1.__exportStar)(require("./username"), exports);
//// [hello.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand All @@ -34,6 +34,6 @@ exports.default = sayHello;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const hello_1 = tslib_1.__importDefault(require("./hello"));
const hello_1 = (0, tslib_1.__importDefault)(require("./hello"));
const utils_1 = require("./utils");
hello_1.default(utils_1.username());
(0, hello_1.default)((0, utils_1.username)());
12 changes: 6 additions & 6 deletions tests/baselines/reference/esModuleInteropTslibHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export { Bar }
"use strict";
exports.__esModule = true;
var tslib_1 = require("tslib");
var path_1 = tslib_1.__importDefault(require("path"));
var path_1 = (0, tslib_1.__importDefault)(require("path"));
path_1["default"].resolve("", "../");
var Foo = /** @class */ (function () {
function Foo() {
Expand All @@ -35,7 +35,7 @@ exports.Foo = Foo;
"use strict";
exports.__esModule = true;
var tslib_1 = require("tslib");
var path = tslib_1.__importStar(require("path"));
var path = (0, tslib_1.__importStar)(require("path"));
path.resolve("", "../");
var Foo2 = /** @class */ (function () {
function Foo2() {
Expand All @@ -47,8 +47,8 @@ exports.Foo2 = Foo2;
"use strict";
exports.__esModule = true;
var tslib_1 = require("tslib");
var path_1 = tslib_1.__importDefault(require("path"));
path_1["default"]("", "../");
var path_1 = (0, tslib_1.__importDefault)(require("path"));
(0, path_1["default"])("", "../");
var Foo3 = /** @class */ (function () {
function Foo3() {
}
Expand All @@ -59,6 +59,6 @@ exports.Foo3 = Foo3;
"use strict";
exports.__esModule = true;
var tslib_1 = require("tslib");
var path_1 = tslib_1.__importStar(require("path"));
var path_1 = (0, tslib_1.__importStar)(require("path"));
exports.Bar = path_1.Bar;
path_1["default"]("", "../");
(0, path_1["default"])("", "../");
Loading

0 comments on commit ec2ef0b

Please sign in to comment.