Skip to content

Commit 4dc3753

Browse files
authored
Implemented DeleteExpression and NonNullExpression, fixed some broken tests (#330)
* Implemented DeleteExpression and NonNullExpression, fixed some broken tests * Return true from delete expression * Delete statement shortcut
1 parent 81f1dd3 commit 4dc3753

5 files changed

Lines changed: 97 additions & 27 deletions

File tree

src/LuaTransformer.ts

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,15 @@ export class LuaTransformer {
10311031
);
10321032
}
10331033

1034+
else if (ts.isDeleteExpression(expression)) {
1035+
return tstl.createAssignmentStatement(
1036+
this.transformExpression(expression.expression) as tstl.IdentifierOrTableIndexExpression,
1037+
tstl.createNilLiteral(),
1038+
undefined,
1039+
expression
1040+
);
1041+
}
1042+
10341043
return tstl.createExpressionStatement(this.transformExpression(expression));
10351044
}
10361045

@@ -1401,6 +1410,8 @@ export class LuaTransformer {
14011410
return this.transformArrayLiteral(expression as ts.ArrayLiteralExpression);
14021411
case ts.SyntaxKind.ObjectLiteralExpression:
14031412
return this.transformObjectLiteral(expression as ts.ObjectLiteralExpression);
1413+
case ts.SyntaxKind.DeleteExpression:
1414+
return this.transformDeleteExpression(expression as ts.DeleteExpression);
14041415
case ts.SyntaxKind.FunctionExpression:
14051416
return this.transformFunctionExpression(expression as ts.ArrowFunction, this.selfIdentifier);
14061417
case ts.SyntaxKind.ArrowFunction:
@@ -1429,6 +1440,10 @@ export class LuaTransformer {
14291440
return this.transformAssertionExpression(expression as ts.AssertionExpression);
14301441
case ts.SyntaxKind.TypeOfExpression:
14311442
return this.transformTypeOfExpression(expression as ts.TypeOfExpression);
1443+
case ts.SyntaxKind.SpreadElement:
1444+
throw new Error("Not yet implemented");
1445+
case ts.SyntaxKind.NonNullExpression:
1446+
return this.transformExpression((expression as ts.NonNullExpression).expression);
14321447
case ts.SyntaxKind.EmptyStatement:
14331448
// TODO move to extra function (consistency)
14341449
return undefined;
@@ -1618,7 +1633,10 @@ export class LuaTransformer {
16181633
tstl.createAssignmentStatement(left as tstl.IdentifierOrTableIndexExpression[], tmps),
16191634
];
16201635
return this.createImmediatelyInvokedFunctionExpression(
1621-
statements, tstl.createTableExpression(tmps.map(t => tstl.createTableFieldExpression(t))));
1636+
statements,
1637+
tstl.createTableExpression(tmps.map(t => tstl.createTableFieldExpression(t))),
1638+
expression
1639+
);
16221640
}
16231641

