Skip to content

Commit 4997d61

Browse files
committed
Replace empty json file diagnostic with runtime error
1 parent 2763d0d commit 4997d61

4 files changed

Lines changed: 16 additions & 21 deletions

File tree

src/transformation/utils/errors.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ export const InvalidExportsExtension = (node: ts.Node) =>
4343
export const InvalidInstanceOfExtension = (node: ts.Node) =>
4444
new TranspileError(`Cannot use instanceof on classes with '@extension' or '@metaExtension' annotation.`, node);
4545

46-
export const InvalidJsonFileContent = (node: ts.Node) => new TranspileError("Invalid JSON file content", node);
47-
4846
export const MissingForOfVariables = (node: ts.Node) =>
4947
new TranspileError("Transpiled ForOf variable declaration list contains no declarations.", node);
5048

src/transformation/visitors/sourceFile.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ts from "typescript";
22
import * as lua from "../../LuaAST";
3+
import { assert } from "../../utils";
34
import { FunctionVisitor } from "../context";
4-
import { InvalidJsonFileContent } from "../utils/errors";
55
import { createExportsIdentifier } from "../utils/lua-ast";
66
import { performHoisting, popScope, pushScope, ScopeType } from "../utils/scope";
77
import { hasExportEquals } from "../utils/typescript";
@@ -10,11 +10,16 @@ export const transformSourceFileNode: FunctionVisitor<ts.SourceFile> = (node, co
1010
let statements: lua.Statement[] = [];
1111
if (node.flags & ts.NodeFlags.JsonFile) {
1212
const [statement] = node.statements;
13-
if (!statement || !ts.isExpressionStatement(statement)) {
14-
throw InvalidJsonFileContent(node);
15-
}
13+
if (statement) {
14+
assert(ts.isExpressionStatement(statement));
15+
statements.push(lua.createReturnStatement([context.transformExpression(statement.expression)]));
16+
} else {
17+
const errorCall = lua.createCallExpression(lua.createIdentifier("error"), [
18+
lua.createStringLiteral("Unexpected end of JSON input"),
19+
]);
1620

17-
statements.push(lua.createReturnStatement([context.transformExpression(statement.expression)]));
21+
statements.push(lua.createExpressionStatement(errorCall));
22+
}
1823
} else {
1924
pushScope(context, ScopeType.File);
2025
statements = performHoisting(context, context.transformStatements(node.statements));

test/unit/json.spec.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1-
import * as ts from "typescript";
2-
import { InvalidJsonFileContent } from "../../src/transformation/utils/errors";
31
import * as util from "../util";
42

5-
const jsonOptions = {
6-
resolveJsonModule: true,
7-
noHeader: true,
8-
moduleResolution: ts.ModuleResolutionKind.NodeJs,
9-
};
10-
113
test.each([0, "", [], [1, "2", []], { a: "b" }, { a: { b: "c" } }])("JSON (%p)", json => {
124
util.testModule(JSON.stringify(json))
13-
.setOptions(jsonOptions)
145
.setMainFileName("main.json")
156
.expectToEqual(json);
167
});
178

18-
test("Empty JSON", () => {
9+
test("Empty JSON file error", () => {
1910
util.testModule("")
20-
.setOptions(jsonOptions)
2111
.setMainFileName("main.json")
22-
.expectToHaveDiagnosticOfError(InvalidJsonFileContent(util.nodeStub));
12+
.expectToEqual(new util.ExecutionError("Unexpected end of JSON input"));
2313
});

test/util.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ export abstract class TestBuilder {
190190
skipLibCheck: true,
191191
target: ts.ScriptTarget.ES2017,
192192
lib: ["lib.esnext.d.ts"],
193+
moduleResolution: ts.ModuleResolutionKind.NodeJs,
194+
resolveJsonModule: true,
193195
experimentalDecorators: true,
194196
};
195197
public setOptions(options: tstl.CompilerOptions = {}): this {
@@ -371,8 +373,8 @@ export abstract class TestBuilder {
371373
{ getCurrentDirectory: () => "", getCanonicalFileName: fileName => fileName, getNewLine: () => "\n" }
372374
);
373375

374-
expect(this.getMainLuaCodeChunk()).toMatchSnapshot('code');
375-
expect(diagnosticMessages.trim()).toMatchSnapshot('diagnostics');
376+
expect(this.getMainLuaCodeChunk()).toMatchSnapshot("code");
377+
expect(diagnosticMessages.trim()).toMatchSnapshot("diagnostics");
376378

377379
return this;
378380
}

0 commit comments

Comments
 (0)