Skip to content

Commit acf4ee0

Browse files
committed
Cleaned up obsolete TSHelper getChildren functions
1 parent 8d99864 commit acf4ee0

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

src/TSHelper.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
import * as ts from "typescript";
22

33
export class TSHelper {
4-
// Get all children of a node, required until microsoft fixes node.getChildren()
5-
public static getChildren(node: ts.Node): ts.Node[] {
6-
const children: ts.Node[] = [];
7-
node.forEachChild((child) => {
8-
children.push(child);
9-
});
10-
return children;
11-
}
12-
13-
// Get children filtered by function and cast to predefined type
14-
public static getChildrenOfType<T>(node: ts.Node, typeFilter: (node: ts.Node) => boolean): T[] {
15-
return this.getChildren(node).filter(typeFilter) as any as T[];
16-
}
174

185
// Reverse lookup of enum key by value
196
public static enumName(needle, haystack) {

src/Transpiler.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ export class LuaTranspiler {
148148
// Transpile a node of unknown kind.
149149
public transpileNode(node: ts.Node): string {
150150
// Ignore declarations
151-
if (tsEx.getChildrenOfType(node, (child) =>
152-
child.kind === ts.SyntaxKind.DeclareKeyword).length > 0
153-
) {
151+
if (node.modifiers && node.modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.DeclareKeyword)) {
154152
return "";
155153
}
156154

@@ -172,7 +170,7 @@ export class LuaTranspiler {
172170
this.transpileVariableStatement(node as ts.VariableStatement) + "\n";
173171
case ts.SyntaxKind.ExpressionStatement:
174172
return this.indent +
175-
this.transpileExpression(tsEx.getChildren(node)[0] as ts.Expression) + "\n";
173+
this.transpileExpression((node as ts.ExpressionStatement).expression) + "\n";
176174
case ts.SyntaxKind.ReturnStatement:
177175
return this.indent + this.transpileReturn(node as ts.ReturnStatement) + "\n";
178176
case ts.SyntaxKind.IfStatement:
@@ -1385,16 +1383,21 @@ export class LuaTranspiler {
13851383
public transpileObjectLiteral(node: ts.ObjectLiteralExpression): string {
13861384
const properties: string[] = [];
13871385
// Add all property assignments
1388-
node.properties.forEach((assignment) => {
1389-
const [key, value] = tsEx.getChildren(assignment);
1390-
if (ts.isIdentifier(key)) {
1391-
properties.push(`${key.escapedText}=` + this.transpileExpression(value));
1392-
} else if (ts.isComputedPropertyName(key)) {
1393-
const index = this.transpileExpression(key);
1394-
properties.push(`${index}=` + this.transpileExpression(value));
1386+
node.properties.forEach((element) => {
1387+
let name = "";
1388+
if (ts.isIdentifier(element.name)) {
1389+
name = element.name.escapedText as string;
1390+
} else if (ts.isComputedPropertyName(element.name)) {
1391+
name = this.transpileExpression(element.name);
1392+
} else {
1393+
name = `[${this.transpileExpression(element.name)}]`;
1394+
}
1395+
1396+
if (ts.isPropertyAssignment(element)) {
1397+
const expression = this.transpileExpression(element.initializer);
1398+
properties.push(`${name} = ${expression}`);
13951399
} else {
1396-
const index = this.transpileExpression(key as ts.Expression);
1397-
properties.push(`[${index}]=` + this.transpileExpression(value));
1400+
throw new TranspileError(`Encountered unsupported object literal element ${element.kind}.`, node);
13981401
}
13991402
});
14001403

test/unit/assignments.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class AssignmentTests {
1010
@TestCase("[1,2,3]", "{1,2,3}")
1111
@TestCase("true", "true")
1212
@TestCase("false", "false")
13-
@TestCase(`{a:3,b:"4"}`, `{a=3,b="4"}`)
13+
@TestCase(`{a:3,b:"4"}`, `{a = 3,b = "4"}`)
1414
@Test("Const assignment")
1515
public constAssignment(inp: string, out: string) {
1616
var lua = util.transpileString(`const myvar = ${inp};`)
@@ -22,7 +22,7 @@ export class AssignmentTests {
2222
@TestCase("[1,2,3]", "{1,2,3}")
2323
@TestCase("true", "true")
2424
@TestCase("false", "false")
25-
@TestCase(`{a:3,b:"4"}`, `{a=3,b="4"}`)
25+
@TestCase(`{a:3,b:"4"}`, `{a = 3,b = "4"}`)
2626
@Test("Const assignment")
2727
public letAssignment(inp: string, out: string) {
2828
var lua = util.transpileString(`let myvar = ${inp};`)
@@ -34,7 +34,7 @@ export class AssignmentTests {
3434
@TestCase("[1,2,3]", "{1,2,3}")
3535
@TestCase("true", "true")
3636
@TestCase("false", "false")
37-
@TestCase(`{a:3,b:"4"}`, `{a=3,b="4"}`)
37+
@TestCase(`{a:3,b:"4"}`, `{a = 3,b = "4"}`)
3838
@Test("Const assignment")
3939
public varAssignment(inp: string, out: string) {
4040
var lua = util.transpileString(`var myvar = ${inp};`)

test/unit/objectLiteral.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Expect, Test, TestCase } from "alsatian";
2+
3+
import * as util from "../src/util";
4+
const fs = require("fs");
5+
6+
export class ObjectLiteralTests {
7+
8+
@TestCase(`{a:3,b:"4"}`, `{a = 3,b = "4"}`)
9+
@TestCase(`{"a":3,b:"4"}`, `{["a"] = 3,b = "4"}`)
10+
@TestCase(`{["a"]:3,b:"4"}`, `{["a"] = 3,b = "4"}`)
11+
@TestCase(`{["a"+123]:3,b:"4"}`, `{["a" .. 123] = 3,b = "4"}`)
12+
@TestCase(`{[myFunc()]:3,b:"4"}`, `{[myFunc()] = 3,b = "4"}`)
13+
@Test("Object Literal")
14+
public objectLiteral(inp: string, out: string) {
15+
var lua = util.transpileString(`const myvar = ${inp};`)
16+
Expect(lua).toBe(`local myvar = ${out}`);
17+
}
18+
}

0 commit comments

Comments
 (0)