16241642
if (ts.isPropertyAccessExpression(expression.left) || ts.isElementAccessExpression(expression.left)) {
@@ -1660,7 +1678,8 @@ export class LuaTransformer {
16601678
const right = this.transformExpression(expression.right);
16611679
return this.createImmediatelyInvokedFunctionExpression(
16621680
[this.transformAssignment(expression.left, right)],
1663-
left
1681+
left,
1682+
expression
16641683
);
16651684
}
16661685
}
@@ -1721,7 +1740,8 @@ export class LuaTransformer {
17211740
// return ____TS_tmp
17221741
return this.createImmediatelyInvokedFunctionExpression(
17231742
[objAndIndexDeclaration, tmpDeclaration, assignStatement],
1724-
tmp
1743+
tmp,
1744+
lhs.parent
17251745
);
17261746

17271747
} else if (isPostfix) {
@@ -1738,7 +1758,11 @@ export class LuaTransformer {
17381758
replacementOperator
17391759
);
17401760
const assignStatement = this.transformAssignment(lhs, operatorExpression);
1741-
return this.createImmediatelyInvokedFunctionExpression([tmpDeclaration, assignStatement], tmpIdentifier);
1761+
return this.createImmediatelyInvokedFunctionExpression(
1762+
[tmpDeclaration, assignStatement],
1763+
tmpIdentifier,
1764+
lhs.parent
1765+
);
17421766

17431767
} else if (ts.isPropertyAccessExpression(lhs) || ts.isElementAccessExpression(lhs)) {
17441768
// Simple property/element access expressions need to cache in temp to avoid double-evaluation
@@ -1749,14 +1773,18 @@ export class LuaTransformer {
17491773
const operatorExpression = this.transformBinaryOperation(lhs.parent, left, right, replacementOperator);
17501774
const tmpDeclaration = tstl.createVariableDeclarationStatement(tmpIdentifier, operatorExpression);
17511775
const assignStatement = this.transformAssignment(lhs, tmpIdentifier);
1752-
return this.createImmediatelyInvokedFunctionExpression([tmpDeclaration, assignStatement], tmpIdentifier);
1776+
return this.createImmediatelyInvokedFunctionExpression(
1777+
[tmpDeclaration, assignStatement],
1778+
tmpIdentifier,
1779+
lhs.parent
1780+
);
17531781

17541782
} else {
17551783
// Simple expressions
17561784
// ${left} = ${right}; return ${right}
17571785
const operatorExpression = this.transformBinaryOperation(lhs.parent, left, right, replacementOperator);
17581786
const assignStatement = this.transformAssignment(lhs, operatorExpression);
1759-
return this.createImmediatelyInvokedFunctionExpression([assignStatement], left);
1787+
return this.createImmediatelyInvokedFunctionExpression([assignStatement], left, lhs.parent);
17601788
}
17611789
}
17621790

@@ -2030,6 +2058,22 @@ export class LuaTransformer {
20302058
return tstl.createTableExpression(properties, undefined, node);
20312059
}
20322060

