Skip to content

Commit

Permalink
Factor out repeated code and call one from another
Browse files Browse the repository at this point in the history
Signed-off-by: Hana Joo <hanajoo@google.com>
  • Loading branch information
h-joo committed Nov 29, 2023
1 parent 4e01096 commit 8e86481
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 129 deletions.
123 changes: 59 additions & 64 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ import {
SourceFilePrologueInfo,
SourceMapEmitResult,
SourceMapGenerator,
SourceMapOptions,
SourceMapSource,
SpreadAssignment,
SpreadElement,
Expand Down Expand Up @@ -721,6 +722,62 @@ export function getOutputFileNames(commandLine: ParsedCommandLine, inputFileName
return getOutputs();
}

/** @internal */
export function getSourceMapDirectory(host: EmitHost, mapOptions: SourceMapOptions, filePath: string, sourceFile: SourceFile | undefined) {
if (mapOptions.sourceRoot) return host.getCommonSourceDirectory();
if (mapOptions.mapRoot) {
let sourceMapDir = normalizeSlashes(mapOptions.mapRoot);
if (sourceFile) {
// 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(sourceFile.fileName, host, sourceMapDir));
}
if (getRootLength(sourceMapDir) === 0) {
// The relative paths are relative to the common directory
sourceMapDir = combinePaths(host.getCommonSourceDirectory(), sourceMapDir);
}
return sourceMapDir;
}
return getDirectoryPath(normalizePath(filePath));
}

/** @internal */
export function getSourceMappingURL(host: EmitHost, mapOptions: SourceMapOptions, sourceMapGenerator: SourceMapGenerator, filePath: string, sourceMapFilePath: string | undefined, sourceFile: SourceFile | undefined) {
if (mapOptions.inlineSourceMap) {
// Encode the sourceMap into the sourceMap url
const sourceMapText = sourceMapGenerator.toString();
const base64SourceMapText = base64encode(sys, sourceMapText);
return `data:application/json;base64,${base64SourceMapText}`;
}

const sourceMapFile = getBaseFileName(normalizeSlashes(Debug.checkDefined(sourceMapFilePath)));
if (mapOptions.mapRoot) {
let sourceMapDir = normalizeSlashes(mapOptions.mapRoot);
if (sourceFile) {
// 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(sourceFile.fileName, host, sourceMapDir));
}
if (getRootLength(sourceMapDir) === 0) {
// The relative paths are relative to the common directory
sourceMapDir = combinePaths(host.getCommonSourceDirectory(), sourceMapDir);
return encodeURI(
getRelativePathToDirectoryOrUrl(
getDirectoryPath(normalizePath(filePath)), // get the relative sourceMapDir path based on jsFilePath
combinePaths(sourceMapDir, sourceMapFile), // this is where user expects to see sourceMap
host.getCurrentDirectory(),
host.getCanonicalFileName,
/*isAbsolutePathAnUrl*/ true,
),
);
}
else {
return encodeURI(combinePaths(sourceMapDir, sourceMapFile));
}
}
return encodeURI(sourceMapFile);
}

/** @internal */
export function getFirstProjectOutput(configFile: ParsedCommandLine, ignoreCase: boolean): string {
if (outFile(configFile.options)) {
Expand Down Expand Up @@ -982,7 +1039,7 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
host,
getBaseFileName(normalizeSlashes(jsFilePath)),
getSourceRoot(mapOptions),
getSourceMapDirectory(mapOptions, jsFilePath, sourceFile),
getSourceMapDirectory(host, mapOptions, jsFilePath, sourceFile),
mapOptions,
);
}
Expand All @@ -1004,6 +1061,7 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
}

const sourceMappingURL = getSourceMappingURL(
host,
mapOptions,
sourceMapGenerator,
jsFilePath,
Expand Down Expand Up @@ -1039,15 +1097,6 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
writer.clear();
}

interface SourceMapOptions {
sourceMap?: boolean;
inlineSourceMap?: boolean;
inlineSources?: boolean;
sourceRoot?: string;
mapRoot?: string;
extendedDiagnostics?: boolean;
}

