Skip to content

Commit d117ecc

Browse files
committed
fix: Project.getSourceFile - Getting a source file by relative path should work.
Previously it would return based on matching the path with all the descendant source files.
1 parent 05364cf commit d117ecc

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/Project.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,11 @@ export class Project {
326326
let searchFunction = fileNameOrSearchFunction as ((file: SourceFile) => boolean);
327327

328328
if (typeof fileNameOrSearchFunction === "string") {
329-
if (FileUtils.pathIsAbsolute(fileNameOrSearchFunction))
330-
return this.global.compilerFactory.getSourceFileFromCacheFromFilePath(fileNameOrSearchFunction);
329+
const fileNameOrPath = FileUtils.standardizeSlashes(fileNameOrSearchFunction);
330+
if (FileUtils.pathIsAbsolute(fileNameOrPath) || fileNameOrPath.indexOf("/") >= 0)
331+
return this.global.compilerFactory.getSourceFileFromCacheFromFilePath(fileNameOrPath);
331332
else
332-
searchFunction = def => FileUtils.pathEndsWith(def.getFilePath(), fileNameOrSearchFunction);
333+
searchFunction = def => FileUtils.pathEndsWith(def.getFilePath(), fileNameOrPath);
333334
}
334335

335336
return ArrayUtils.find(this.global.compilerFactory.getSourceFilesByDirectoryDepth(), searchFunction);

src/tests/projectTests.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,27 @@ describe(nameof(Project), () => {
511511
expect(project.getSourceFile("file.ts")!.getFilePath()).to.equal(expectedFile.getFilePath());
512512
});
513513

514+
it("should get the first match based on the directory structure when specifying a dot slash", () => {
515+
const project = new Project({ useVirtualFileSystem: true });
516+
project.createSourceFile("dir/file.ts");
517+
const expectedFile = project.createSourceFile("file.ts");
518+
expect(project.getSourceFile("./file.ts")!.getFilePath()).to.equal(expectedFile.getFilePath());
519+
});
520+
521+
it("should get the first match based on the directory structure when using ../", () => {
522+
const project = new Project({ useVirtualFileSystem: true });
523+
const expectedFile = project.createSourceFile("dir/file.ts");
524+
project.createSourceFile("file.ts");
525+
expect(project.getSourceFile("dir/../dir/file.ts")!.getFilePath()).to.equal(expectedFile.getFilePath());
526+
});
527+
528+
it("should get the first match based on a file name", () => {
529+
const project = new Project({ useVirtualFileSystem: true });
530+
project.createSourceFile("file.ts");
531+
const expectedFile = project.createSourceFile("dir/file2.ts");
532+
expect(project.getSourceFile("file2.ts")!.getFilePath()).to.equal(expectedFile.getFilePath());
533+
});
534+
514535
it("should get when specifying an absolute path", () => {
515536
const project = new Project({ useVirtualFileSystem: true });
516537
project.createSourceFile("dir/file.ts");

0 commit comments

Comments
 (0)