Skip to content

Commit 862c1f8

Browse files
committed
Fixing tests
Added trasnformVariableStatement & transformReturn Fixed couple of typos in the AST Fixed indent not being initialized
1 parent 631f885 commit 862c1f8

File tree

4 files changed

+162
-44
lines changed

4 files changed

+162
-44
lines changed

src/Errors.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,65 +12,65 @@ export class TranspileError extends Error {
1212

1313
export class TSTLErrors {
1414
public static DefaultImportsNotSupported = (node: ts.Node) =>
15-
new TranspileError(`Default Imports are not supported, please use named imports instead!`, node)
15+
new TranspileError(`Default Imports are not supported, please use named imports instead!`, node);
1616

1717
public static ForbiddenEllipsisDestruction = (node: ts.Node) =>
18-
new TranspileError(`Ellipsis destruction is not allowed.`, node)
18+
new TranspileError(`Ellipsis destruction is not allowed.`, node);
1919

2020
public static ForbiddenForIn = (node: ts.Node) =>
21-
new TranspileError(`Iterating over arrays with 'for ... in' is not allowed.`, node)
21+
new TranspileError(`Iterating over arrays with 'for ... in' is not allowed.`, node);
2222

2323
public static HeterogeneousEnum = (node: ts.Node) =>
2424
new TranspileError(`Invalid heterogeneous enum. Enums should either specify no member values, ` +
2525
`or specify values (of the same type) for all members.`,
26-
node)
26+
node);
2727

2828
public static InvalidEnumMember = (node: ts.Node) =>
29-
new TranspileError(`Only numeric or string initializers allowed for enums.`, node)
29+
new TranspileError(`Only numeric or string initializers allowed for enums.`, node);
3030

3131
public static InvalidDecoratorArgumentNumber = (name: string, got: number, expected: number, node: ts.Node) =>
32-
new TranspileError(`${name} expects ${expected} argument(s) but got ${got}.`, node)
32+
new TranspileError(`${name} expects ${expected} argument(s) but got ${got}.`, node);
3333

3434
public static InvalidExtensionMetaExtension = (node: ts.Node) =>
35-
new TranspileError(`Cannot use both '!Extension' and '!MetaExtension' decorators on the same class.`, node)
35+
new TranspileError(`Cannot use both '!Extension' and '!MetaExtension' decorators on the same class.`, node);
3636

3737
public static InvalidNewExpressionOnExtension = (node: ts.Node) =>
38-
new TranspileError(`Cannot construct classes with decorator '!Extension' or '!MetaExtension'.`, node)
38+
new TranspileError(`Cannot construct classes with decorator '!Extension' or '!MetaExtension'.`, node);
3939

4040
public static InvalidPropertyCall = (node: ts.Node) =>
41-
new TranspileError(`Tried to transpile a non-property call as property call.`, node)
41+
new TranspileError(`Tried to transpile a non-property call as property call.`, node);
4242

4343
public static InvalidElementCall = (node: ts.Node) =>
44-
new TranspileError(`Tried to transpile a non-element call as an element call.`, node)
44+
new TranspileError(`Tried to transpile a non-element call as an element call.`, node);
4545

4646
public static InvalidThrowExpression = (node: ts.Node) =>
47-
new TranspileError(`Invalid throw expression, only strings can be thrown.`, node)
47+
new TranspileError(`Invalid throw expression, only strings can be thrown.`, node);
4848

4949
public static KeywordIdentifier = (node: ts.Identifier) =>
50-
new TranspileError(`Cannot use Lua keyword ${node.escapedText} as identifier.`, node)
50+
new TranspileError(`Cannot use Lua keyword ${node.escapedText} as identifier.`, node);
5151

5252
public static MissingClassName = (node: ts.Node) =>
53-
new TranspileError(`Class declarations must have a name.`, node)
53+
new TranspileError(`Class declarations must have a name.`, node);
5454

5555
public static MissingMetaExtension = (node: ts.Node) =>
56-
new TranspileError(`!MetaExtension requires the extension of the metatable class.`, node)
56+
new TranspileError(`!MetaExtension requires the extension of the metatable class.`, node);
5757

5858
public static UnsupportedImportType = (node: ts.Node) =>
59-
new TranspileError(`Unsupported import type.`, node)
59+
new TranspileError(`Unsupported import type.`, node);
6060

6161
public static UnsupportedKind = (description: string, kind: ts.SyntaxKind, node: ts.Node) => {
6262
const kindName = tsHelper.enumName(kind, ts.SyntaxKind);
6363
return new TranspileError(`Unsupported ${description} kind: ${kindName}`, node);
6464
}
6565

6666
public static UnsupportedProperty = (parentName: string, property: string, node: ts.Node) =>
67-
new TranspileError(`Unsupported property on ${parentName}: ${property}`, node)
67+
new TranspileError(`Unsupported property on ${parentName}: ${property}`, node);
6868

6969
public static UnsupportedForTarget = (functionality: string, version: string, node: ts.Node) =>
70-
new TranspileError(`${functionality} is/are not supported for target Lua ${version}.`, node)
70+
new TranspileError(`${functionality} is/are not supported for target Lua ${version}.`, node);
7171

7272
public static UnsupportedObjectLiteralElement = (elementKind: ts.SyntaxKind, node: ts.Node) =>
73-
new TranspileError(`Unsupported object literal element: ${elementKind}.`, node)
73+
new TranspileError(`Unsupported object literal element: ${elementKind}.`, node);
7474

7575
public static UnsupportedFunctionConversion = (node: ts.Node, name?: string) => {
7676
if (name) {
@@ -82,7 +82,7 @@ export class TSTLErrors {
8282
+ `To fix, wrap the method in an arrow function.`,
8383
node);
8484
}
85-
}
85+
};
8686