function shouldEmitSourceMaps(mapOptions: SourceMapOptions, sourceFileOrBundle: SourceFile | Bundle) {
return (mapOptions.sourceMap || mapOptions.inlineSourceMap)
&& (sourceFileOrBundle.kind !== SyntaxKind.SourceFile || !fileExtensionIs(sourceFileOrBundle.fileName, Extension.Json));
Expand All @@ -1059,60 +1108,6 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
const sourceRoot = normalizeSlashes(mapOptions.sourceRoot || "");
return sourceRoot ? ensureTrailingDirectorySeparator(sourceRoot) : sourceRoot;
}

function getSourceMapDirectory(mapOptions: SourceMapOptions, filePath: string, sourceFile: SourceFile | undefined) {
if (mapOptions.sourceRoot) return host.getCommonSourceDirectory();
if (mapOptions.mapRoot) {
let sourceMapDir = normalizeSlashes(mapOptions.mapRoot);
if (sourceFile) {
// 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(sourceFile.fileName, host, sourceMapDir));
}
if (getRootLength(sourceMapDir) === 0) {
// The relative paths are relative to the common directory
sourceMapDir = combinePaths(host.getCommonSourceDirectory(), sourceMapDir);
}
return sourceMapDir;
}
return getDirectoryPath(normalizePath(filePath));
}

function getSourceMappingURL(mapOptions: SourceMapOptions, sourceMapGenerator: SourceMapGenerator, filePath: string, sourceMapFilePath: string | undefined, sourceFile: SourceFile | undefined) {
if (mapOptions.inlineSourceMap) {
// Encode the sourceMap into the sourceMap url
const sourceMapText = sourceMapGenerator.toString();
const base64SourceMapText = base64encode(sys, sourceMapText);
return `data:application/json;base64,${base64SourceMapText}`;
}

const sourceMapFile = getBaseFileName(normalizeSlashes(Debug.checkDefined(sourceMapFilePath)));
if (mapOptions.mapRoot) {
let sourceMapDir = normalizeSlashes(mapOptions.mapRoot);
if (sourceFile) {
// 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(sourceFile.fileName, host, sourceMapDir));
}
if (getRootLength(sourceMapDir) === 0) {
// The relative paths are relative to the common directory
sourceMapDir = combinePaths(host.getCommonSourceDirectory(), sourceMapDir);
return encodeURI(
getRelativePathToDirectoryOrUrl(
getDirectoryPath(normalizePath(filePath)), // get the relative sourceMapDir path based on jsFilePath
combinePaths(sourceMapDir, sourceMapFile), // this is where user expects to see sourceMap
host.getCurrentDirectory(),
host.getCanonicalFileName,
/*isAbsolutePathAnUrl*/ true,
),
);
}
else {
return encodeURI(combinePaths(sourceMapDir, sourceMapFile));
}
}
return encodeURI(sourceMapFile);
}
}

