Skip to content

Commit d36356d

Browse files
committed
Resolve luaEntry paths relative to tsconfig
1 parent 7c6ab11 commit d36356d

4 files changed

Lines changed: 30 additions & 7 deletions

File tree

src/LuaTransformer.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,15 @@ export class LuaTransformer {
142142

143143
if (this.options.luaEntry) {
144144
const requireCalls = this.options.luaEntry.map(entry => {
145-
const formattedEntryName = tsHelper.formatPathToLuaPath(entry.replace(/.ts$/, ""));
146-
return tstl.createExpressionStatement(
147-
this.createModuleRequire(ts.createStringLiteral(formattedEntryName), false)
148-
);
145+
const sourceFile = this.program.getSourceFile(entry);
146+
if (sourceFile) {
147+
const formattedEntryName = tsHelper.getExportPath(sourceFile.fileName, this.options);
148+
return tstl.createExpressionStatement(
149+
this.createModuleRequire(ts.createStringLiteral(formattedEntryName), false)
150+
);
151+
} else {
152+
throw TSTLErrors.LuaEntryNotFound(entry);
153+
}
149154
});
150155
combinedStatements.push(...requireCalls);
151156
}

src/TSTLErrors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ export const InvalidThrowExpression = (node: ts.Node) =>
6060
export const ForbiddenStaticClassPropertyName = (node: ts.Node, name: string) =>
6161
new TranspileError(`Cannot use "${name}" as a static class property or method name.`, node);
6262

63+
export const LuaEntryNotFound = (entryName: string) =>
64+
new TranspileError(`Could not find luaEntry file '${entryName}'.`, ts.createNode(ts.SyntaxKind.Unknown));
65+
6366
export const MissingClassName = (node: ts.Node) => new TranspileError(`Class declarations must have a name.`, node);
6467

6568
export const MissingForOfVariables = (node: ts.Node) =>

src/diagnostics.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as ts from "typescript";
22
import { TranspileError } from "./TranspileError";
33

44
export const transpileError = (error: TranspileError): ts.Diagnostic => ({
5-
file: error.node.getSourceFile(),
6-
start: error.node.getStart(),
7-
length: error.node.getWidth(),
5+
file: error.node.kind === ts.SyntaxKind.Unknown ? undefined : error.node.getSourceFile(),
6+
start: error.node.kind === ts.SyntaxKind.Unknown ? undefined : error.node.getStart(),
7+
length: error.node.kind === ts.SyntaxKind.Unknown ? undefined : error.node.getWidth(),
88
category: ts.DiagnosticCategory.Error,
99
code: 0,
1010
source: "typescript-to-lua",

test/unit/outFile.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as util from "../util";
22
import * as ts from "typescript";
3+
import * as TSTLErrors from "../../src/TSTLErrors";
34
import { CompilerOptions } from "../../src/CompilerOptions";
45

56
const exportValueSource = "export const value = true;";
@@ -115,4 +116,18 @@ describe("outFile", () => {
115116
.setOptions(outFileOptionsWithEntry("main.ts"))
116117
.expectNoExecutionError();
117118
});
119+
120+
test("luaEntry doesn't exist", () => {
121+
util.testBundle``
122+
.setOptions(outFileOptionsWithEntry("entry.ts"))
123+
.expectToHaveDiagnosticOfError(TSTLErrors.LuaEntryNotFound("entry.ts"));
124+
});
125+
126+
test("luaEntry resolved from path specified in tsconfig", () => {
127+
util.testBundle``
128+
.addExtraFile("src/main.ts", "")
129+
.addExtraFile("src/module.ts", "")
130+
.setOptions({ rootDir: "src", ...outFileOptionsWithEntry("src/main.ts") })
131+
.expectToHaveNoDiagnostics();
132+
});
118133
});

0 commit comments

Comments
 (0)