Skip to content

Commit

Permalink
fix: Project.getSourceFile - Getting a source file by relative path s…
Browse files Browse the repository at this point in the history
…hould work.

Previously it would return based on matching the path with all the descendant source files.
  • Loading branch information
dsherret committed Mar 13, 2018
1 parent 05364cf commit d117ecc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,11 @@ export class Project {
let searchFunction = fileNameOrSearchFunction as ((file: SourceFile) => boolean);

if (typeof fileNameOrSearchFunction === "string") {
if (FileUtils.pathIsAbsolute(fileNameOrSearchFunction))
return this.global.compilerFactory.getSourceFileFromCacheFromFilePath(fileNameOrSearchFunction);
const fileNameOrPath = FileUtils.standardizeSlashes(fileNameOrSearchFunction);
if (FileUtils.pathIsAbsolute(fileNameOrPath) || fileNameOrPath.indexOf("/") >= 0)
return this.global.compilerFactory.getSourceFileFromCacheFromFilePath(fileNameOrPath);
else
searchFunction = def => FileUtils.pathEndsWith(def.getFilePath(), fileNameOrSearchFunction);
searchFunction = def => FileUtils.pathEndsWith(def.getFilePath(), fileNameOrPath);
}

return ArrayUtils.find(this.global.compilerFactory.getSourceFilesByDirectoryDepth(), searchFunction);
Expand Down
21 changes: 21 additions & 0 deletions src/tests/projectTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,27 @@ describe(nameof(Project), () => {
expect(project.getSourceFile("file.ts")!.getFilePath()).to.equal(expectedFile.getFilePath());
});

it("should get the first match based on the directory structure when specifying a dot slash", () => {
const project = new Project({ useVirtualFileSystem: true });
project.createSourceFile("dir/file.ts");
const expectedFile = project.createSourceFile("file.ts");
expect(project.getSourceFile("./file.ts")!.getFilePath()).to.equal(expectedFile.getFilePath());
});

it("should get the first match based on the directory structure when using ../", () => {
const project = new Project({ useVirtualFileSystem: true });
const expectedFile = project.createSourceFile("dir/file.ts");
project.createSourceFile("file.ts");
expect(project.getSourceFile("dir/../dir/file.ts")!.getFilePath()).to.equal(expectedFile.getFilePath());
});

it("should get the first match based on a file name", () => {
const project = new Project({ useVirtualFileSystem: true });
project.createSourceFile("file.ts");
const expectedFile = project.createSourceFile("dir/file2.ts");
expect(project.getSourceFile("file2.ts")!.getFilePath()).to.equal(expectedFile.getFilePath());
});

it("should get when specifying an absolute path", () => {
const project = new Project({ useVirtualFileSystem: true });
project.createSourceFile("dir/file.ts");
Expand Down

0 comments on commit d117ecc

Please sign in to comment.