/** @internal */
Expand Down
69 changes: 4 additions & 65 deletions src/compiler/transformers/declarations/transpileDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
base64encode,
combinePaths,
CompilerOptions,
createEmitDeclarationResolver,
createGetCanonicalFileName,
Expand All @@ -15,16 +13,12 @@ import {
getAreDeclarationMapsEnabled,
getBaseFileName,
getDeclarationEmitOutputFilePathWorker,
getDirectoryPath,
getNewLineCharacter,
getRelativePathToDirectoryOrUrl,
getRootLength,
getSourceFilePathInNewDir,
normalizePath,
getSourceMapDirectory,
getSourceMappingURL,
normalizeSlashes,
PrinterOptions,
SourceFile,
SourceMapGenerator,
sys,
TransformationContext,
transformDeclarations,
Expand Down Expand Up @@ -126,25 +120,6 @@ export function transpileDeclaration(sourceFile: SourceFile, transpileOptions: T
diagnostics,
};

// logic replicated from emitter.ts
function getSourceMapDirectory(mapOptions: CompilerOptions, filePath: string, sourceFile: SourceFile | undefined) {
if (mapOptions.sourceRoot) return emitHost.getCommonSourceDirectory();
if (mapOptions.mapRoot) {
let sourceMapDir = normalizeSlashes(mapOptions.mapRoot);
if (sourceFile) {
// 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(sourceFile.fileName, emitHost as unknown as EmitHost, sourceMapDir));
}
if (getRootLength(sourceMapDir) === 0) {
// The relative paths are relative to the common directory
sourceMapDir = combinePaths(emitHost.getCommonSourceDirectory(), sourceMapDir);
}
return sourceMapDir;
}
return getDirectoryPath(normalizePath(filePath));
}

// logic replicated from emitter.ts
function getSourceMapGenerator(declarationFilePath: string, declarationMapPath: string) {
if (!getAreDeclarationMapsEnabled(compilerOptions)) return;
Expand All @@ -161,11 +136,12 @@ export function transpileDeclaration(sourceFile: SourceFile, transpileOptions: T
emitHost,
getBaseFileName(normalizeSlashes(declarationFilePath)),
sourceRoot ? ensureTrailingDirectorySeparator(sourceRoot) : sourceRoot,
getSourceMapDirectory(compilerOptions, declarationFilePath, sourceFile),
getSourceMapDirectory(emitHost, compilerOptions, declarationFilePath, sourceFile),
mapOptions,
);

const sourceMappingURL = getSourceMappingURL(
emitHost,
mapOptions,
sourceMapGenerator,
declarationFilePath,
Expand All @@ -174,41 +150,4 @@ export function transpileDeclaration(sourceFile: SourceFile, transpileOptions: T
);
return { sourceMapGenerator, sourceMappingURL: `//# ${"sourceMappingURL"}=${sourceMappingURL}` };
}

// logic replicated from emitter.ts
function getSourceMappingURL(mapOptions: CompilerOptions, sourceMapGenerator: SourceMapGenerator, filePath: string, sourceMapFilePath: string | undefined, sourceFile: SourceFile | undefined) {
if (mapOptions.inlineSourceMap) {
// Encode the sourceMap into the sourceMap url
const sourceMapText = sourceMapGenerator.toString();
const base64SourceMapText = base64encode(sys, sourceMapText);
return `data:application/json;base64,${base64SourceMapText}`;
}

const sourceMapFile = getBaseFileName(normalizeSlashes(Debug.checkDefined(sourceMapFilePath)));
if (mapOptions.mapRoot) {
let sourceMapDir = normalizeSlashes(mapOptions.mapRoot);
if (sourceFile) {
// 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(sourceFile.fileName, emitHost as unknown as EmitHost, sourceMapDir));
}
if (getRootLength(sourceMapDir) === 0) {
// The relative paths are relative to the common directory
sourceMapDir = combinePaths(emitHost.getCommonSourceDirectory(), sourceMapDir);
return encodeURI(
getRelativePathToDirectoryOrUrl(
getDirectoryPath(normalizePath(filePath)), // get the relative sourceMapDir path based on jsFilePath
combinePaths(sourceMapDir, sourceMapFile), // this is where user expects to see sourceMap
emitHost.getCurrentDirectory(),
emitHost.getCanonicalFileName,
/*isAbsolutePathAnUrl*/ true,
),
);
}
else {
return encodeURI(combinePaths(sourceMapDir, sourceMapFile));
}
}
return encodeURI(sourceMapFile);
}
}
10 changes: 10 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9630,6 +9630,16 @@ export interface SourceMapGenerator {
toString(): string;
}

/** @internal */
export interface SourceMapOptions {
sourceMap?: boolean;
inlineSourceMap?: boolean;
inlineSources?: boolean;
sourceRoot?: string;
mapRoot?: string;
extendedDiagnostics?: boolean;
}

/** @internal */
export interface DocumentPositionMapperHost {
getSourceFileLike(fileName: string): SourceFileLike | undefined;
Expand Down

0 comments on commit 8e86481

Please sign in to comment.