Skip to content

Commit

Permalink
fix: Directory.getSourceFile should only return source files currentl…
Browse files Browse the repository at this point in the history
…y existing within the cache.

It shouldn't return source files on the disk. That is done by addExistingSourceFile.
  • Loading branch information
dsherret committed Jan 2, 2018
1 parent 05a01d0 commit 8db2d84
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/fileSystem/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ export class Directory {
getSourceFile(pathOrCondition: string | ((sourceFile: SourceFile) => boolean)) {
if (typeof pathOrCondition === "string") {
const path = FileUtils.getStandardizedAbsolutePath(this.global.fileSystem, pathOrCondition, this.getPath());
return this.global.compilerFactory.getSourceFileFromFilePath(path);
if (this.global.compilerFactory.containsSourceFileAtPath(path))
return this.global.compilerFactory.getSourceFileFromFilePath(path);
else
return undefined;
}

return ArrayUtils.find(this.getSourceFiles(), pathOrCondition);
Expand Down
7 changes: 7 additions & 0 deletions src/tests/fileSystem/directoryTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,18 @@ describe(nameof(Directory), () => {
describe(nameof<Directory>(d => d.getSourceFile), () => {
const ast = getAst();
const directory = ast.createDirectory("dir");
const existingFile = directory.createSourceFile("existing-file.ts");
existingFile.saveSync();
existingFile.forget();
const child1 = directory.createSourceFile("child1.ts");
const child2 = directory.createSourceFile("child2.ts");
const subDir = directory.createDirectory("subDir");
const child3 = subDir.createSourceFile("child3.ts");

it("should not return a file that doesn't exist internally", () => {
expect(directory.getSourceFile("existing-file.ts")).to.be.undefined;
});

it("should get based on the name", () => {
expect(directory.getSourceFile("child2.ts")!.getFilePath()).to.equal(child2.getFilePath());
});
Expand Down

0 comments on commit 8db2d84

Please sign in to comment.