Skip to content

Commit dd44532

Browse files
Add in operator
1 parent 5a0936b commit dd44532

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/Transpiler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,9 @@ export class LuaTranspiler {
687687
case ts.SyntaxKind.ExclamationEqualsEqualsToken:
688688
result = `${lhs}~=${rhs}`;
689689
break;
690+
case ts.SyntaxKind.InKeyword:
691+
result = `${rhs}[${lhs}]~=nil`;
692+
break;
690693
default:
691694
throw new TranspileError("Unsupported binary operator kind: " + ts.tokenToString(node.operatorToken.kind), node);
692695
}

test/unit/expressions.spec.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export class ExpressionTests {
2222

2323
@TestCase("obj instanceof someClass", "Unsupported binary operator kind: instanceof")
2424
@TestCase("typeof obj", "Unsupported expression kind: TypeOfExpression")
25-
@TestCase("2 in obj", "Unsupported binary operator kind: in")
2625
@Test("Prohibted Expressions")
2726
public prohibtedExpressions(input: string, expectedError: string) {
2827
Expect(() => {
@@ -47,8 +46,32 @@ export class ExpressionTests {
4746
@TestCase("1&&1", "1 and 1")
4847
@TestCase("1||1", "1 or 1")
4948
@Test("Binary expressions basic")
50-
public binary(input: string, lua: string) {
51-
Expect(util.transpileString(input)).toBe(lua);
49+
public binary(input: string, output: string) {
50+
// Transpile
51+
const lua = util.transpileString(input);
52+
53+
// Execute
54+
const result = util.executeLua(`return ${lua}`);
55+
56+
// Assert
57+
Expect(lua).toBe(output);
58+
Expect(result).toBe(eval(input));
59+
}
60+
61+
@TestCase("'key' in obj")
62+
@TestCase("'existingKey' in obj")
63+
@TestCase("0 in obj")
64+
@TestCase("9 in obj")
65+
@Test("Binary expression in")
66+
public binaryIn(input: string) {
67+
// Transpile
68+
const lua = util.transpileString(input);
69+
70+
// Execute
71+
const result = util.executeLua(`obj = { existingKey = 1 }\nreturn ${lua}`);
72+
73+
// Assert
74+
Expect(result).toBe(eval(`let obj = { existingKey: 1 }; ${input}`));
5275
}
5376

5477
@TestCase("a+=b", "a=a+b")

0 commit comments

Comments
 (0)