Skip to content

Commit 1741286

Browse files
martinjlowmPerryvw
authored andcommitted
Support omitted declarations for destructured tuples (#682)
1 parent f9f5fd7 commit 1741286

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/LuaTransformer.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,9 @@ export class LuaTransformer {
19431943
// For nested bindings and object bindings, fall back to transformBindingPattern
19441944
if (
19451945
ts.isObjectBindingPattern(statement.name) ||
1946-
statement.name.elements.some(elem => !ts.isBindingElement(elem) || !ts.isIdentifier(elem.name))
1946+
statement.name.elements.some(
1947+
elem => (!ts.isBindingElement(elem) || !ts.isIdentifier(elem.name)) && !ts.isOmittedExpression(elem)
1948+
)
19471949
) {
19481950
const statements = [];
19491951
let table: tstl.Identifier;
@@ -1968,7 +1970,13 @@ export class LuaTransformer {
19681970
}
19691971

19701972
// Disallow ellipsis destruction
1971-
if (statement.name.elements.some(elem => !ts.isBindingElement(elem) || elem.dotDotDotToken !== undefined)) {
1973+
if (
1974+
statement.name.elements.some(
1975+
elem =>
1976+
(!ts.isBindingElement(elem) || elem.dotDotDotToken !== undefined) &&
1977+
!ts.isOmittedExpression(elem)
1978+
)
1979+
) {
19721980
throw TSTLErrors.ForbiddenEllipsisDestruction(statement);
19731981
}
19741982

src/TSHelper.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,8 +832,14 @@ export function isWithinLiteralAssignmentStatement(node: ts.Node): boolean {
832832
if (!node.parent) {
833833
return false;
834834
}
835-
if (ts.isArrayLiteralExpression(node.parent) || ts.isObjectLiteralExpression(node.parent)) {
835+
if (
836+
ts.isArrayLiteralExpression(node.parent) ||
837+
ts.isArrayBindingPattern(node.parent) ||
838+
ts.isObjectLiteralExpression(node.parent)
839+
) {
836840
return isWithinLiteralAssignmentStatement(node.parent);
841+
} else if (isInDestructingAssignment(node)) {
842+
return true;
837843
} else if (ts.isBinaryExpression(node.parent) && node.parent.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
838844
return true;
839845
} else {

test/unit/tuples.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ test("Tuple Return Destruct Declaration", () => {
122122
const code = `
123123
/** @tupleReturn */
124124
function tuple(): [number, number, number] { return [3,5,1]; }
125-
const [a,b,c] = tuple();
125+
const [,b,c] = tuple();
126126
return b;`;
127127

128128
const lua = util.transpileString(code);

0 commit comments

Comments
 (0)