( fileName: string, rootDirs: readonly string[], compilerHost: Pick<ts.CompilerHost, 'getCanonicalFileName'>, )
| 27 | * @returns |
| 28 | */ |
| 29 | export function getProjectRelativePath( |
| 30 | fileName: string, |
| 31 | rootDirs: readonly string[], |
| 32 | compilerHost: Pick<ts.CompilerHost, 'getCanonicalFileName'>, |
| 33 | ): string | null { |
| 34 | // Note: we need to pass both the file name and the root directories through getCanonicalFileName, |
| 35 | // because the root directories might've been passed through it already while the source files |
| 36 | // definitely have not. This can break the relative return value, because in some platforms |
| 37 | // getCanonicalFileName lowercases the path. |
| 38 | const filePath = compilerHost.getCanonicalFileName(fileName); |
| 39 | |
| 40 | for (const rootDir of rootDirs) { |
| 41 | const rel = relative(compilerHost.getCanonicalFileName(rootDir), filePath); |
| 42 | if (!rel.startsWith('..')) { |
| 43 | return rel; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return null; |
| 48 | } |
no test coverage detected