Skip to content

Commit

Permalink
fixed source file discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
milux committed Apr 4, 2018
1 parent 663b6b3 commit 79bc163
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions source/rules/rxjsAddRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,31 +163,35 @@ class Walker extends UsedWalker {

private findSourceFile(file: string): ts.SourceFile {

let message = "Cannot find 'tsconfig.json'";
const program = this.getProgram();
const rootFiles = program.getRootFileNames();
const fileDirCandidates = new Set<string>();

for (let i = 0, length = rootFiles.length; i < length; ++i) {

const configFile = ts.findConfigFile(
this.normalizeFile(path.dirname(rootFiles[i])),
ts.sys.fileExists
);
if (configFile) {
if (configFile && !fileDirCandidates.has(path.dirname(configFile))) {
const resolvedFile = this.normalizeFile(
path.resolve(path.dirname(configFile), file)
);
const sourceFile = program.getSourceFileByPath(resolvedFile as any);
if (sourceFile) {
return sourceFile;
} else {
message = `Cannot find '${resolvedFile}' in the compiled program. Has it been imported?`;
break;
fileDirCandidates.add(path.dirname(configFile));
}
}
}

throw new Error(message);
if (fileDirCandidates.size === 0) {
throw new Error("Cannot find 'tsconfig.json'");
} else {
throw new Error(`Cannot find an import of '${file}' from any of these locations: `
+ `[${Array.from(fileDirCandidates.values()).join(", ")}]. Has it been imported?`);
}
}

private normalizeFile(file: string): string {
Expand Down

0 comments on commit 79bc163

Please sign in to comment.