Skip to content

Commit d70666f

Browse files
committed
Remove remaining TranspileError handling code
1 parent 11c3ee4 commit d70666f

File tree

5 files changed

+27
-101
lines changed

5 files changed

+27
-101
lines changed

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ export * from "./LuaAST";
66
export { LuaLibFeature } from "./LuaLib";
77
export * from "./LuaPrinter";
88
export * from "./transformation/context";
9-
export { TranspileError } from "./transformation/utils/errors";
109
export * from "./transpilation";

src/transformation/index.ts

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,9 @@ import * as lua from "../LuaAST";
33
import { LuaLibFeature } from "../LuaLib";
44
import { getOrUpdate } from "../utils";
55
import { ObjectVisitor, TransformationContext, VisitorMap, Visitors } from "./context";
6-
import { TranspileError } from "./utils/errors";
76
import { getUsedLuaLibFeatures } from "./utils/lualib";
87
import { standardVisitors } from "./visitors";
98

10-
const transpileErrorDiagnostic = (error: TranspileError): ts.Diagnostic => ({
11-
file: error.node.getSourceFile(),
12-
start: error.node.getStart(),
13-
length: error.node.getWidth(),
14-
category: ts.DiagnosticCategory.Error,
15-
code: 0,
16-
source: "typescript-to-lua",
17-
messageText: error.message,
18-
});
19-
209
export function createVisitorMap(customVisitors: Visitors[]): VisitorMap {
2110
const visitorMap: VisitorMap = new Map();
2211
for (const visitors of [standardVisitors, ...customVisitors]) {
@@ -52,27 +41,11 @@ export function transformSourceFile(
5241
visitorMap: VisitorMap
5342
): TransformSourceFileResult {
5443
const context = new TransformationContext(program, sourceFile, visitorMap);
44+
const [luaAst] = context.transformNode(sourceFile) as [lua.Block];
5545

56-
// TODO: Remove once we'll get rid of all `TranspileError`s
57-
try {
58-
const [luaAst] = context.transformNode(sourceFile) as [lua.Block];
59-
60-
return {
61-
luaAst,
62-
luaLibFeatures: getUsedLuaLibFeatures(context),
63-
diagnostics: context.diagnostics,
64-
};
65-
} catch (error) {
66-
if (!(error instanceof TranspileError)) throw error;
67-
68-
return {
69-
luaAst: lua.createBlock([
70-
lua.createExpressionStatement(
71-
lua.createCallExpression(lua.createIdentifier("error"), [lua.createStringLiteral(error.message)])
72-
),
73-
]),
74-
luaLibFeatures: new Set(),
75-
diagnostics: [transpileErrorDiagnostic(error)],
76-
};
77-
}
46+
return {
47+
luaAst,
48+
luaLibFeatures: getUsedLuaLibFeatures(context),
49+
diagnostics: context.diagnostics,
50+
};
7851
}

src/transformation/utils/errors.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/setup.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,15 @@
11
import * as ts from "typescript";
22
import * as tstl from "../src";
3-
import * as util from "./util";
43

54
declare global {
65
namespace jest {
76
interface Matchers<R, T> {
8-
toThrowExactError(error: Error): R;
97
toHaveDiagnostics(): R;
108
}
119
}
1210
}
1311

1412
expect.extend({
15-
toThrowExactError(callback: () => void, error: Error): jest.CustomMatcherResult {
16-
if (this.isNot) {
17-
return { pass: true, message: () => "Inverted toThrowExactError is not implemented" };
18-
}
19-
20-
let executionError: Error | undefined;
21-
try {
22-
callback();
23-
} catch (err) {
24-
executionError = err;
25-
}
26-
27-
// TODO:
28-
if (util.expectToBeDefined(executionError)) {
29-
expect(executionError.message).toContain(error.message);
30-
}
31-
32-
return { pass: true, message: () => "" };
33-
},
3413
toHaveDiagnostics(diagnostics: ts.Diagnostic[]): jest.CustomMatcherResult {
3514
expect(diagnostics).toBeInstanceOf(Array);
3615
// @ts-ignore

test/util.ts

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function testEachVersion<T extends TestBuilder>(
6060
test(testName, () => {
6161
const builder = common();
6262
builder.setOptions({ luaTarget: version });
63-
if (typeof specialBuilder === 'function') {
63+
if (typeof specialBuilder === "function") {
6464
specialBuilder(builder);
6565
}
6666
});
@@ -151,7 +151,6 @@ export class ExecutionError extends Error {
151151

152152
export type ExecutableTranspiledFile = tstl.TranspiledFile & { lua: string; sourceMap: string };
153153
export type TapCallback = (builder: TestBuilder) => void;
154-
export type DiagnosticMatcher = (diagnostic: ts.Diagnostic) => boolean;
155154
export abstract class TestBuilder {
156155
constructor(protected _tsCode: string) {}
157156

@@ -339,47 +338,21 @@ export abstract class TestBuilder {
339338
return this;
340339
}
341340

342-
public expectToHaveDiagnostic(matcher: DiagnosticMatcher): this {
343-
expect(this.getLuaDiagnostics().find(matcher)).toBeDefined();
344-
return this;
345-
}
346-
347-
public expectToHaveExactDiagnostic(diagnostic: ts.Diagnostic): this {
348-
expect(this.getLuaDiagnostics()).toContainEqual(diagnostic);
349-
return this;
350-
}
341+
private diagnosticsChecked = false;
351342

352343
public expectToHaveDiagnostics(): this {
353-
expect(this.getLuaDiagnostics()).toHaveDiagnostics();
354-
return this;
355-
}
344+
if (this.diagnosticsChecked) return this;
345+
this.diagnosticsChecked = true;
356346

357-
public expectToHaveDiagnosticOfError(error: Error): this {
358-
this.expectToHaveDiagnostics();
359-
expect(this.getLuaDiagnostics()).toHaveLength(1);
360-
const firstDiagnostic = this.getLuaDiagnostics()[0];
361-
expect(firstDiagnostic).toMatchObject({ messageText: error.message });
347+
expect(this.getLuaDiagnostics()).toHaveDiagnostics();
362348
return this;
363349
}
364350

365351
public expectToHaveNoDiagnostics(): this {
366-
expect(this.getLuaDiagnostics()).not.toHaveDiagnostics();
367-
return this;
368-
}
369-
370-
public expectDiagnosticsToMatchSnapshot(diagnosticsOnly = false): this {
371-
this.expectToHaveDiagnostics();
372-
373-
const diagnosticMessages = ts.formatDiagnostics(
374-
this.getLuaDiagnostics().map(tstl.prepareDiagnosticForFormatting),
375-
{ getCurrentDirectory: () => "", getCanonicalFileName: fileName => fileName, getNewLine: () => "\n" }
376-
);
377-
378-
expect(diagnosticMessages.trim()).toMatchSnapshot("diagnostics");
379-
if (!diagnosticsOnly) {
380-
expect(this.getMainLuaCodeChunk()).toMatchSnapshot("code");
381-
}
352+
if (this.diagnosticsChecked) return this;
353+
this.diagnosticsChecked = true;
382354

355+
expect(this.getLuaDiagnostics()).not.toHaveDiagnostics();
383356
return this;
384357
}
385358

@@ -416,9 +389,19 @@ export abstract class TestBuilder {
416389
return this;
417390
}
418391

419-
public expectResultToMatchSnapshot(): this {
420-
this.expectToHaveNoDiagnostics();
421-
expect(this.getLuaExecutionResult()).toMatchSnapshot();
392+
public expectDiagnosticsToMatchSnapshot(diagnosticsOnly = false): this {
393+
this.expectToHaveDiagnostics();
394+
395+
const diagnosticMessages = ts.formatDiagnostics(
396+
this.getLuaDiagnostics().map(tstl.prepareDiagnosticForFormatting),
397+
{ getCurrentDirectory: () => "", getCanonicalFileName: fileName => fileName, getNewLine: () => "\n" }
398+
);
399+
400+
expect(diagnosticMessages.trim()).toMatchSnapshot("diagnostics");
401+
if (!diagnosticsOnly) {
402+
expect(this.getMainLuaCodeChunk()).toMatchSnapshot("code");
403+
}
404+
422405
return this;
423406
}
424407

0 commit comments

Comments
 (0)