8787
public static UnsupportedMethodConversion = (node: ts.Node, name?: string) => {
8888
if (name) {
@@ -96,7 +96,7 @@ export class TSTLErrors {
9696
+ ` an explicit 'this' parameter.`,
9797
node);
9898
}
99-
}
99+
};
100100

101101
public static UnsupportedOverloadAssignment = (node: ts.Node, name?: string) => {
102102
if (name) {
@@ -108,5 +108,5 @@ export class TSTLErrors {
108108
+ `Overloads should either be all functions or all methods, but not both.`,
109109
node);
110110
}
111-
}
111+
};
112112
}

src/LuaAST.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export function isDoStatement(node: Node): node is DoStatement {
170170
}
171171

172172
export function createDoStatement(statements?: Statement[], parent?: Node, tsOriginal?: ts.Node): DoStatement {
173-
const statement = createNode(SyntaxKind.Block, parent, tsOriginal) as DoStatement;
173+
const statement = createNode(SyntaxKind.DoStatement, parent, tsOriginal) as DoStatement;
174174
setParent(statements, statement);
175175
statement.statements = statements;
176176
return statement;
@@ -376,7 +376,7 @@ export function createForInStatement(
376376
parent?: Node,
377377
tsOriginal?: ts.Node): ForInStatement {
378378

379-
const statement = createNode(SyntaxKind.ForStatement, parent, tsOriginal) as ForInStatement;
379+
const statement = createNode(SyntaxKind.ForInStatement, parent, tsOriginal) as ForInStatement;
380380
setParent(body, statement);
381381
statement.body = body;
382382
setParent(names, statement);
@@ -769,7 +769,7 @@ export function isTableIndexExpression(node: Node): node is TableIndexExpression
769769
export function createTableIndexExpression(
770770
table: Expression, index: Expression, parent?: Node, tsOriginal?: ts.Node): TableIndexExpression {
771771

772-
const expression = createNode(SyntaxKind.Identifier, parent, tsOriginal) as TableIndexExpression;
772+
const expression = createNode(SyntaxKind.TableIndexExpression, parent, tsOriginal) as TableIndexExpression;
773773
setParent(table, expression);
774774
expression.table = table;
775775
setParent(index, expression);

src/LuaPrinter.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export class LuaPrinter {
3434

3535
private currentIndent: string;
3636

37+
public constructor() {
38+
this.currentIndent = "";
39+
}
40+
3741
public print(block: tstl.Block): string {
3842
return this.printBlock(block);
3943
}
@@ -96,12 +100,16 @@ export class LuaPrinter {
96100
}
97101

98102
private printVariableDeclarationStatement(statement: tstl.VariableDeclarationStatement): string {
99-
return this.indent(`"local" ${statement.left.map(e => this.printExpression(e)).join(", ")} =` +
100-
`${statement.right.map(e => this.printExpression(e)).join(", ")};\n`);
103+
const left = this.indent(`local ${statement.left.map(e => this.printExpression(e)).join(", ")}`);
104+
if (statement.right) {
105+
return left + ` = ${statement.right.map(e => this.printExpression(e)).join(", ")};\n`;
106+
} else {
107+
return left + ";\n";
108+
}
101109
}
102110

103111
private printVariableAssignmentStatement(statement: tstl.VariableAssignmentStatement): string {
104-
return this.indent(`${statement.left.map(e => this.printExpression(e)).join(", ")} =` +
112+
return this.indent(`${statement.left.map(e => this.printExpression(e)).join(", ")} = ` +
105113
`${statement.right.map(e => this.printExpression(e)).join(", ")};\n`);
106114
}
107115

@@ -272,7 +280,7 @@ export class LuaPrinter {
272280
this.pushIndent();
273281
result += this.printBlock(expression.body);
274282
this.popIndent();
275-
result += this.indent("end\n");
283+
result += this.indent("end");
276284

277285
return result;
278286
}

0 commit comments

Comments
 (0)