Skip to content

Commit d63ce03

Browse files
authored
Error when Lua keywords are used as identifier (#173)
* Refactored the way TranspileErrors are created * Disallow lua keywords as identifiers
1 parent 60f43a4 commit d63ce03

17 files changed

Lines changed: 233 additions & 158 deletions

src/Compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { LuaTranspiler51 } from "./targets/Transpiler.51";
77
import { LuaTranspiler52 } from "./targets/Transpiler.52";
88
import { LuaTranspiler53 } from "./targets/Transpiler.53";
99
import { LuaTranspilerJIT } from "./targets/Transpiler.JIT";
10-
import { LuaLibImportKind, LuaTarget, LuaTranspiler, TranspileError } from "./Transpiler";
10+
import { LuaLibImportKind, LuaTarget, LuaTranspiler } from "./Transpiler";
1111

1212
export function compile(argv: string[]): void {
1313
const commandLine = parseCommandLine(argv);

src/Errors.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as ts from "typescript";
2+
3+
import { TSHelper as tsHelper } from "./TSHelper";
4+
5+
export class TranspileError extends Error {
6+
public node: ts.Node;
7+
constructor(message: string, node: ts.Node) {
8+
super(message);
9+
this.node = node;
10+
}
11+
}
12+
13+
export class TSTLErrors {
14+
public static DefaultImportsNotSupported = (node: ts.Node) =>
15+
new TranspileError(`Default Imports are not supported, please use named imports instead!`, node)
16+
17+
public static ForbiddenEllipsisDestruction = (node: ts.Node) =>
18+
new TranspileError(`Ellipsis destruction is not allowed.`, node)
19+
20+
public static ForbiddenForIn = (node: ts.Node) =>
21+
new TranspileError(`Iterating over arrays with 'for ... in' is not allowed.`, node)
22+
23+
public static HeterogeneousEnum = (node: ts.Node) =>
24+
new TranspileError(`Invalid heterogeneous enum. Enums should either specify no member values, ` +
25+
`or specify values (of the same type) for all members.`,
26+
node)
27+
28+
public static InvalidEnumMember = (node: ts.Node) =>
29+
new TranspileError(`Only numeric or string initializers allowed for enums.`, node)
30+
31+
public static InvalidDecoratorArgumentNumber = (name: string, got: number, expected: number, node: ts.Node) =>
32+
new TranspileError(`${name} expects ${expected} argument(s) but got ${got}.`, node)
33+
34+
public static InvalidExtensionMetaExtension = (node: ts.Node) =>
35+
new TranspileError(`Cannot use both '!Extension' and '!MetaExtension' decorators on the same class.`, node)
36+
37+
public static InvalidPropertyCall = (node: ts.Node) =>
38+
new TranspileError(`Tried to transpile a non-property call as property call.`, node)
39+
40+
public static InvalidThrowExpression = (node: ts.Node) =>
41+
new TranspileError(`Invalid throw expression, only strings can be thrown.`, node)
42+
43+
public static KeywordIdentifier = (node: ts.Identifier) =>
44+
new TranspileError(`Cannot use Lua keyword ${node.escapedText} as identifier.`, node)
45+
46+
public static MissingClassName = (node: ts.Node) =>
47+
new TranspileError(`Class declarations must have a name.`, node)
48+
49+
public static MissingMetaExtension = (node: ts.Node) =>
50+
new TranspileError(`!MetaExtension requires the extension of the metatable class.`, node)
51+
52+
public static UnsupportedImportType = (node: ts.Node) =>
53+
new TranspileError(`Unsupported import type.`, node)
54+
55+
public static UnsupportedKind = (description: string, kind: ts.SyntaxKind, node: ts.Node) => {
56+
const kindName = tsHelper.enumName(kind, ts.SyntaxKind);
57+
return new TranspileError(`Unsupported ${description} kind: ${kindName}`, node);
58+
}
59+
60+
public static UnsupportedProperty = (parentName: string, property: string, node: ts.Node) =>
61+
new TranspileError(`Unsupported property on ${parentName}: ${property}`, node)
62+
63+
public static UnsupportedForTarget = (functionality: string, version: string, node: ts.Node) =>
64+
new TranspileError(`${functionality} is/are not supported for target Lua ${version}.`, node)
65+
66+
public static UnsupportedObjectLiteralElement = (elementKind: ts.SyntaxKind, node: ts.Node) =>
67+
new TranspileError(`Unsupported object literal element: ${elementKind}.`, node)
68+
}

0 commit comments

Comments
 (0)