mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-11-18 20:11:14 +00:00
fix receiver on calls of imported and exported functions (#35877)
* fix receiver of imported and exported functions fixes: #35420 * Rebase against master and clean up substitution flow * Add evaluator tests * Fix evaluator tests Co-authored-by: Ron Buckton <ron.buckton@microsoft.com>
This commit is contained in:
1 parent
a8742e35cb
commit
7751ecb544
166 files changed
+599
-376
No files matched your search
@@ -92,6 +92,7 @@
|
||||
"unittests/evaluation/asyncGenerator.ts",
|
||||
"unittests/evaluation/awaiter.ts",
|
||||
"unittests/evaluation/destructuring.ts",
|
||||
"unittests/evaluation/externalModules.ts",
|
||||
"unittests/evaluation/forAwaitOf.ts",
|
||||
"unittests/evaluation/forOf.ts",
|
||||
"unittests/evaluation/optionalCall.ts",
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
describe("unittests:: evaluation:: externalModules", () => {
|
||||
// https://github.com/microsoft/TypeScript/issues/35420
|
||||
it("Correct 'this' in function exported from external module", async () => {
|
||||
const result = evaluator.evaluateTypeScript({
|
||||
files: {
|
||||
"/.src/output.ts": `
|
||||
export const output: any[] = [];
|
||||
`,
|
||||
"/.src/other.ts": `
|
||||
import { output } from "./output";
|
||||
export function f(this: any, expected) {
|
||||
output.push(this === expected);
|
||||
}
|
||||
|
||||
// 0
|
||||
f(undefined);
|
||||
`,
|
||||
"/.src/main.ts": `
|
||||
export { output } from "./output";
|
||||
import { output } from "./output";
|
||||
import { f } from "./other";
|
||||
import * as other from "./other";
|
||||
|
||||
// 1
|
||||
f(undefined);
|
||||
|
||||
// 2
|
||||
const obj = {};
|
||||
f.call(obj, obj);
|
||||
|
||||
// 3
|
||||
other.f(other);
|
||||
`
|
||||
},
|
||||
rootFiles: ["/.src/main.ts"],
|
||||
main: "/.src/main.ts"
|
||||
});
|
||||
assert.equal(result.output[0], true); // `f(undefined)` inside module. Existing behavior is correct.
|
||||
assert.equal(result.output[1], true); // `f(undefined)` from import. New behavior to match first case.
|
||||
assert.equal(result.output[2], true); // `f.call(obj, obj)`. Behavior of `.call` (or `.apply`, etc.) should not be affected.
|
||||
assert.equal(result.output[3], true); // `other.f(other)`. `this` is still namespace because it is left of `.`.
|
||||
});
|
||||
|
||||
it("Correct 'this' in function expression exported from external module", async () => {
|
||||
const result = evaluator.evaluateTypeScript({
|
||||
files: {
|
||||
"/.src/output.ts": `
|
||||
export const output: any[] = [];
|
||||
`,
|
||||
"/.src/other.ts": `
|
||||
import { output } from "./output";
|
||||
export const f = function(this: any, expected) {
|
||||
output.push(this === expected);
|
||||
}
|
||||
|
||||
// 0
|
||||
f(undefined);
|
||||
`,
|
||||
"/.src/main.ts": `
|
||||
export { output } from "./output";
|
||||
import { output } from "./output";
|
||||
import { f } from "./other";
|
||||
import * as other from "./other";
|
||||
|
||||
// 1
|
||||
f(undefined);
|
||||
|
||||
// 2
|
||||
const obj = {};
|
||||
f.call(obj, obj);
|
||||
|
||||
// 3
|
||||
other.f(other);
|
||||
`
|
||||
},
|
||||
rootFiles: ["/.src/main.ts"],
|
||||
main: "/.src/main.ts"
|
||||
});
|
||||
assert.equal(result.output[0], true); // `f(undefined)` inside module. Existing behavior is incorrect.
|
||||
assert.equal(result.output[1], true); // `f(undefined)` from import. New behavior to match first case.
|
||||
assert.equal(result.output[2], true); // `f.call(obj, obj)`. Behavior of `.call` (or `.apply`, etc.) should not be affected.
|
||||
assert.equal(result.output[3], true); // `other.f(other)`. `this` is still namespace because it is left of `.`.
|
||||
});
|
||||
});
|
||||
@@ -238,8 +238,8 @@ exports.fn3 = fn3;`;
|
||||
content: `"use strict";
|
||||
exports.__esModule = true;${appendJsText === changeJs ? "\nexports.fn3 = void 0;" : ""}
|
||||
var fns_1 = require("../decls/fns");
|
||||
fns_1.fn1();
|
||||
fns_1.fn2();
|
||||
(0, fns_1.fn1)();
|
||||
(0, fns_1.fn2)();
|
||||
${appendJs}`
|
||||
}]
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user