X Tutup
Skip to content

Commit 375e009

Browse files
authored
Replaced error throw with diagnostic (#861)
* Replaced error throw with diagnostic * Return nil if node is not a statement * Handle expression case in transformExpression * Throw error when expression visitor returns nothing * Moved tests around * Added error incase superTransform expression visitor returns nothing
1 parent 7556611 commit 375e009

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
lines changed

src/transformation/context/context.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as ts from "typescript";
22
import { CompilerOptions, LuaTarget } from "../../CompilerOptions";
33
import * as lua from "../../LuaAST";
44
import { castArray } from "../../utils";
5+
import { unsupportedNodeKind } from "../utils/diagnostics";
56
import { unwrapVisitorResult } from "../utils/lua-ast";
67
import { ExpressionLikeNode, ObjectVisitor, StatementLikeNode, VisitorMap } from "./visitors";
78

@@ -46,15 +47,21 @@ export class TransformationContext {
4647
}
4748

4849
private currentNodeVisitors: Array<ObjectVisitor<ts.Node>> = [];
49-
public transformNode(node: ts.Node): lua.Node[] {
50+
51+
public transformNode(node: ts.Node): lua.Node[];
52+
/** @internal */
53+
// eslint-disable-next-line @typescript-eslint/unified-signatures
54+
public transformNode(node: ts.Node, isExpression?: boolean): lua.Node[];
55+
public transformNode(node: ts.Node, isExpression?: boolean): lua.Node[] {
5056
// TODO: Move to visitors?
5157
if (node.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.DeclareKeyword)) {
5258
return [];
5359
}
5460

5561
const nodeVisitors = this.visitorMap.get(node.kind);
5662
if (!nodeVisitors || nodeVisitors.length === 0) {
57-
throw new Error(`Unsupported node kind: ${ts.SyntaxKind[node.kind]}.`);
63+
this.diagnostics.push(unsupportedNodeKind(node, node.kind));
64+
return isExpression ? [lua.createNilLiteral()] : [];
5865
}
5966

6067
const previousNodeVisitors = this.currentNodeVisitors;
@@ -78,12 +85,22 @@ export class TransformationContext {
7885
}
7986

8087
public transformExpression(node: ExpressionLikeNode): lua.Expression {
81-
const [result] = this.transformNode(node);
88+
const [result] = this.transformNode(node, true);
89+
90+
if (result === undefined) {
91+
throw new Error(`Expression visitor for node type ${ts.SyntaxKind[node.kind]} did not return any result.`);
92+
}
93+
8294
return result as lua.Expression;
8395
}
8496

8597
public superTransformExpression(node: ExpressionLikeNode): lua.Expression {
8698
const [result] = this.superTransformNode(node);
99+
100+
if (result === undefined) {
101+
throw new Error(`Expression visitor for node type ${ts.SyntaxKind[node.kind]} did not return any result.`);
102+
}
103+
87104
return result as lua.Expression;
88105
}
89106

src/transformation/utils/diagnostics.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const createDiagnosticFactory = <TArgs extends any[]>(message: string | ((...arg
1111
messageText: typeof message === "string" ? message : message(...args),
1212
}));
1313

14+
export const unsupportedNodeKind = createDiagnosticFactory(
15+
(kind: ts.SyntaxKind) => `Unsupported node kind ${ts.SyntaxKind[kind]}`
16+
);
17+
1418
export const forbiddenForIn = createDiagnosticFactory("Iterating over arrays with 'for ... in' is not allowed.");
1519

1620
export const unsupportedNoSelfFunctionConversion = createDiagnosticFactory((name?: string) => {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Unsupported node adds diagnostic: code 1`] = `
4+
"a = function()
5+
end"
6+
`;
7+
8+
exports[`Unsupported node adds diagnostic: diagnostics 1`] = `"main.ts(3,13): error TSTL: Unsupported node kind LabeledStatement"`;

test/unit/expressions.spec.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@ import * as tstl from "../../src";
22
import { unsupportedForTarget, unsupportedRightShiftOperator } from "../../src/transformation/utils/diagnostics";
33
import * as util from "../util";
44

5-
// TODO:
6-
test("Block statement", () => {
7-
util.testFunction`
8-
let a = 4;
9-
{ let a = 42; }
10-
return a;
11-
`.expectToMatchJsResult();
12-
});
13-
145
test.each([
156
"i++",
167
"++i",

test/unit/statements.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as util from "../util";
2+
import { unsupportedNodeKind } from "../../src/transformation/utils/diagnostics";
3+
4+
test("Block statement", () => {
5+
util.testFunction`
6+
let a = 4;
7+
{ let a = 42; }
8+
return a;
9+
`.expectToMatchJsResult();
10+
});
11+
12+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/860
13+
test("Unsupported node adds diagnostic", () => {
14+
util.testModule`
15+
const a = () => {
16+
foo: "bar"
17+
};
18+
`.expectDiagnosticsToMatchSnapshot([unsupportedNodeKind.code]);
19+
});

0 commit comments

Comments
 (0)
X Tutup