forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdestructuring.spec.ts
More file actions
172 lines (153 loc) · 5.94 KB
/
destructuring.spec.ts
File metadata and controls
172 lines (153 loc) · 5.94 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
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.each(testCases)("in variable declaration (%p)", ({ binding, value }) => {
util.testFunction`
let ${allBindings};
{
const ${binding} = ${value};
return { ${allBindings} };
}
`.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(["[]", "{}"])("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();
});
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();
});
});