X Tutup
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
23 changes: 20 additions & 3 deletions src/transformation/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as ts from "typescript";
import { CompilerOptions, LuaTarget } from "../../CompilerOptions";
import * as lua from "../../LuaAST";
import { castArray } from "../../utils";
import { unsupportedNodeKind } from "../utils/diagnostics";
import { unwrapVisitorResult } from "../utils/lua-ast";
import { ExpressionLikeNode, ObjectVisitor, StatementLikeNode, VisitorMap } from "./visitors";

Expand Down Expand Up @@ -46,15 +47,21 @@ export class TransformationContext {
}

private currentNodeVisitors: Array<ObjectVisitor<ts.Node>> = [];
public transformNode(node: ts.Node): lua.Node[] {

public transformNode(node: ts.Node): lua.Node[];
/** @internal */
// eslint-disable-next-line @typescript-eslint/unified-signatures
public transformNode(node: ts.Node, isExpression?: boolean): lua.Node[];
public transformNode(node: ts.Node, isExpression?: boolean): lua.Node[] {
// TODO: Move to visitors?
if (node.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.DeclareKeyword)) {
return [];
}

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

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

public transformExpression(node: ExpressionLikeNode): lua.Expression {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing should be done for superTransformExpression

const [result] = this.transformNode(node);
const [result] = this.transformNode(node, true);

if (result === undefined) {
throw new Error(`Expression visitor for node type ${ts.SyntaxKind[node.kind]} did not return any result.`);
}

return result as lua.Expression;
}

public superTransformExpression(node: ExpressionLikeNode): lua.Expression {
const [result] = this.superTransformNode(node);

if (result === undefined) {
throw new Error(`Expression visitor for node type ${ts.SyntaxKind[node.kind]} did not return any result.`);
}

return result as lua.Expression;
}

Expand Down
4 changes: 4 additions & 0 deletions src/transformation/utils/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const createDiagnosticFactory = <TArgs extends any[]>(message: string | ((...arg
messageText: typeof message === "string" ? message : message(...args),
}));

export const unsupportedNodeKind = createDiagnosticFactory(
(kind: ts.SyntaxKind) => `Unsupported node kind ${ts.SyntaxKind[kind]}`
);

export const forbiddenForIn = createDiagnosticFactory("Iterating over arrays with 'for ... in' is not allowed.");

export const unsupportedNoSelfFunctionConversion = createDiagnosticFactory((name?: string) => {
Expand Down
8 changes: 8 additions & 0 deletions test/unit/__snapshots__/statements.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Unsupported node adds diagnostic: code 1`] = `
"a = function()
end"
`;

exports[`Unsupported node adds diagnostic: diagnostics 1`] = `"main.ts(3,13): error TSTL: Unsupported node kind LabeledStatement"`;
9 changes: 0 additions & 9 deletions test/unit/expressions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ import * as tstl from "../../src";
import { unsupportedForTarget, unsupportedRightShiftOperator } from "../../src/transformation/utils/diagnostics";
import * as util from "../util";

// TODO:
test("Block statement", () => {
util.testFunction`
let a = 4;
{ let a = 42; }
return a;
`.expectToMatchJsResult();
});

test.each([
"i++",
"++i",
Expand Down
19 changes: 19 additions & 0 deletions test/unit/statements.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as util from "../util";
import { unsupportedNodeKind } from "../../src/transformation/utils/diagnostics";

test("Block statement", () => {
util.testFunction`
let a = 4;
{ let a = 42; }
return a;
`.expectToMatchJsResult();
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/860
test("Unsupported node adds diagnostic", () => {
util.testModule`
const a = () => {
foo: "bar"
};
`.expectDiagnosticsToMatchSnapshot([unsupportedNodeKind.code]);
});
X Tutup