2061+
public transformDeleteExpression(expression: ts.DeleteExpression): tstl.CallExpression {
2062+
const lhs = this.transformExpression(expression.expression) as tstl.IdentifierOrTableIndexExpression;
2063+
const assignment = tstl.createAssignmentStatement(
2064+
lhs,
2065+
tstl.createNilLiteral(),
2066+
undefined,
2067+
expression
2068+
);
2069+
2070+
return this.createImmediatelyInvokedFunctionExpression(
2071+
[assignment],
2072+
[tstl.createBooleanLiteral(true)],
2073+
expression
2074+
);
2075+
}
2076+
20332077
public transformFunctionExpression(
20342078
node: ts.FunctionLikeDeclaration,
20352079
context: tstl.Identifier | undefined
@@ -2275,7 +2319,7 @@ export class LuaTransformer {
22752319
const selfAssignment = this.createLocalOrGlobalDeclaration(selfIdentifier, context);
22762320
const index = tstl.createTableIndexExpression(selfIdentifier, argument);
22772321
const callExpression = tstl.createCallExpression(index, parameters);
2278-
return this.createImmediatelyInvokedFunctionExpression([selfAssignment], callExpression);
2322+
return this.createImmediatelyInvokedFunctionExpression([selfAssignment], callExpression, node);
22792323
} else {
22802324
return tstl.createCallExpression(this.transformExpression(node.expression), [context, ...parameters]);
22812325
}
@@ -2792,13 +2836,14 @@ export class LuaTransformer {
27922836

27932837
public createImmediatelyInvokedFunctionExpression(
27942838
statements: tstl.Statement[],
2795-
result: tstl.Expression | tstl.Expression[]
2839+
result: tstl.Expression | tstl.Expression[],
2840+
tsOriginal: ts.Node
27962841
): tstl.CallExpression
27972842
{
27982843
const body = statements ? statements.slice(0) : [];
27992844
body.push(tstl.createReturnStatement(Array.isArray(result) ? result : [result]));
28002845
const iife = tstl.createFunctionExpression(tstl.createBlock(body));
2801-
return tstl.createCallExpression(tstl.createParenthesizedExpression(iife));
2846+
return tstl.createCallExpression(tstl.createParenthesizedExpression(iife), [], undefined, tsOriginal);
28022847
}
28032848

28042849
public createUnpackCall(expression: tstl.Expression): tstl.Expression {

test/unit/array.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,43 @@ export class ArrayTests {
5757
const result = util.executeLua(lua);
5858
Expect(result).toBe(expected);
5959
}
60+
61+
@Test("Array delete")
62+
public arrayDelete(): void {
63+
const lua = util.transpileString(
64+
`const myarray = [1,2,3,4];
65+
delete myarray[2];
66+
return \`\${myarray[0]},\${myarray[1]},\${myarray[2]},\${myarray[3]}\`;`
67+
);
68+
69+
const result = util.executeLua(lua);
70+
71+
Expect(result).toBe("1,2,nil,4");
72+
}
73+
74+
@Test("Array delete return true")
75+
public arrayDeleteReturnTrue(): void {
76+
const lua = util.transpileString(
77+
`const myarray = [1,2,3,4];
78+
const exists = delete myarray[2];
79+
return \`\${exists}:\${myarray[0]},\${myarray[1]},\${myarray[2]},\${myarray[3]}\`;`
80+
);
81+
82+
const result = util.executeLua(lua);
83+
84+
Expect(result).toBe("true:1,2,nil,4");
85+
}
86+
87+
@Test("Array delete return false")
88+
public arrayDeleteReturnFalse(): void {
89+
const lua = util.transpileString(
90+
`const myarray = [1,2,3,4];
91+
const exists = delete myarray[4];
92+
return \`\${exists}:\${myarray[0]},\${myarray[1]},\${myarray[2]},\${myarray[3]}\`;`
93+
);
94+
95+
const result = util.executeLua(lua);
96+
97+
Expect(result).toBe("true:1,2,3,4");
98+
}
6099
}

test/unit/curry.spec.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,10 @@ import * as util from "../src/util";
33

44
export class LuaCurryTests {
55

6-
@Test("currying")
7-
public currying() {
8-
// Transpile
9-
const lua = util.transpileString(
10-
`(x: number) => (y: number) => x + y;`
11-
);
12-
// Assert
13-
Expect(lua).toBe(`function (x)
14-
return function(y)
15-
return x + y
16-
end
17-
end;`);
18-
}
19-
206
@Test("curryingAdd")
217
@TestCase(2, 3)
228
@TestCase(5, 4)
23-
public curryingAdd(x: number, y: number) {
9+
public curryingAdd(x: number, y: number): void {
2410
// Transpile
2511
const lua = util.transpileString(
2612
`let add = (x: number) => (y: number) => x + y;

test/unit/expressions.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export class ExpressionTests {
1313
@TestCase("--i", "i = i - 1;")
1414
@TestCase("!a", "not a;")
1515
@TestCase("-a", "-a;")
16-
@TestCase("delete tbl['test']", "tbl[\"test\"]=nil;")
17-
@TestCase("delete tbl.test", "tbl.test=nil;")
16+
@TestCase("delete tbl['test']", "(function ()\n tbl.test = nil;\n return ;\nend)();")
17+
@TestCase("delete tbl.test", "(function ()\n tbl.test = nil;\n return ;\nend)();")
1818
@Test("Unary expressions basic")
1919
public unaryBasic(input: string, lua: string): void {
2020
Expect(util.transpileString(input)).toBe(lua);

test/unit/functions.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Expect, Test, TestCase } from "alsatian";
1+
import { Expect, Test, TestCase, FocusTest } from "alsatian";
22

33
import * as ts from "typescript";
44
import * as util from "../src/util";

0 commit comments

Comments
 (0)