Skip to content

Commit 1e539f0

Browse files
authored
Also resolve tsx files (#1039)
1 parent c6e3555 commit 1e539f0

File tree

6 files changed

+46
-14
lines changed

6 files changed

+46
-14
lines changed

src/transpilation/resolve.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,18 @@ function resolveDependency(
103103
// Check if file is a file in the project
104104
const resolvedPath = path.join(fileDirectory, dependency);
105105

106-
if (isProjectFile(resolvedPath, program)) {
107-
// JSON files need their extension as part of the import path, caught by this branch
108-
return resolvedPath;
109-
}
110-
111-
const resolvedFile = resolvedPath + ".ts";
112-
if (isProjectFile(resolvedFile, program)) {
113-
return resolvedFile;
114-
}
115-
116-
const projectIndexPath = path.resolve(resolvedPath, "index.ts");
117-
if (isProjectFile(projectIndexPath, program)) {
118-
return projectIndexPath;
106+
const possibleProjectFiles = [
107+
resolvedPath, // JSON files need their extension as part of the import path, caught by this branch,
108+
resolvedPath + ".ts", // Regular ts file
109+
path.join(resolvedPath, "index.ts"), // Index ts file,
110+
resolvedPath + ".tsx", // tsx file
111+
path.join(resolvedPath, "index.tsx"), // tsx index
112+
];
113+
114+
for (const possibleFile of possibleProjectFiles) {
115+
if (isProjectFile(possibleFile, program)) {
116+
return possibleFile;
117+
}
119118
}
120119

121120
// Check if this is a sibling of a required lua file
@@ -215,7 +214,9 @@ function isProjectFile(file: string, program: ts.Program): boolean {
215214
function hasSourceFileInProject(filePath: string, program: ts.Program) {
216215
const pathWithoutExtension = trimExtension(filePath);
217216
return (
218-
isProjectFile(pathWithoutExtension + ".ts", program) || isProjectFile(pathWithoutExtension + ".json", program)
217+
isProjectFile(pathWithoutExtension + ".ts", program) ||
218+
isProjectFile(pathWithoutExtension + ".tsx", program) ||
219+
isProjectFile(pathWithoutExtension + ".json", program)
219220
);
220221
}
221222

test/transpile/module-resolution.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,18 @@ describe("module resolution project with dependencies built by tstl library mode
280280
.expectToEqual(expectedResult);
281281
});
282282
});
283+
284+
// Test fix for https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1037
285+
describe("module resolution with tsx", () => {
286+
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-tsx");
287+
288+
test("project with tsx files", () => {
289+
util.testProject(path.join(projectPath, "tsconfig.json"))
290+
.setMainFileName(path.join(projectPath, "main.tsx"))
291+
.debug()
292+
.expectToEqual({
293+
result: "hello from other.tsx",
294+
indexResult: "hello from dir/index.tsx",
295+
});
296+
});
297+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function indexf() {
2+
return "hello from dir/index.tsx";
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { f } from "./other";
2+
import { indexf } from "./dir";
3+
4+
export const result = f();
5+
export const indexResult = indexf();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function f() {
2+
return "hello from other.tsx";
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "react"
4+
}
5+
}

0 commit comments

Comments
 (0)