Skip to content
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
76 changes: 61 additions & 15 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1370,14 +1370,26 @@ export class LuaTransformer {
const expression = isObjectBindingPattern
? tstl.createTableIndexExpression(tableExpression, tstl.createStringLiteral(propertyName.text))
: tstl.createTableIndexExpression(tableExpression, tstl.createNumericLiteral(index + 1));
yield* this.createLocalOrExportedOrGlobalDeclaration(variableName, expression);
if (element.initializer) {
const defaultExpression = tstl.createBinaryExpression(expression,
this.expectExpression(this.transformExpression(element.initializer)),
tstl.SyntaxKind.OrOperator
const identifier = this.shouldExportIdentifier(variableName)
? this.createExportedIdentifier(variableName)
: variableName;
yield tstl.createIfStatement(
tstl.createBinaryExpression(
identifier,
tstl.createNilLiteral(),
tstl.SyntaxKind.EqualityOperator
),
tstl.createBlock(
[
tstl.createAssignmentStatement(
identifier,
this.transformExpression(element.initializer)
),
]
)
);
yield* this.createLocalOrExportedOrGlobalDeclaration(variableName, defaultExpression);
} else {
yield* this.createLocalOrExportedOrGlobalDeclaration(variableName, expression);
}
}
}
Expand Down Expand Up @@ -1802,6 +1814,8 @@ export class LuaTransformer {
} else if (ts.isArrayBindingPattern(statement.name) || ts.isObjectBindingPattern(statement.name)) {
// Destructuring types

const statements: tstl.Statement[] = [];

// For nested bindings and object bindings, fall back to transformBindingPattern
if (ts.isObjectBindingPattern(statement.name)
|| statement.name.elements.some(elem => !ts.isBindingElement(elem) || !ts.isIdentifier(elem.name))) {
Expand Down Expand Up @@ -1835,26 +1849,58 @@ export class LuaTransformer {
// Don't unpack TupleReturn decorated functions
if (statement.initializer) {
if (tsHelper.isTupleReturnCall(statement.initializer, this.checker)) {
return this.createLocalOrExportedOrGlobalDeclaration(
vars,
this.transformExpression(statement.initializer),
statement
statements.push(
...this.createLocalOrExportedOrGlobalDeclaration(
vars,
this.transformExpression(statement.initializer),
statement
)
);
} else {
// local vars = this.transpileDestructingAssignmentValue(node.initializer);
const initializer = this.createUnpackCall(
this.expectExpression(this.transformExpression(statement.initializer)),
statement.initializer
);
return this.createLocalOrExportedOrGlobalDeclaration(vars, initializer, statement);
statements.push(...this.createLocalOrExportedOrGlobalDeclaration(vars, initializer, statement));
}
} else {
return this.createLocalOrExportedOrGlobalDeclaration(
vars,
tstl.createNilLiteral(),
statement
statements.push(
...this.createLocalOrExportedOrGlobalDeclaration(
vars,
tstl.createNilLiteral(),
statement
)
);
}

statement.name.elements.forEach(element => {
if (!ts.isOmittedExpression(element) && element.initializer) {
const variableName = this.transformIdentifier(element.name as ts.Identifier);
const identifier = this.shouldExportIdentifier(variableName)
? this.createExportedIdentifier(variableName)
: variableName;
statements.push(
tstl.createIfStatement(
tstl.createBinaryExpression(
identifier,
tstl.createNilLiteral(),
tstl.SyntaxKind.EqualityOperator
),
tstl.createBlock(
[
tstl.createAssignmentStatement(
identifier,
this.transformExpression(element.initializer)
),
]
)
)
);
}
});

return statements;
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/unit/bindingpatterns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,22 @@ test.each(testCases)(
expect(result).toBe(true);
},
);

test.each([
{ bindingString: "{x, y = true}", objectString: "{x: false, y: false}", returnVariable: "y" },
{
bindingString: "{x, y: [z = true]}",
objectString: "{x: false, y: [false]}",
returnVariable: "z",
},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test with simply the pattern [x = false] too?

Copy link
Copy Markdown
Contributor Author

@hazzard993 hazzard993 Apr 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array binding pattern default parameters don't work in variable declaration statements. I'll add this with another commit, doesn't look like it resulted in much more code to review

{ bindingString: "[x = true]", objectString: "[false]", returnVariable: "x" },
])(
"Binding patterns handle false correctly (%p)",
({ bindingString, objectString, returnVariable }) => {
const result = util.transpileExecuteAndReturnExport(
`export const ${bindingString} = ${objectString};`,
returnVariable,
);
expect(result).toBe(false);
},
);