-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathdestructuring.spec.ts
More file actions
250 lines (224 loc) · 8.46 KB
/
destructuring.spec.ts
File metadata and controls
250 lines (224 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { cannotAssignToNodeOfKind, invalidMultiReturnAccess } from "../../src/transformation/utils/diagnostics";
import * as util from "../util";
const allBindings = "x, y, z, rest";
const testCases = [
{ binding: "{ x }", value: { x: true } },
{ binding: "{ x, y }", value: { x: false, y: true } },
{ binding: "{ x: z, y }", value: { x: true, y: false } },
{ binding: "{ x: { x, y }, z }", value: { x: { x: true, y: false }, z: false } },
{ binding: "{ x, y = true }", value: { x: false, y: false } },
{ binding: "{ x = true }", value: {} },
{ binding: "{ x, y = true }", value: { x: false } },
{ binding: "{ ...rest }", value: {} },
{ binding: "{ x, ...rest }", value: { x: "x" } },
{ binding: "{ x, ...rest }", value: { x: "x", y: "y", z: "z" } },
{ binding: "{ x, ...y }", value: { x: "x", y: "y", z: "z" } },
{ binding: "{ x: y, ...z }", value: { x: "x", y: "y", z: "z" } },
{ binding: "[]", value: [] },
{ binding: "[x, y]", value: ["x", "y"] },
{ binding: "[x, , y]", value: ["x", "", "y"] },
{ binding: "[x = true]", value: [false] },
{ binding: "[[x, y]]", value: [["x", "y"]] },
{ binding: "[x, ...rest]", value: ["x"] },
{ binding: "[x, ...rest]", value: ["x", "y", "z"] },
{ binding: "{ y: [z = true] }", value: { y: [false] } },
{ binding: "{ x: [x, y] }", value: { x: ["x", "y"] } },
{ binding: "{ x: [{ y }] }", value: { x: [{ y: "y" }] } },
].map(({ binding, value }) => ({ binding, value: util.formatCode(value) }));
test.each([
...testCases,
{ binding: "{ x, y }, z", value: "{ x: false, y: false }, true" },
{ binding: "{ x, y }, { z }", value: "{ x: false, y: false }, { z: true }" },
])("in function parameter (%p)", ({ binding, value }) => {
util.testFunction`
let ${allBindings};
function test(${binding}) {
return { ${allBindings} };
}
return test(${value});
`.expectToMatchJsResult();
});
test("in function parameter creates local variables", () => {
const builder = util.testModule`
function test({ a, b }: any) {}
`;
const code = builder.getMainLuaCodeChunk();
expect(code).toContain("local a =");
expect(code).toContain("local b =");
});
test("in function parameter creates local variables in correct scope", () => {
util.testFunction`
let x = 7;
function foo([x]: [number]) {
x *= 2;
}
foo([1]);
return x;
`.expectToMatchJsResult();
});
test.each(testCases)("in variable declaration (%p)", ({ binding, value }) => {
util.testFunction`
let ${allBindings};
{
const ${binding} = ${value};
return { ${allBindings} };
}
`.expectToMatchJsResult();
});
test.each(testCases)("in variable declaration from const variable (%p)", ({ binding, value }) => {
util.testFunction`
let ${allBindings};
{
const v: any = ${value};
const ${binding} = v;
return { ${allBindings} };
}
`.expectToMatchJsResult();
});
test.each(testCases)("in variable declaration from this (%p)", ({ binding, value }) => {
util.testFunction`
let ${allBindings};
function test(this: any) {
const ${binding} = this;
return { ${allBindings} };
}
return test.call(${value});
`.expectToMatchJsResult();
});
test.each(testCases)("in exported variable declaration (%p)", ({ binding, value }) => {
util.testModule`
export const ${binding} = ${value};
`.expectToMatchJsResult();
});
const assignmentTestCases = [
...testCases,
...[
{ binding: "{ x: obj.prop }", value: { x: true } },
{ binding: "{ x: obj.prop = true }", value: {} },
{ binding: "[{ x: obj.prop }]", value: [{ x: true }] },
{ binding: "{ obj: { prop: obj.prop } }", value: { obj: { prop: true } } },
{ binding: "{ x = true }", value: {} },
].map(({ binding, value }) => ({ binding, value: util.formatCode(value) })),
{ binding: "{ x: { [(3).toString()]: y } }", value: "{ x: { [(3).toString()]: true } }" },
];
test.each(assignmentTestCases)("in assignment expression (%p)", ({ binding, value }) => {
util.testFunction`
let ${allBindings};
const obj = { prop: false };
const expressionResult = (${binding} = ${value});
return { ${allBindings}, obj, expressionResult };
`.expectToMatchJsResult();
});
test.each(assignmentTestCases)("in assignment expression from const variable (%p)", ({ binding, value }) => {
util.testFunction`
let ${allBindings};
const obj = { prop: false };
const v: any = ${value};
const expressionResult = (${binding} = v);
return { ${allBindings}, expressionResult };
`.expectToMatchJsResult();
});
test.each(assignmentTestCases)("in assignment expression from this (%p)", ({ binding, value }) => {
util.testFunction`
let ${allBindings};
const obj = { prop: false };
function test(this: any) {
const expressionResult = (${binding} = this);
return { ${allBindings}, obj, expressionResult };
}
return test.call(${value});
`.expectToMatchJsResult();
});
test.each(["[]", "{}"])("empty binding pattern", bindingPattern => {
util.testFunction`
let i = 1;
const ${bindingPattern} = [i++];
return i;
`.expectToMatchJsResult();
});
// TODO: https://github.com/microsoft/TypeScript/pull/35906
// Adjust this test to use expectToMatchJsResult() and testCases when this issue is fixed.
test.each([
["foo", "['bar']"],
["[foo]", "[['bar']]"],
["[foo = 'bar']", "[[]]"],
["{ foo }", "[{ foo: 'bar' }]"],
["{ x: foo }", "[{ x: 'bar' }]"],
["{ foo = 'bar' }", "[{}] as { foo?: string }[]"],
])("forof assignment updates dependencies", (initializer, expression) => {
util.testModule`
let foo = '';
export { foo };
for (${initializer} of ${expression}) {}
`
.setReturnExport("foo")
.expectToEqual("bar");
});
test.each(testCases)("forof variable declaration binding patterns (%p)", ({ binding, value }) => {
util.testFunction`
let ${allBindings};
for (const ${binding} of [${value} as any]) {
return { ${allBindings} };
}
`.expectToMatchJsResult();
});
describe("array destructuring optimization", () => {
// TODO: Try to generalize optimization logic between declaration and assignment and make more generic tests
test("array", () => {
util.testFunction`
const array = [3, 5, 1];
const [a, b, c] = array;
return { a, b, c };
`
.tap(builder => expect(builder.getMainLuaCodeChunk()).toContain("unpack"))
.expectToMatchJsResult();
});
util.testEachVersion(
"array versions",
() =>
util.testFunction`
const array = [3, 5, 1];
const [a, b, c] = array;
return { a, b, c };
`,
util.expectEachVersionExceptJit(builder => builder.expectToMatchJsResult())
);
test("array literal", () => {
util.testFunction`
const [a, b, c] = [3, 5, 1];
return { a, b, c };
`
.tap(builder => expect(builder.getMainLuaCodeChunk()).not.toContain("unpack"))
.expectToMatchJsResult();
});
test("array literal with extra values", () => {
util.testFunction`
let called = false;
const set = () => { called = true; };
const [head] = ["foo", set()];
return { head, called };
`
.tap(builder => expect(builder.getMainLuaCodeChunk()).not.toContain("unpack"))
.expectToMatchJsResult();
});
test("array union", () => {
util.testFunction`
const array: [string] | [] = ["bar"];
let x: string;
[x] = array;
return x;
`
.tap(builder => expect(builder.getMainLuaCodeChunk()).toContain("unpack"))
.expectToMatchJsResult();
});
});
test("no exception from semantically invalid TS", () => {
util.testModule`
declare function testFunc(value: number): LuaMultiReturn<[number, number]>;
let [a, b] = testFunc(5) // Missing ;
[a, b] = testFunc(b) // Interpreted as testFunc(5)[a, b]
`
.withLanguageExtensions()
.disableSemanticCheck()
.expectToHaveDiagnostics([invalidMultiReturnAccess.code, cannotAssignToNodeOfKind.code]);
});