| 1 |
import * as evaluator from "../../_namespaces/evaluator"; |
| 2 |
|
| 3 |
describe("unittests:: evaluation:: externalModules", () => { |
| 4 |
// https://github.com/microsoft/TypeScript/issues/35420 |
| 5 |
it("Correct 'this' in function exported from external module", async () => { |
| 6 |
const result = evaluator.evaluateTypeScript({ |
| 7 |
files: { |
| 8 |
"/.src/output.ts": ` |
| 9 |
export const output: any[] = []; |
| 10 |
`, |
| 11 |
"/.src/other.ts": ` |
| 12 |
import { output } from "./output"; |
| 13 |
export function f(this: any, expected) { |
| 14 |
output.push(this === expected); |
| 15 |
} |
| 16 |
|
| 17 |
// 0 |
| 18 |
f(undefined); |
| 19 |
`, |
| 20 |
"/.src/main.ts": ` |
| 21 |
export { output } from "./output"; |
| 22 |
import { output } from "./output"; |
| 23 |
import { f } from "./other"; |
| 24 |
import * as other from "./other"; |
| 25 |
|
| 26 |
// 1 |
| 27 |
f(undefined); |
| 28 |
|
| 29 |
// 2 |
| 30 |
const obj = {}; |
| 31 |
f.call(obj, obj); |
| 32 |
|
| 33 |
// 3 |
| 34 |
other.f(other); |
| 35 |
` |
| 36 |
}, |
| 37 |
rootFiles: ["/.src/main.ts"], |
| 38 |
main: "/.src/main.ts" |
| 39 |
}); |
| 40 |
assert.equal(result.output[0], true); // `f(undefined)` inside module. Existing behavior is correct. |
| 41 |
assert.equal(result.output[1], true); // `f(undefined)` from import. New behavior to match first case. |
| 42 |
assert.equal(result.output[2], true); // `f.call(obj, obj)`. Behavior of `.call` (or `.apply`, etc.) should not be affected. |
| 43 |
assert.equal(result.output[3], true); // `other.f(other)`. `this` is still namespace because it is left of `.`. |
| 44 |
}); |
| 45 |
|
| 46 |
it("Correct 'this' in function expression exported from external module", async () => { |
| 47 |
const result = evaluator.evaluateTypeScript({ |
| 48 |
files: { |
| 49 |
"/.src/output.ts": ` |
| 50 |
export const output: any[] = []; |
| 51 |
`, |
| 52 |
"/.src/other.ts": ` |
| 53 |
import { output } from "./output"; |
| 54 |
export const f = function(this: any, expected) { |
| 55 |
output.push(this === expected); |
| 56 |
} |
| 57 |
|
| 58 |
// 0 |
| 59 |
f(undefined); |
| 60 |
`, |
| 61 |
"/.src/main.ts": ` |
| 62 |
export { output } from "./output"; |
| 63 |
import { output } from "./output"; |
| 64 |
import { f } from "./other"; |
| 65 |
import * as other from "./other"; |
| 66 |
|
| 67 |
// 1 |
| 68 |
f(undefined); |
| 69 |
|
| 70 |
// 2 |
| 71 |
const obj = {}; |
| 72 |
f.call(obj, obj); |
| 73 |
|
| 74 |
// 3 |
| 75 |
other.f(other); |
| 76 |
` |
| 77 |
}, |
| 78 |
rootFiles: ["/.src/main.ts"], |
| 79 |
main: "/.src/main.ts" |
| 80 |
}); |
| 81 |
assert.equal(result.output[0], true); // `f(undefined)` inside module. Existing behavior is incorrect. |
| 82 |
assert.equal(result.output[1], true); // `f(undefined)` from import. New behavior to match first case. |
| 83 |
assert.equal(result.output[2], true); // `f.call(obj, obj)`. Behavior of `.call` (or `.apply`, etc.) should not be affected. |
| 84 |
assert.equal(result.output[3], true); // `other.f(other)`. `this` is still namespace because it is left of `.`. |
| 85 |
}); |
| 86 |
}); |