Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/LuaNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as ts from "typescript";

export enum LuaSyntaxKind {
ConcatExpression = 10000,
FunctionCallExpression,
MethodCallExpression,
}

export interface LuaNode extends ts.Node {
luaKind: LuaSyntaxKind;
}

export type LuaConcatExpression = LuaNode & ts.BinaryExpression;
export type LuaCallExpression = LuaNode & ts.CallExpression;
21 changes: 21 additions & 0 deletions src/TSHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as ts from "typescript";
import { Decorator, DecoratorKind } from "./Decorator";
import { TSTLErrors } from "./Errors";

export class TSHelper {

Expand Down Expand Up @@ -220,6 +221,26 @@ export class TSHelper {
return [false, null];
}

public static isCompoundPrefixUnaryOperator(node: ts.PrefixUnaryExpression): boolean {
return node.operator !== ts.SyntaxKind.ExclamationToken
&& node.operator !== ts.SyntaxKind.MinusToken
&& node.operator !== ts.SyntaxKind.PlusToken
&& node.operator !== ts.SyntaxKind.TildeToken;
}

public static getUnaryCompoundAssignmentOperator(node: ts.PrefixUnaryExpression | ts.PostfixUnaryExpression)
: ts.BinaryOperator {
switch (node.operator) {
case ts.SyntaxKind.PlusPlusToken:
return ts.SyntaxKind.PlusToken;
case ts.SyntaxKind.MinusMinusToken:
return ts.SyntaxKind.MinusToken;
default:
throw TSTLErrors.UnsupportedKind(
`unary ${ts.isPrefixUnaryExpression(node) ? "prefix" : "postfix"} operator`, node.operator, node);
}
}

public static isExpressionStatement(node: ts.Expression): boolean {
return node.parent === undefined
|| ts.isExpressionStatement(node.parent)
Expand Down
19 changes: 19 additions & 0 deletions src/TransformHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ts from "typescript";
import { LuaCallExpression, LuaConcatExpression, LuaNode, LuaSyntaxKind } from "./LuaNode";

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

public static createLuaConcatExpression(left: ts.Expression, right: ts.Expression): LuaConcatExpression {
const node = ts.createAdd(left, right) as LuaConcatExpression;
node.luaKind = LuaSyntaxKind.ConcatExpression;
return node;
}

public static createLuaCallExpression(expression: ts.Expression,
argumentsArray: ReadonlyArray<ts.Expression>,
isMethod: boolean): LuaCallExpression {
const node = ts.createCall(expression, undefined, argumentsArray) as LuaCallExpression;
node.luaKind = isMethod ? LuaSyntaxKind.MethodCallExpression : LuaSyntaxKind.FunctionCallExpression;
return node;
}

public static isLuaNode<T extends ts.Node>(node: T): node is LuaNode & T {
return (node as LuaNode & T).luaKind !== undefined;
}

public static flatten<T>(arr: T[]): T[] {
const flat = [].concat(...arr);
return flat.some(Array.isArray) ? this.flatten(flat) : flat;
Expand Down
Loading