From 1db8bb062c1fda1f901352471d90816656abc9ee Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 15 Nov 2018 11:49:54 -0800 Subject: [PATCH] Use file names instead of paths for reading files --- src/compiler/sourcemap.ts | 9 +++------ src/compiler/types.ts | 2 +- src/services/sourcemaps.ts | 27 ++++++++++++++------------- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/compiler/sourcemap.ts b/src/compiler/sourcemap.ts index 9b74fb22d58..b3b4e1fbb5e 100644 --- a/src/compiler/sourcemap.ts +++ b/src/compiler/sourcemap.ts @@ -592,11 +592,9 @@ namespace ts { const mapDirectory = getDirectoryPath(mapPath); const sourceRoot = map.sourceRoot ? getNormalizedAbsolutePath(map.sourceRoot, mapDirectory) : mapDirectory; const generatedAbsoluteFilePath = getNormalizedAbsolutePath(map.file, mapDirectory); - const generatedCanonicalFilePath = host.getCanonicalFileName(generatedAbsoluteFilePath) as Path; - const generatedFile = host.getSourceFileLike(generatedCanonicalFilePath); + const generatedFile = host.getSourceFileLike(generatedAbsoluteFilePath); const sourceFileAbsolutePaths = map.sources.map(source => getNormalizedAbsolutePath(source, sourceRoot)); - const sourceFileCanonicalPaths = sourceFileAbsolutePaths.map(source => host.getCanonicalFileName(source) as Path); - const sourceToSourceIndexMap = createMapFromEntries(sourceFileCanonicalPaths.map((source, i) => [source, i] as [string, number])); + const sourceToSourceIndexMap = createMapFromEntries(sourceFileAbsolutePaths.map((source, i) => [host.getCanonicalFileName(source), i] as [string, number])); let decodedMappings: ReadonlyArray | undefined; let generatedMappings: SortedReadonlyArray | undefined; let sourceMappings: ReadonlyArray> | undefined; @@ -613,8 +611,7 @@ namespace ts { let source: string | undefined; let sourcePosition: number | undefined; if (isSourceMapping(mapping)) { - const sourceFilePath = sourceFileCanonicalPaths[mapping.sourceIndex]; - const sourceFile = host.getSourceFileLike(sourceFilePath); + const sourceFile = host.getSourceFileLike(sourceFileAbsolutePaths[mapping.sourceIndex]); source = map.sources[mapping.sourceIndex]; sourcePosition = sourceFile !== undefined ? getPositionOfLineAndCharacterWithEdits(sourceFile, mapping.sourceLine, mapping.sourceCharacter) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index f52d82a8a2f..4da96730a93 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5528,7 +5528,7 @@ namespace ts { /* @internal */ export interface DocumentPositionMapperHost { - getSourceFileLike(path: Path): SourceFileLike | undefined; + getSourceFileLike(fileName: string): SourceFileLike | undefined; getCanonicalFileName(path: string): string; log(text: string): void; } diff --git a/src/services/sourcemaps.ts b/src/services/sourcemaps.ts index c2b9dbfb46a..47f39d85136 100644 --- a/src/services/sourcemaps.ts +++ b/src/services/sourcemaps.ts @@ -23,7 +23,7 @@ namespace ts { ): SourceMapper { const getCanonicalFileName = createGetCanonicalFileName(host.useCaseSensitiveFileNames()); const currentDirectory = host.getCurrentDirectory(); - const generatedFileCache = createMap(); + const sourceFileLike = createMap(); return { tryGetSourcePosition, tryGetGeneratedPosition, toLineColumnOffset, clearCache }; function toPath(fileName: string) { @@ -31,7 +31,7 @@ namespace ts { } function scanForSourcemapURL(fileName: string) { - const mappedFile = generatedFileCache.get(toPath(fileName)); + const mappedFile = sourceFileLike.get(toPath(fileName)); if (!mappedFile) { return; } @@ -39,7 +39,7 @@ namespace ts { return tryGetSourceMappingURL(mappedFile.text, getLineStarts(mappedFile)); } - function convertDocumentToSourceMapper(file: { sourceMapper?: DocumentPositionMapper }, contents: string, mapFileName: string) { + function convertDocumentToSourceMapper(file: SourceFileLike, contents: string, mapFileName: string) { const map = tryParseRawSourceMap(contents); if (!map || !map.sources || !map.file || !map.mappings) { // obviously invalid map @@ -75,9 +75,9 @@ namespace ts { } possibleMapLocations.push(fileName + ".map"); for (const location of possibleMapLocations) { - const mapPath = ts.toPath(location, getDirectoryPath(fileName), getCanonicalFileName); - if (host.fileExists(mapPath)) { - return convertDocumentToSourceMapper(file, host.readFile(mapPath)!, mapPath); // TODO: GH#18217 + const mapFileName = getNormalizedAbsolutePath(location, getDirectoryPath(fileName)); + if (host.fileExists(mapFileName)) { + return convertDocumentToSourceMapper(file, host.readFile(mapFileName)!, mapFileName); // TODO: GH#18217 } } return file.sourceMapper = identitySourceMapConsumer; @@ -105,7 +105,7 @@ namespace ts { getDeclarationEmitOutputFilePathWorker(info.fileName, program.getCompilerOptions(), currentDirectory, program.getCommonSourceDirectory(), getCanonicalFileName); if (declarationPath === undefined) return undefined; - const declarationFile = getGeneratedFile(declarationPath); + const declarationFile = getSourceFileLikeFromCache(declarationPath); if (!declarationFile) return undefined; const newLoc = getSourceMapper(declarationPath, declarationFile).getGeneratedPosition(info); @@ -117,14 +117,14 @@ namespace ts { return program && program.getSourceFileByPath(toPath(fileName)); } - function getGeneratedFile(fileName: string): SourceFileLike | undefined { + function getSourceFileLikeFromCache(fileName: string): SourceFileLike | undefined { const path = toPath(fileName); - const fileFromCache = generatedFileCache.get(path); + const fileFromCache = sourceFileLike.get(path); if (fileFromCache !== undefined) return fileFromCache ? fileFromCache : undefined; // TODO: should ask host instead? if (!host.fileExists(path)) { - generatedFileCache.set(path, false); + sourceFileLike.set(path, false); return undefined; } @@ -137,14 +137,15 @@ namespace ts { return computeLineAndCharacterOfPosition(getLineStarts(this as SourceFileLike), pos); } } : false; - generatedFileCache.set(path, file); + sourceFileLike.set(path, file); return file ? file : undefined; } + // This can be called from source mapper in either source program or program that includes generated file function getSourceFileLike(fileName: string) { const sourceFile = getSourceFile(fileName); // file returned here could be .d.ts when asked for .ts file if projectReferences and module resolution created this source file - return sourceFile && sourceFile.resolvedPath === toPath(fileName) ? sourceFile : getGeneratedFile(fileName); + return sourceFile && sourceFile.resolvedPath === toPath(fileName) ? sourceFile : getSourceFileLikeFromCache(fileName); } function toLineColumnOffset(fileName: string, position: number): LineAndCharacter { @@ -153,7 +154,7 @@ namespace ts { } function clearCache(): void { - generatedFileCache.clear(); + sourceFileLike.clear(); } } }