Use file names instead of paths for reading files

This commit is contained in:
Sheetal Nandi
2018-11-15 11:49:54 -08:00
parent 0a8c47bd45
commit 1db8bb062c
3 changed files with 18 additions and 20 deletions

View File

@@ -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<MappedPosition> | undefined;
let generatedMappings: SortedReadonlyArray<MappedPosition> | undefined;
let sourceMappings: ReadonlyArray<SortedReadonlyArray<SourceMappedPosition>> | 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)

View File

@@ -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;
}

View File

@@ -23,7 +23,7 @@ namespace ts {
): SourceMapper {
const getCanonicalFileName = createGetCanonicalFileName(host.useCaseSensitiveFileNames());
const currentDirectory = host.getCurrentDirectory();
const generatedFileCache = createMap<SourceFileLike | false>();
const sourceFileLike = createMap<SourceFileLike | false>();
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();
}
}
}