Skip to content

Commit a8589f1

Browse files
tomblindPerryvw
authored andcommitted
support in transformer for assignments (#299)
* support in transformer for assignments * fixes for feedback and issues found in tests
1 parent 71ecb56 commit a8589f1

File tree

5 files changed

+459
-54
lines changed

5 files changed

+459
-54
lines changed

src/LuaNode.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as ts from "typescript";
2+
3+
export enum LuaSyntaxKind {
4+
ConcatExpression = 10000,
5+
FunctionCallExpression,
6+
MethodCallExpression,
7+
}
8+
9+
export interface LuaNode extends ts.Node {
10+
luaKind: LuaSyntaxKind;
11+
}
12+
13+
export type LuaConcatExpression = LuaNode & ts.BinaryExpression;
14+
export type LuaCallExpression = LuaNode & ts.CallExpression;

src/TSHelper.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as ts from "typescript";
22
import { Decorator, DecoratorKind } from "./Decorator";
3+
import { TSTLErrors } from "./Errors";
34

45
export class TSHelper {
56

@@ -220,6 +221,26 @@ export class TSHelper {
220221
return [false, null];
221222
}
222223

224+
public static isCompoundPrefixUnaryOperator(node: ts.PrefixUnaryExpression): boolean {
225+
return node.operator !== ts.SyntaxKind.ExclamationToken
226+
&& node.operator !== ts.SyntaxKind.MinusToken
227+
&& node.operator !== ts.SyntaxKind.PlusToken
228+
&& node.operator !== ts.SyntaxKind.TildeToken;
229+
}
230+
231+
public static getUnaryCompoundAssignmentOperator(node: ts.PrefixUnaryExpression | ts.PostfixUnaryExpression)
232+
: ts.BinaryOperator {
233+
switch (node.operator) {
234+
case ts.SyntaxKind.PlusPlusToken:
235+
return ts.SyntaxKind.PlusToken;
236+
case ts.SyntaxKind.MinusMinusToken:
237+
return ts.SyntaxKind.MinusToken;
238+
default:
239+
throw TSTLErrors.UnsupportedKind(
240+
`unary ${ts.isPrefixUnaryExpression(node) ? "prefix" : "postfix"} operator`, node.operator, node);
241+
}
242+
}
243+
223244
public static isExpressionStatement(node: ts.Expression): boolean {
224245
return node.parent === undefined
225246
|| ts.isExpressionStatement(node.parent)

src/TransformHelper.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as ts from "typescript";
2+
import { LuaCallExpression, LuaConcatExpression, LuaNode, LuaSyntaxKind } from "./LuaNode";
23

34
export class TransformHelper {
45
// Helper to create simple lua variable statement;
@@ -18,6 +19,24 @@ export class TransformHelper {
1819
return this.createLuaVariableStatement(identifier, requireCall);
1920
}
2021

22+
public static createLuaConcatExpression(left: ts.Expression, right: ts.Expression): LuaConcatExpression {
23+
const node = ts.createAdd(left, right) as LuaConcatExpression;
24+
node.luaKind = LuaSyntaxKind.ConcatExpression;
25+
return node;
26+
}
27+
28+
public static createLuaCallExpression(expression: ts.Expression,
29+
argumentsArray: ReadonlyArray<ts.Expression>,
30+
isMethod: boolean): LuaCallExpression {
31+
const node = ts.createCall(expression, undefined, argumentsArray) as LuaCallExpression;
32+
node.luaKind = isMethod ? LuaSyntaxKind.MethodCallExpression : LuaSyntaxKind.FunctionCallExpression;
33+
return node;
34+
}
35+
36+
public static isLuaNode<T extends ts.Node>(node: T): node is LuaNode & T {
37+
return (node as LuaNode & T).luaKind !== undefined;
38+
}
39+
2140
public static flatten<T>(arr: T[]): T[] {
2241
const flat = [].concat(...arr);
2342
return flat.some(Array.isArray) ? this.flatten(flat) : flat;

0 commit comments

Comments
 (0)