Skip to content

Commit 0d61965

Browse files
authored
Fix spread bug #898 (#936)
1 parent 54569aa commit 0d61965

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/transformation/visitors/variable-declaration.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,13 @@ export function transformBindingPattern(
3636
propertyAccessStack: ts.PropertyName[] = []
3737
): lua.Statement[] {
3838
const result: lua.Statement[] = [];
39-
const isObjectBindingPattern = ts.isObjectBindingPattern(pattern);
4039

4140
for (const [index, element] of pattern.elements.entries()) {
4241
if (ts.isOmittedExpression(element)) continue;
4342

4443
if (ts.isArrayBindingPattern(element.name) || ts.isObjectBindingPattern(element.name)) {
4544
// nested binding pattern
46-
const propertyName = isObjectBindingPattern
45+
const propertyName = ts.isObjectBindingPattern(pattern)
4746
? element.propertyName
4847
: ts.createNumericLiteral(String(index + 1));
4948

@@ -73,24 +72,37 @@ export function transformBindingPattern(
7372
continue;
7473
}
7574

76-
if (isObjectBindingPattern) {
77-
const elements = pattern.elements as ts.NodeArray<ts.BindingElement>;
78-
const usedProperties = elements.map(e =>
79-
lua.createTableFieldExpression(
80-
lua.createBooleanLiteral(true),
81-
lua.createStringLiteral(
82-
((e.propertyName ?? e.name) as ts.Identifier).text,
83-
e.propertyName ?? e.name
84-
)
85-
)
75+
if (ts.isObjectBindingPattern(pattern)) {
76+
const excludedProperties: ts.Identifier[] = [];
77+
78+
for (const element of pattern.elements) {
79+
// const { ...x } = ...;
80+
// ~~~~
81+
if (element.dotDotDotToken) continue;
82+
83+
// const { x } = ...;
84+
// ~
85+
if (ts.isIdentifier(element.name) && !element.propertyName) {
86+
excludedProperties.push(element.name);
87+
}
88+
89+
// const { x: ... } = ...;
90+
// ~~~~~~
91+
if (element.propertyName && element.name && ts.isIdentifier(element.propertyName)) {
92+
excludedProperties.push(element.propertyName);
93+
}
94+
}
95+
96+
const excludedPropertiesTable = excludedProperties.map(e =>
97+
lua.createTableFieldExpression(lua.createBooleanLiteral(true), lua.createStringLiteral(e.text, e))
8698
);
8799

88100
expression = transformLuaLibFunction(
89101
context,
90102
LuaLibFeature.ObjectRest,
91103
undefined,
92104
tableExpression,
93-
lua.createTableExpression(usedProperties)
105+
lua.createTableExpression(excludedPropertiesTable)
94106
);
95107
} else {
96108
expression = transformLuaLibFunction(
@@ -104,7 +116,7 @@ export function transformBindingPattern(
104116
} else {
105117
expression = lua.createTableIndexExpression(
106118
tableExpression,
107-
isObjectBindingPattern ? propertyName : lua.createNumericLiteral(index + 1)
119+
ts.isObjectBindingPattern(pattern) ? propertyName : lua.createNumericLiteral(index + 1)
108120
);
109121
}
110122

test/unit/destructuring.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const testCases = [
1212
{ binding: "{ ...rest }", value: {} },
1313
{ binding: "{ x, ...rest }", value: { x: "x" } },
1414
{ binding: "{ x, ...rest }", value: { x: "x", y: "y", z: "z" } },
15+
{ binding: "{ x, ...y }", value: { x: "x", y: "y", z: "z" } },
16+
{ binding: "{ x: y, ...z }", value: { x: "x", y: "y", z: "z" } },
1517

1618
{ binding: "[]", value: [] },
1719
{ binding: "[x, y]", value: ["x", "y"] },

0 commit comments

Comments
 (0)