Skip to content

Commit af82e4e

Browse files
committed
More tests
1 parent 29b5d62 commit af82e4e

7 files changed

Lines changed: 94 additions & 8 deletions

File tree

src/transpilation/resolve.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { EmitHost, ProcessedFile } from "./utils";
66
import { SourceNode } from "source-map";
77

88
const resolver = resolve.ResolverFactory.createResolver({
9-
extensions: [".lua", ".ts"],
9+
extensions: [".lua"],
1010
fileSystem: { ...new resolve.CachedInputFileSystem(fs) },
1111
useSyncFileSystemCalls: true,
1212
});
@@ -15,7 +15,7 @@ export function resolveDependencies(program: ts.Program, files: ProcessedFile[],
1515
const outFiles = [];
1616

1717
for (const file of files) {
18-
outFiles.push(file, ...resolveFileDependencies(file, program.getCommonSourceDirectory(), emitHost));
18+
outFiles.push(file, ...resolveFileDependencies(file, program.getCompilerOptions().rootDir ?? program.getCommonSourceDirectory(), emitHost));
1919
}
2020

2121
return outFiles;
@@ -24,8 +24,14 @@ export function resolveDependencies(program: ts.Program, files: ProcessedFile[],
2424
function resolveFileDependencies(file: ProcessedFile, projectRootDir: string, emitHost: EmitHost): ProcessedFile[] {
2525
const dependencies: ProcessedFile[] = [];
2626
for (const required of findRequiredPaths(file.code)) {
27+
// Do no resolve lualib
28+
if (required === "lualib_bundle") {
29+
continue;
30+
}
31+
2732
// Try to resolve the import starting from the directory `file` is in
28-
const resolvedDependency = resolveDependency(projectRootDir, required);
33+
const fileDir = path.dirname(file.fileName);
34+
const resolvedDependency = resolveDependency(fileDir, projectRootDir, required, emitHost);
2935
if (resolvedDependency) {
3036
// If dependency resolved successfully, read its content
3137
const dependencyContent = emitHost.readFile(resolvedDependency);
@@ -48,6 +54,7 @@ function resolveFileDependencies(file: ProcessedFile, projectRootDir: string, em
4854
} else {
4955
//throw `TODO: COULD NOT RESOLVE ${required}`;
5056
console.error(`Failed to resolve ${required} referenced in ${file.fileName}.`);
57+
console.error(projectRootDir);
5158
}
5259
}
5360
return dependencies;
@@ -95,13 +102,25 @@ function findRequiredPaths(code: string): string[] {
95102
return paths;
96103
}
97104

98-
function resolveDependency(fromDirectory: string, dependency: string): string | undefined {
105+
function resolveDependency(fileDirectory: string, rootDirectory: string, dependency: string, emitHost: EmitHost): string | undefined {
106+
// Check if
107+
const dependencyPath = dependency.replace(".", "/");
108+
const projectFilePath = path.join(fileDirectory, dependencyPath + ".ts");
109+
if (emitHost.fileExists(projectFilePath)) {
110+
return projectFilePath;
111+
}
112+
113+
const projectIndexPath = path.join(fileDirectory, dependencyPath, "index.ts");
114+
if (emitHost.fileExists(projectIndexPath)) {
115+
return projectIndexPath;
116+
}
117+
99118
try {
100-
const resolveResult = resolver.resolveSync({}, fromDirectory, dependency.replace(".", "/"));
119+
const resolveResult = resolver.resolveSync({}, rootDirectory, dependencyPath);
101120
if (resolveResult) {
102121
return resolveResult;
103122
}
104-
} catch {
123+
} catch (e) {
105124
// resolveSync errors if it fails to resolve
106125
}
107126

src/transpilation/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as lua from "../LuaAST";
88
import * as diagnosticFactories from "./diagnostics";
99

1010
export interface EmitHost {
11+
fileExists(path: string): boolean;
1112
getCurrentDirectory(): string;
1213
readFile(path: string): string | undefined;
1314
writeFile: ts.WriteFileCallback;

test/transpile/module-resolution.spec.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ describe("module resolution with chained dependencies", () => {
8686
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-dependency-chain");
8787

8888
test("can resolve dependencies in chain", () => {
89-
//transpileProject(path.join(projectPath, "tsconfig.json"))
9089
util.testProject(path.join(projectPath, "tsconfig.json"))
9190
.setMainFileName(path.join(projectPath, "main.ts"))
9291
.expectToEqual({ result: "dependency3" });
@@ -100,3 +99,49 @@ describe("module resolution with chained dependencies", () => {
10099
.expectToEqual({ result: "dependency3" });
101100
});
102101
});
102+
103+
describe("module resolution with outDir", () => {
104+
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-dependency-chain");
105+
106+
test("emits files in outDir", () => {
107+
const builder = util.testProject(path.join(projectPath, "tsconfig.json"))
108+
.setMainFileName(path.join(projectPath, "main.ts"))
109+
.setOptions({ outDir: "tstl-out" })
110+
.expectToEqual({ result: "dependency3" });
111+
112+
// Get the output paths relative to the project path
113+
const outPaths = builder.getLuaResult().transpiledFiles.map(f => path.relative(projectPath, f.outPath));
114+
expect(outPaths).toHaveLength(4);
115+
expect(outPaths).toContain("tstl-out/main.lua");
116+
expect(outPaths).toContain("tstl-out/node_modules/dependency1/index.lua");
117+
expect(outPaths).toContain("tstl-out/node_modules/dependency2/index.lua");
118+
expect(outPaths).toContain("tstl-out/node_modules/dependency3/index.lua");
119+
});
120+
121+
test("emits bundle in outDir", () => {
122+
const mainFile = path.join(projectPath, "main.ts");
123+
const builder = util.testProject(path.join(projectPath, "tsconfig.json"))
124+
.setMainFileName(mainFile)
125+
.setOptions({ luaBundle: "tstl-out/bundle.lua", luaBundleEntry: mainFile })
126+
.expectToEqual({ result: "dependency3" });
127+
});
128+
});
129+
130+
describe("module resolution with sourceDir", () => {
131+
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-sourceDir");
132+
133+
test("can resolve dependencies with sourceDir", () => {
134+
util.testProject(path.join(projectPath, "tsconfig.json"))
135+
.setMainFileName(path.join(projectPath, "src", "main.ts"))
136+
.setOptions({ outDir: "tstl-out" })
137+
.expectToEqual({ result: "dependency3" });
138+
});
139+
140+
test("can resolve dependencies and bundle files with sourceDir", () => {
141+
const mainFile = path.join(projectPath, "src", "main.ts");
142+
util.testProject(path.join(projectPath, "tsconfig.json"))
143+
.setMainFileName(mainFile)
144+
.setOptions({ luaBundle: "bundle.lua", luaBundleEntry: mainFile })
145+
.expectToEqual({ result: "dependency3" });
146+
});
147+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as dependency1 from "dependency1";
2+
import { func } from "./subdir/otherfile";
3+
4+
export const result = dependency1.f1();
5+
export const result2 = func();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function func() {
2+
return "non-node_modules import";
3+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"moduleResolution": "Node",
5+
"noUnusedLocals": true,
6+
"noUnusedParameters": true,
7+
"target": "esnext",
8+
"lib": ["esnext"],
9+
"types": [],
10+
"rootDir": "src",
11+
"outDir": "tstl-out",
12+
}
13+
}

test/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ export abstract class TestBuilder {
385385
const { transpiledFiles } = this.getLuaResult();
386386
for (const transpiledFile of transpiledFiles) {
387387
if (transpiledFile.lua) {
388-
const filePath = path.relative(path.dirname(this.mainFileName), transpiledFile.outPath);
388+
const filePath = path.relative(this.options.outDir ?? this.getProgram().getCommonSourceDirectory(), transpiledFile.outPath);
389389
this.packagePreloadLuaFile(L, lua, lauxlib, filePath, transpiledFile.lua);
390390
}
391391
}

0 commit comments

Comments
 (0)