Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions 40 src/transformation/visitors/variable-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ export function transformBindingPattern(
propertyAccessStack: ts.PropertyName[] = []
): lua.Statement[] {
const result: lua.Statement[] = [];
const isObjectBindingPattern = ts.isObjectBindingPattern(pattern);

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

if (ts.isArrayBindingPattern(element.name) || ts.isObjectBindingPattern(element.name)) {
// nested binding pattern
const propertyName = isObjectBindingPattern
const propertyName = ts.isObjectBindingPattern(pattern)
? element.propertyName
: ts.createNumericLiteral(String(index + 1));

Expand Down Expand Up @@ -73,24 +72,37 @@ export function transformBindingPattern(
continue;
}

if (isObjectBindingPattern) {
const elements = pattern.elements as ts.NodeArray<ts.BindingElement>;
const usedProperties = elements.map(e =>
lua.createTableFieldExpression(
lua.createBooleanLiteral(true),
lua.createStringLiteral(
((e.propertyName ?? e.name) as ts.Identifier).text,
e.propertyName ?? e.name
)
)
if (ts.isObjectBindingPattern(pattern)) {
const excludedProperties: ts.Identifier[] = [];

for (const element of pattern.elements) {
// const { ...x } = ...;
// ~~~~
if (element.dotDotDotToken) continue;

// const { x } = ...;
// ~
if (ts.isIdentifier(element.name) && !element.propertyName) {
excludedProperties.push(element.name);
}

// const { x: ... } = ...;
// ~~~~~~
if (element.propertyName && element.name && ts.isIdentifier(element.propertyName)) {
excludedProperties.push(element.propertyName);
}
}

const excludedPropertiesTable = excludedProperties.map(e =>
lua.createTableFieldExpression(lua.createBooleanLiteral(true), lua.createStringLiteral(e.text, e))
);

expression = transformLuaLibFunction(
context,
LuaLibFeature.ObjectRest,
undefined,
tableExpression,
lua.createTableExpression(usedProperties)
lua.createTableExpression(excludedPropertiesTable)
);
} else {
expression = transformLuaLibFunction(
Expand All @@ -104,7 +116,7 @@ export function transformBindingPattern(
} else {
expression = lua.createTableIndexExpression(
tableExpression,
isObjectBindingPattern ? propertyName : lua.createNumericLiteral(index + 1)
ts.isObjectBindingPattern(pattern) ? propertyName : lua.createNumericLiteral(index + 1)
);
}

Expand Down
2 changes: 2 additions & 0 deletions 2 test/unit/destructuring.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const testCases = [
{ 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"] },
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.