Skip to content

Commit

Permalink
Remove Program#getDeclarationEmitPath
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Hanson committed Jul 19, 2018
1 parent 7d5891a commit bd64c07
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/compiler/emitter.ts
Expand Up @@ -49,11 +49,11 @@ namespace ts {
return { jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath, bundleInfoPath };
}
else {
const jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, getOutputExtension(sourceFile, options));
const jsFilePath = getOwnEmitOutputFilePath(sourceFile.fileName, host, getOutputExtension(sourceFile, options));
const sourceMapFilePath = isJsonSourceFile(sourceFile) ? undefined : getSourceMapFilePath(jsFilePath, options);
// For legacy reasons (ie, we have baselines capturing the behavior), js files don't report a .d.ts output path - this would only matter if `declaration` and `allowJs` were both on, which is currently an error
const isJs = isSourceFileJavaScript(sourceFile);
const declarationFilePath = ((forceDtsPaths || getEmitDeclarations(options)) && !isJs) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined;
const declarationFilePath = ((forceDtsPaths || getEmitDeclarations(options)) && !isJs) ? getDeclarationEmitOutputFilePath(sourceFile.fileName, host) : undefined;
const declarationMapPath = getAreDeclarationMapsEnabled(options) ? declarationFilePath + ".map" : undefined;
return { jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath, bundleInfoPath: undefined };
}
Expand Down
4 changes: 0 additions & 4 deletions src/compiler/program.ts
Expand Up @@ -738,10 +738,6 @@ namespace ts {
getDiagnosticsProducingTypeChecker,
getCommonSourceDirectory,
emit,
getDeclarationEmitPath: fileName => {
const file = getSourceFile(fileName);
return file && getDeclarationEmitOutputFilePath(file, getEmitHost());
},
getCurrentDirectory: () => currentDirectory,
getNodeCount: () => getDiagnosticsProducingTypeChecker().getNodeCount(),
getIdentifierCount: () => getDiagnosticsProducingTypeChecker().getIdentifierCount(),
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/sourcemap.ts
Expand Up @@ -170,7 +170,7 @@ namespace ts {
if (sourceFileOrBundle.kind === SyntaxKind.SourceFile) { // emitting single module file
// For modules or multiple emit files the mapRoot will have directory structure like the sources
// So if src\a.ts and src\lib\b.ts are compiled together user would be moving the maps into mapRoot\a.js.map and mapRoot\lib\b.js.map
sourceMapDir = getDirectoryPath(getSourceFilePathInNewDir(sourceFileOrBundle, host, sourceMapDir));
sourceMapDir = getDirectoryPath(getSourceFilePathInNewDir(sourceFileOrBundle.fileName, host, sourceMapDir));
}

if (!isRootedDiskPath(sourceMapDir) && !isUrl(sourceMapDir)) {
Expand Down
2 changes: 0 additions & 2 deletions src/compiler/types.ts
Expand Up @@ -2746,8 +2746,6 @@ namespace ts {
* will be invoked when writing the JavaScript and declaration files.
*/
emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult;
/* @internal */
getDeclarationEmitPath(fileName: string): string | undefined;

getOptionsDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
getGlobalDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
Expand Down
35 changes: 25 additions & 10 deletions src/compiler/utilities.ts
Expand Up @@ -3152,26 +3152,38 @@ namespace ts {
return referencePath ? ensurePathIsNonModuleName(extensionless) : extensionless;
}

export function getOwnEmitOutputFilePath(sourceFile: SourceFile, host: EmitHost, extension: string) {
export function getOwnEmitOutputFilePath(fileName: string, host: EmitHost, extension: string) {
const compilerOptions = host.getCompilerOptions();
let emitOutputFilePathWithoutExtension: string;
if (compilerOptions.outDir) {
emitOutputFilePathWithoutExtension = removeFileExtension(getSourceFilePathInNewDir(sourceFile, host, compilerOptions.outDir));
emitOutputFilePathWithoutExtension = removeFileExtension(getSourceFilePathInNewDir(fileName, host, compilerOptions.outDir));
}
else {
emitOutputFilePathWithoutExtension = removeFileExtension(sourceFile.fileName);
emitOutputFilePathWithoutExtension = removeFileExtension(fileName);
}

return emitOutputFilePathWithoutExtension + extension;
}

export function getDeclarationEmitOutputFilePath(sourceFile: SourceFile, host: EmitHost) {
export function getDeclarationEmitOutputFilePath(fileName: string, host: EmitHost) {
// TODO: GH#25810 following should work but makes the build break:
// return getDeclarationEmitOutputFilePathWorker(fileName, host.getCompilerOptions(), host.getCurrentDirectory(), host.getCommonSourceDirectory(), f => host.getCanonicalFileName(f));

const options = host.getCompilerOptions();
const outputDir = options.declarationDir || options.outDir; // Prefer declaration folder if specified

const path = outputDir
? getSourceFilePathInNewDir(sourceFile, host, outputDir)
: sourceFile.fileName;
? getSourceFilePathInNewDir(fileName, host, outputDir)
: fileName;
return removeFileExtension(path) + Extension.Dts;
}

export function getDeclarationEmitOutputFilePathWorker(fileName: string, options: CompilerOptions, currentDirectory: string, commonSourceDirectory: string, getCanonicalFileName: GetCanonicalFileName): string {
const outputDir = options.declarationDir || options.outDir; // Prefer declaration folder if specified

const path = outputDir
? getSourceFilePathInNewDirWorker(fileName, outputDir, currentDirectory, commonSourceDirectory, getCanonicalFileName)
: fileName;
return removeFileExtension(path) + Extension.Dts;
}

Expand Down Expand Up @@ -3213,10 +3225,13 @@ namespace ts {
return !(options.noEmitForJsFiles && isSourceFileJavaScript(sourceFile)) && !sourceFile.isDeclarationFile && !isSourceFileFromExternalLibrary(sourceFile);
}

export function getSourceFilePathInNewDir(sourceFile: SourceFile, host: EmitHost, newDirPath: string) {
let sourceFilePath = getNormalizedAbsolutePath(sourceFile.fileName, host.getCurrentDirectory());
const commonSourceDirectory = host.getCommonSourceDirectory();
const isSourceFileInCommonSourceDirectory = host.getCanonicalFileName(sourceFilePath).indexOf(host.getCanonicalFileName(commonSourceDirectory)) === 0;
export function getSourceFilePathInNewDir(fileName: string, host: EmitHost, newDirPath: string): string {
return getSourceFilePathInNewDirWorker(fileName, newDirPath, host.getCurrentDirectory(), host.getCommonSourceDirectory(), f => host.getCanonicalFileName(f));
}

export function getSourceFilePathInNewDirWorker(fileName: string, newDirPath: string, currentDirectory: string, commonSourceDirectory: string, getCanonicalFileName: GetCanonicalFileName): string {
let sourceFilePath = getNormalizedAbsolutePath(fileName, currentDirectory);
const isSourceFileInCommonSourceDirectory = getCanonicalFileName(sourceFilePath).indexOf(getCanonicalFileName(commonSourceDirectory)) === 0;
sourceFilePath = isSourceFileInCommonSourceDirectory ? sourceFilePath.substring(commonSourceDirectory.length) : sourceFilePath;
return combinePaths(newDirPath, sourceFilePath);
}
Expand Down
3 changes: 2 additions & 1 deletion src/services/sourcemaps.ts
Expand Up @@ -104,7 +104,8 @@ namespace ts {
}

function tryGetGeneratedLocation(info: sourcemaps.SourceMappableLocation): sourcemaps.SourceMappableLocation | undefined {
const declarationPath = getProgram().getDeclarationEmitPath(info.fileName);
const program = getProgram();
const declarationPath = getDeclarationEmitOutputFilePathWorker(info.fileName, program.getCompilerOptions(), currentDirectory, program.getCommonSourceDirectory(), getCanonicalFileName);
if (declarationPath === undefined) return undefined;
const declarationFile = getFile(declarationPath);
if (!declarationFile) return undefined;
Expand Down

0 comments on commit bd64c07

Please sign in to comment.