| 1 |
import * as evaluator from "../../_namespaces/evaluator"; |
| 2 |
import * as ts from "../../_namespaces/ts"; |
| 3 |
|
| 4 |
describe("unittests:: evaluation:: destructuring", () => { |
| 5 |
// https://github.com/microsoft/TypeScript/issues/39205 |
| 6 |
describe("correct order for array destructuring evaluation and initializers", () => { |
| 7 |
it("when element is undefined", () => { |
| 8 |
const result = evaluator.evaluateTypeScript(` |
| 9 |
export const output: any[] = []; |
| 10 |
const order = (n: any): any => output.push(n); |
| 11 |
let [{ [order(1)]: x } = order(0)] = []; |
| 12 |
`, { target: ts.ScriptTarget.ES5 }); |
| 13 |
assert.deepEqual(result.output, [0, 1]); |
| 14 |
}); |
| 15 |
it("when element is defined", async () => { |
| 16 |
const result = evaluator.evaluateTypeScript(` |
| 17 |
export const output: any[] = []; |
| 18 |
const order = (n: any): any => output.push(n); |
| 19 |
let [{ [order(1)]: x } = order(0)] = [{}]; |
| 20 |
`, { target: ts.ScriptTarget.ES5 }); |
| 21 |
assert.deepEqual(result.output, [1]); |
| 22 |
}); |
| 23 |
}); |
| 24 |
describe("correct order for array destructuring evaluation and initializers with spread", () => { |
| 25 |
it("ES5", () => { |
| 26 |
const result = evaluator.evaluateTypeScript(` |
| 27 |
export const output: any[] = []; |
| 28 |
const order = (n: any): any => output.push(n); |
| 29 |
let { [order(0)]: { [order(2)]: z } = order(1), ...w } = {} as any; |
| 30 |
`, { target: ts.ScriptTarget.ES5 }); |
| 31 |
assert.deepEqual(result.output, [0, 1, 2]); |
| 32 |
}); |
| 33 |
it("ES2015", () => { |
| 34 |
const result = evaluator.evaluateTypeScript(` |
| 35 |
export const output: any[] = []; |
| 36 |
const order = (n: any): any => output.push(n); |
| 37 |
let { [order(0)]: { [order(2)]: z } = order(1), ...w } = {} as any; |
| 38 |
`, { target: ts.ScriptTarget.ES2015 }); |
| 39 |
assert.deepEqual(result.output, [0, 1, 2]); |
| 40 |
}); |
| 41 |
}); |
| 42 |
describe("correct evaluation for nested rest assignment in destructured object", () => { |
| 43 |
it("ES5", () => { |
| 44 |
const result = evaluator.evaluateTypeScript(` |
| 45 |
let a: any, b: any, c: any = { x: { a: 1, y: 2 } }, d: any; |
| 46 |
({ x: { a, ...b } = d } = c); |
| 47 |
export const output = { a, b }; |
| 48 |
`, { target: ts.ScriptTarget.ES5 }); |
| 49 |
assert.deepEqual(result.output, { a: 1, b: { y: 2 } }); |
| 50 |
}); |
| 51 |
it("ES2015", () => { |
| 52 |
const result = evaluator.evaluateTypeScript(` |
| 53 |
let a: any, b: any, c: any = { x: { a: 1, y: 2 } }, d: any; |
| 54 |
({ x: { a, ...b } = d } = c); |
| 55 |
export const output = { a, b }; |
| 56 |
`, { target: ts.ScriptTarget.ES2015 }); |
| 57 |
assert.deepEqual(result.output, { a: 1, b: { y: 2 } }); |
| 58 |
}); |
| 59 |
it("ES2018", () => { |
| 60 |
const result = evaluator.evaluateTypeScript(` |
| 61 |
let a: any, b: any, c: any = { x: { a: 1, y: 2 } }, d: any; |
| 62 |
({ x: { a, ...b } = d } = c); |
| 63 |
export const output = { a, b }; |
| 64 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 65 |
assert.deepEqual(result.output, { a: 1, b: { y: 2 } }); |
| 66 |
}); |
| 67 |
}); |
| 68 |
}); |