Skip to content

Commit 3fa2065

Browse files
hazzard993Perryvw
authored andcommitted
Omitted expression support (#643)
* Omitted expression support * Add test for result of omitted expression * Check the contents of the same array * Neaten up test * Support OmittedExpressions in array binding assignments * Make transformOmittedExpression context aware and use transformBindingExpression
1 parent 51a125e commit 3fa2065

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/LuaTransformer.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,6 +2683,8 @@ export class LuaTransformer {
26832683
return this.transformArrayLiteral(expression as ts.ArrayLiteralExpression);
26842684
case ts.SyntaxKind.ObjectLiteralExpression:
26852685
return this.transformObjectLiteral(expression as ts.ObjectLiteralExpression);
2686+
case ts.SyntaxKind.OmittedExpression:
2687+
return this.transformOmittedExpression(expression as ts.OmittedExpression);
26862688
case ts.SyntaxKind.DeleteExpression:
26872689
return this.transformDeleteExpression(expression as ts.DeleteExpression);
26882690
case ts.SyntaxKind.FunctionExpression:
@@ -2909,7 +2911,7 @@ export class LuaTransformer {
29092911
// Destructuring assignment
29102912
const left =
29112913
expression.left.elements.length > 0
2912-
? expression.left.elements.map(e => this.transformExpression(e))
2914+
? expression.left.elements.map(e => this.transformArrayBindingExpression(e))
29132915
: [tstl.createAnonymousIdentifier(expression.left)];
29142916
let right: tstl.Expression[];
29152917
if (ts.isArrayLiteralExpression(expression.right)) {
@@ -3466,6 +3468,11 @@ export class LuaTransformer {
34663468
return tstl.createTableExpression(properties, expression);
34673469
}
34683470

3471+
public transformOmittedExpression(node: ts.OmittedExpression): ExpressionVisitResult {
3472+
const isWithinBindingAssignmentStatement = tsHelper.isWithinLiteralAssignmentStatement(node);
3473+
return isWithinBindingAssignmentStatement ? tstl.createAnonymousIdentifier() : tstl.createNilLiteral(node);
3474+
}
3475+
34693476
public transformDeleteExpression(expression: ts.DeleteExpression): ExpressionVisitResult {
34703477
const lhs = this.transformExpression(expression.expression) as tstl.AssignmentLeftHandSideExpression;
34713478
const assignment = tstl.createAssignmentStatement(lhs, tstl.createNilLiteral(), expression);
@@ -4577,14 +4584,18 @@ export class LuaTransformer {
45774584
}
45784585

45794586
public transformArrayBindingElement(name: ts.ArrayBindingElement): ExpressionVisitResult {
4587+
return this.transformArrayBindingExpression(name as ts.Expression);
4588+
}
4589+
4590+
public transformArrayBindingExpression(name: ts.Expression): ExpressionVisitResult {
45804591
if (ts.isOmittedExpression(name)) {
4581-
return tstl.createIdentifier("__", name);
4592+
return this.transformOmittedExpression(name);
45824593
} else if (ts.isIdentifier(name)) {
45834594
return this.transformIdentifier(name);
45844595
} else if (ts.isBindingElement(name) && ts.isIdentifier(name.name)) {
45854596
return this.transformIdentifier(name.name);
45864597
} else {
4587-
throw TSTLErrors.UnsupportedKind("array binding element", name.kind, name);
4598+
throw TSTLErrors.UnsupportedKind("array binding expression", name.kind, name);
45884599
}
45894600
}
45904601

src/TSHelper.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,19 @@ export function isEnumMember(
794794
}
795795
}
796796

797+
export function isWithinLiteralAssignmentStatement(node: ts.Node): boolean {
798+
if (!node.parent) {
799+
return false;
800+
}
801+
if (ts.isArrayLiteralExpression(node.parent) || ts.isObjectLiteralExpression(node.parent)) {
802+
return isWithinLiteralAssignmentStatement(node.parent);
803+
} else if (ts.isBinaryExpression(node.parent) && node.parent.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
804+
return true;
805+
} else {
806+
return false;
807+
}
808+
}
809+
797810
export function moduleHasEmittedBody(
798811
statement: ts.ModuleDeclaration
799812
): statement is ts.ModuleDeclaration & { body: ts.ModuleBlock | ts.ModuleDeclaration } {

test/unit/array.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,22 @@ test.each([
191191
`;
192192
expect(() => util.transpileAndExecute(code)).toThrowError(`invalid array length: ${result}`);
193193
});
194+
195+
test.each([0, 1, 2])("Array with OmittedExpression", index => {
196+
const result = util.transpileAndExecute(
197+
`const myarray = [1, , 2];
198+
return myarray[${index}];`
199+
);
200+
201+
expect(result).toBe([1, , 2][index]);
202+
});
203+
204+
test("OmittedExpression in Array Binding Assignment Statement", () => {
205+
const result = util.transpileAndExecute(
206+
`let a, c;
207+
[a, , c] = [1, 2, 3];
208+
return a + c;`
209+
);
210+
211+
expect(result).toBe(4);
212+
});

0 commit comments

Comments
 (0)