fix(compiler-cli): ensure LogicalProjectPaths always start with a slash (#29627)

Previously, if a matching rootDir ended with a slash then the path
returned from `logicalPathOfFile()` would not start with a slash,
which is inconsistent.

PR Close #29627
This commit is contained in:
Pete Bacon Darwin 2019-04-01 08:54:07 +01:00 committed by Igor Minar
parent ec56354306
commit e02684e609
2 changed files with 17 additions and 1 deletions

View File

@ -81,7 +81,7 @@ export class LogicalFileSystem {
let logicalFile: LogicalProjectPath|null = null;
for (const rootDir of this.rootDirs) {
if (physicalFile.startsWith(rootDir)) {
logicalFile = stripExtension(physicalFile.substr(rootDir.length)) as LogicalProjectPath;
logicalFile = this.createLogicalProjectPath(physicalFile, rootDir);
// The logical project does not include any special "node_modules" nested directories.
if (logicalFile.indexOf('/node_modules/') !== -1) {
logicalFile = null;
@ -94,4 +94,10 @@ export class LogicalFileSystem {
}
return this.cache.get(physicalFile) !;
}
private createLogicalProjectPath(file: AbsoluteFsPath, rootDir: AbsoluteFsPath):
LogicalProjectPath {
const logicalPath = stripExtension(file.substr(rootDir.length));
return (logicalPath.startsWith('/') ? logicalPath : '/' + logicalPath) as LogicalProjectPath;
}
}

View File

@ -31,6 +31,16 @@ describe('logical paths', () => {
expect(fs.logicalPathOfFile(abs('/test/foo.ts'))).toEqual('/foo' as LogicalProjectPath);
expect(fs.logicalPathOfFile(abs('/test/dist/foo.ts'))).toEqual('/foo' as LogicalProjectPath);
});
it('should always return `/` prefixed logical paths', () => {
const rootFs = new LogicalFileSystem([abs('/')]);
expect(rootFs.logicalPathOfFile(abs('/foo/foo.ts')))
.toEqual('/foo/foo' as LogicalProjectPath);
const nonRootFs = new LogicalFileSystem([abs('/test/')]);
expect(nonRootFs.logicalPathOfFile(abs('/test/foo/foo.ts')))
.toEqual('/foo/foo' as LogicalProjectPath);
});
});
describe('utilities', () => {