Skip to content

Commit d9103e8

Browse files
authored
Feature/small bugfixes (#475)
* Swapped arithmetic and logical bitwise right shift operators * Fixed bug where named class expressions would return ____ * Used tstl.createAnonymousIdentifier instead of manually creating them * Switched around bitwise right shift for 5.3 again and fixed failing test * Fixed issue with class names * Fixed shift bugs * Removed bitshiftright operator * Fixed issues with string concatenation detection * Fixed remaining tests * Fixed strange operator switch
1 parent f9a225e commit d9103e8

File tree

8 files changed

+180
-138
lines changed

8 files changed

+180
-138
lines changed

src/LuaAST.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ export enum SyntaxKind {
7070
BitwiseOrOperator,
7171
BitwiseExclusiveOrOperator,
7272
BitwiseRightShiftOperator,
73-
BitwiseArithmeticRightShift,
7473
BitwiseLeftShiftOperator,
7574
BitwiseNotOperator, // Unary
7675
}
@@ -85,7 +84,7 @@ export type UnaryOperator = SyntaxKind.NegationOperator
8584

8685
export type BinaryBitwiseOperator = SyntaxKind.BitwiseAndOperator | SyntaxKind.BitwiseOrOperator
8786
| SyntaxKind.BitwiseExclusiveOrOperator | SyntaxKind.BitwiseRightShiftOperator
88-
| SyntaxKind.BitwiseArithmeticRightShift | SyntaxKind.BitwiseLeftShiftOperator;
87+
| SyntaxKind.BitwiseLeftShiftOperator;
8988

9089
export type BinaryOperator =
9190
SyntaxKind.AdditionOperator | SyntaxKind.SubractionOperator | SyntaxKind.MultiplicationOperator

src/LuaPrinter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export class LuaPrinter {
3131
[tstl.SyntaxKind.BitwiseOrOperator]: "|",
3232
[tstl.SyntaxKind.BitwiseExclusiveOrOperator]: "~",
3333
[tstl.SyntaxKind.BitwiseRightShiftOperator]: ">>",
34-
[tstl.SyntaxKind.BitwiseArithmeticRightShift]: ">>>",
3534
[tstl.SyntaxKind.BitwiseLeftShiftOperator]: "<<",
3635
[tstl.SyntaxKind.BitwiseNotOperator]: "~",
3736
};

src/LuaTransformer.ts

Lines changed: 121 additions & 99 deletions
Large diffs are not rendered by default.

src/TSHelper.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,32 +262,32 @@ export class TSHelper {
262262
return undefined;
263263
}
264264

265-
public static isBinaryAssignmentToken(token: ts.SyntaxKind): [boolean, tstl.BinaryOperator] {
265+
public static isBinaryAssignmentToken(token: ts.SyntaxKind): [boolean, ts.BinaryOperator] {
266266
switch (token) {
267267
case ts.SyntaxKind.BarEqualsToken:
268-
return [true, tstl.SyntaxKind.BitwiseOrOperator];
268+
return [true, ts.SyntaxKind.BarToken];
269269
case ts.SyntaxKind.PlusEqualsToken:
270-
return [true, tstl.SyntaxKind.AdditionOperator];
270+
return [true, ts.SyntaxKind.PlusToken];
271271
case ts.SyntaxKind.CaretEqualsToken:
272-
return [true, tstl.SyntaxKind.BitwiseExclusiveOrOperator];
272+
return [true, ts.SyntaxKind.CaretToken];
273273
case ts.SyntaxKind.MinusEqualsToken:
274-
return [true, tstl.SyntaxKind.SubractionOperator];
274+
return [true, ts.SyntaxKind.MinusToken];
275275
case ts.SyntaxKind.SlashEqualsToken:
276-
return [true, tstl.SyntaxKind.DivisionOperator];
276+
return [true, ts.SyntaxKind.SlashToken];
277277
case ts.SyntaxKind.PercentEqualsToken:
278-
return [true, tstl.SyntaxKind.ModuloOperator];
278+
return [true, ts.SyntaxKind.PercentToken];
279279
case ts.SyntaxKind.AsteriskEqualsToken:
280-
return [true, tstl.SyntaxKind.MultiplicationOperator];
280+
return [true, ts.SyntaxKind.AsteriskToken];
281281
case ts.SyntaxKind.AmpersandEqualsToken:
282-
return [true, tstl.SyntaxKind.BitwiseAndOperator];
282+
return [true, ts.SyntaxKind.AmpersandToken];
283283
case ts.SyntaxKind.AsteriskAsteriskEqualsToken:
284-
return [true, tstl.SyntaxKind.PowerOperator];
284+
return [true, ts.SyntaxKind.AsteriskAsteriskToken];
285285
case ts.SyntaxKind.LessThanLessThanEqualsToken:
286-
return [true, tstl.SyntaxKind.BitwiseLeftShiftOperator];
286+
return [true, ts.SyntaxKind.LessThanLessThanToken];
287287
case ts.SyntaxKind.GreaterThanGreaterThanEqualsToken:
288-
return [true, tstl.SyntaxKind.BitwiseRightShiftOperator];
288+
return [true, ts.SyntaxKind.GreaterThanGreaterThanToken];
289289
case ts.SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken:
290-
return [true, tstl.SyntaxKind.BitwiseArithmeticRightShift];
290+
return [true, ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken];
291291
}
292292

293293
return [false, undefined];

test/unit/class.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,22 @@ export class ClassTests {
710710
Expect(result).toBe("instance of b");
711711
}
712712

713+
@Test("Named Class Expression")
714+
public namedClassExpression(): void {
715+
const result = util.transpileAndExecute(
716+
`const a = class MyClass {
717+
public method() {
718+
return "foo";
719+
}
720+
}
721+
let inst = new a();
722+
return inst.method();`
723+
);
724+
725+
// Assert
726+
Expect(result).toBe("foo");
727+
}
728+
713729
@Test("classExpressionBaseClassMethod")
714730
public classExpressionBaseClassMethod(): void {
715731
const result = util.transpileAndExecute(

test/unit/expressions.spec.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { LuaTarget, LuaLibImportKind } from "../../src/CompilerOptions";
44

55
import * as ts from "typescript";
66
import * as util from "../src/util";
7+
import { TSTLErrors } from "../../src/TSTLErrors";
78

89
export class ExpressionTests {
910

@@ -114,10 +115,10 @@ export class ExpressionTests {
114115
@TestCase("a^=b", "a = bit.bxor(a, b);")
115116
@TestCase("a<<b", "bit.lshift(a, b);")
116117
@TestCase("a<<=b", "a = bit.lshift(a, b);")
117-
@TestCase("a>>b", "bit.rshift(a, b);")
118-
@TestCase("a>>=b", "a = bit.rshift(a, b);")
119-
@TestCase("a>>>b", "bit.arshift(a, b);")
120-
@TestCase("a>>>=b", "a = bit.arshift(a, b);")
118+
@TestCase("a>>b", "bit.arshift(a, b);")
119+
@TestCase("a>>=b", "a = bit.arshift(a, b);")
120+
@TestCase("a>>>b", "bit.rshift(a, b);")
121+
@TestCase("a>>>=b", "a = bit.rshift(a, b);")
121122
@Test("Bitop [JIT]")
122123
public bitOperatorOverrideJIT(input: string, lua: string): void {
123124
const options = { luaTarget: LuaTarget.LuaJIT, luaLibImport: LuaLibImportKind.None };
@@ -133,10 +134,10 @@ export class ExpressionTests {
133134
@TestCase("a^=b", "a = bit32.bxor(a, b);")
134135
@TestCase("a<<b", "bit32.lshift(a, b);")
135136
@TestCase("a<<=b", "a = bit32.lshift(a, b);")
136-
@TestCase("a>>b", "bit32.rshift(a, b);")
137-
@TestCase("a>>=b", "a = bit32.rshift(a, b);")
138-
@TestCase("a>>>b", "bit32.arshift(a, b);")
139-
@TestCase("a>>>=b", "a = bit32.arshift(a, b);")
137+
@TestCase("a>>b", "bit32.arshift(a, b);")
138+
@TestCase("a>>=b", "a = bit32.arshift(a, b);")
139+
@TestCase("a>>>b", "bit32.rshift(a, b);")
140+
@TestCase("a>>>=b", "a = bit32.rshift(a, b);")
140141
@Test("Bitop [5.2]")
141142
public bitOperatorOverride52(input: string, lua: string): void {
142143
const options = { luaTarget: LuaTarget.Lua52, luaLibImport: LuaLibImportKind.None };
@@ -152,20 +153,24 @@ export class ExpressionTests {
152153
@TestCase("a^=b", "a = a ~ b;")
153154
@TestCase("a<<b", "a << b;")
154155
@TestCase("a<<=b", "a = a << b;")
155-
@TestCase("a>>b", "a >> b;")
156-
@TestCase("a>>=b", "a = a >> b;")
156+
@TestCase("a>>>b", "a >> b;")
157+
@TestCase("a>>>=b", "a = a >> b;")
157158
@Test("Bitop [5.3]")
158159
public bitOperatorOverride53(input: string, lua: string): void {
159160
const options = { luaTarget: LuaTarget.Lua53, luaLibImport: LuaLibImportKind.None };
160161
Expect(util.transpileString(input, options)).toBe(lua);
161162
}
162163

163-
@TestCase("a>>>b")
164-
@TestCase("a>>>=b")
164+
@TestCase("a>>b")
165+
@TestCase("a>>=b")
165166
@Test("Unsupported bitop 5.3")
166167
public bitOperatorOverride53Unsupported(input: string): void {
167168
Expect(() => util.transpileString(input, { luaTarget: LuaTarget.Lua53, luaLibImport: LuaLibImportKind.None }))
168-
.toThrowError(TranspileError, "Bitwise >>> operator is/are not supported for target Lua 5.3.");
169+
.toThrowError(TranspileError, TSTLErrors.UnsupportedKind(
170+
"right shift operator (use >>> instead)",
171+
ts.SyntaxKind.GreaterThanGreaterThanToken,
172+
undefined
173+
).message);
169174
}
170175

171176
@TestCase("1+1", "1 + 1;")
@@ -249,7 +254,7 @@ export class ExpressionTests {
249254
@TestCase("(inst.field + 3) & 3", (8 + 3) & 3)
250255
@TestCase("inst.field | 3", 8 | 3)
251256
@TestCase("inst.field << 3", 8 << 3)
252-
@TestCase("inst.field >> 1", 8 >> 1)
257+
@TestCase("inst.field >>> 1", 8 >> 1)
253258
@TestCase("inst.field = 3", 3)
254259
@TestCase(`"abc" + inst.field`, "abc8")
255260
@Test("Get accessor expression")
@@ -278,7 +283,7 @@ export class ExpressionTests {
278283
@TestCase("&= 3", (4 & 3) + 4)
279284
@TestCase("|= 3", (4 | 3) + 4)
280285
@TestCase("<<= 3", (4 << 3) + 4)
281-
@TestCase(">>= 3", (4 >> 3) + 4)
286+
@TestCase(">>>= 3", (4 >> 3) + 4)
282287
@Test("Set accessorExpression")
283288
public setAccessorBinary(expression: string, expected: any): void {
284289
const source = `class MyClass {`

test/unit/math.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class MathTests {
5151
@TestCase("x &= y", "x=2;y=6")
5252
@TestCase("x ^= y", "x=5;y=6")
5353
@TestCase("x <<= y", "x=192;y=6")
54-
@TestCase("x >>= y", "x=0;y=6")
54+
@TestCase("x >>>= y", "x=0;y=6")
5555
@Test("Operator assignment statements")
5656
public opAssignmentStatement(statement: string, expected: string): void {
5757
const result = util.transpileAndExecute(
@@ -76,7 +76,7 @@ export class MathTests {
7676
@TestCase("o.p &= a[0]", "o=2;a=6")
7777
@TestCase("o.p ^= a[0]", "o=5;a=6")
7878
@TestCase("o.p <<= a[0]", "o=192;a=6")
79-
@TestCase("o.p >>= a[0]", "o=0;a=6")
79+
@TestCase("o.p >>>= a[0]", "o=0;a=6")
8080
@Test("Operator assignment to simple property statements")
8181
public opSimplePropAssignmentStatement(statement: string, expected: string): void {
8282
const result = util.transpileAndExecute(
@@ -101,7 +101,7 @@ export class MathTests {
101101
@TestCase("o.p.d &= a[0][0]", "o=2;a=[6,11],[7,13]")
102102
@TestCase("o.p.d ^= a[0][0]", "o=5;a=[6,11],[7,13]")
103103
@TestCase("o.p.d <<= a[0][0]", "o=192;a=[6,11],[7,13]")
104-
@TestCase("o.p.d >>= a[0][0]", "o=0;a=[6,11],[7,13]")
104+
@TestCase("o.p.d >>>= a[0][0]", "o=0;a=[6,11],[7,13]")
105105
@Test("Operator assignment to deep property statements")
106106
public opDeepPropAssignmentStatement(statement: string, expected: string): void {
107107
const result = util.transpileAndExecute(
@@ -126,7 +126,7 @@ export class MathTests {
126126
@TestCase("of().p &= af()[i()]", "o=2;a=6")
127127
@TestCase("of().p ^= af()[i()]", "o=5;a=6")
128128
@TestCase("of().p <<= af()[i()]", "o=192;a=6")
129-
@TestCase("of().p >>= af()[i()]", "o=0;a=6")
129+
@TestCase("of().p >>>= af()[i()]", "o=0;a=6")
130130
@Test("Operator assignment to complex property statements")
131131
public opComplexPropAssignmentStatement(statement: string, expected: string): void {
132132
const result = util.transpileAndExecute(
@@ -154,7 +154,7 @@ export class MathTests {
154154
@TestCase("of().p.d &= af()[i()][i()]", "o=2;a=[7,6],[11,13];i=2")
155155
@TestCase("of().p.d ^= af()[i()][i()]", "o=5;a=[7,6],[11,13];i=2")
156156
@TestCase("of().p.d <<= af()[i()][i()]", "o=192;a=[7,6],[11,13];i=2")
157-
@TestCase("of().p.d >>= af()[i()][i()]", "o=0;a=[7,6],[11,13];i=2")
157+
@TestCase("of().p.d >>>= af()[i()][i()]", "o=0;a=[7,6],[11,13];i=2")
158158
@Test("Operator assignment to complex deep property statements")
159159
public opComplexDeepPropAssignmentStatement(statement: string, expected: string): void {
160160
const result = util.transpileAndExecute(
@@ -183,7 +183,7 @@ export class MathTests {
183183
@TestCase("x &= y", "2;x=2;y=6")
184184
@TestCase("x ^= y", "5;x=5;y=6")
185185
@TestCase("x <<= y", "192;x=192;y=6")
186-
@TestCase("x >>= y", "0;x=0;y=6")
186+
@TestCase("x >>>= y", "0;x=0;y=6")
187187
@TestCase("x + (y += 7)", "16;x=3;y=13")
188188
@TestCase("x + (y += 7)", "16;x=3;y=13")
189189
@TestCase("x++ + (y += 7)", "16;x=4;y=13")
@@ -211,7 +211,7 @@ export class MathTests {
211211
@TestCase("o.p &= a[0]", "2;o=2;a=6")
212212
@TestCase("o.p ^= a[0]", "5;o=5;a=6")
213213
@TestCase("o.p <<= a[0]", "192;o=192;a=6")
214-
@TestCase("o.p >>= a[0]", "0;o=0;a=6")
214+
@TestCase("o.p >>>= a[0]", "0;o=0;a=6")
215215
@TestCase("o.p + (a[0] += 7)", "16;o=3;a=13")
216216
@TestCase("o.p += (a[0] += 7)", "16;o=16;a=13")
217217
@TestCase("o.p++ + (a[0] += 7)", "16;o=4;a=13")
@@ -239,7 +239,7 @@ export class MathTests {
239239
@TestCase("of().p &= af()[i()]", "2;o=2;a=6")
240240
@TestCase("of().p ^= af()[i()]", "5;o=5;a=6")
241241
@TestCase("of().p <<= af()[i()]", "192;o=192;a=6")
242-
@TestCase("of().p >>= af()[i()]", "0;o=0;a=6")
242+
@TestCase("of().p >>>= af()[i()]", "0;o=0;a=6")
243243
@TestCase("of().p + (af()[i()] += 7)", "16;o=3;a=13")
244244
@TestCase("of().p += (af()[i()] += 7)", "16;o=16;a=13")
245245
@TestCase("of().p++ + (af()[i()] += 7)", "16;o=4;a=13")

tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"no-construct": true,
3535
"no-debugger": true,
3636
"no-default-export": true,
37+
"no-duplicate-switch-case": true,
3738
"no-duplicate-variable": true,
3839
"no-inferrable-types": true,
3940
"no-namespace": [true, "allow-declarations"],

0 commit comments

Comments
 (0)