Skip to content

Commit 8e86481

Browse files
committed
Factor out repeated code and call one from another
Signed-off-by: Hana Joo <[email protected]>
1 parent 4e01096 commit 8e86481

File tree

3 files changed

+73
-129
lines changed

3 files changed

+73
-129
lines changed

src/compiler/emitter.ts

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ import {
402402
SourceFilePrologueInfo,
403403
SourceMapEmitResult,
404404
SourceMapGenerator,
405+
SourceMapOptions,
405406
SourceMapSource,
406407
SpreadAssignment,
407408
SpreadElement,
@@ -721,6 +722,62 @@ export function getOutputFileNames(commandLine: ParsedCommandLine, inputFileName
721722
return getOutputs();
722723
}
723724

725+
/** @internal */
726+
export function getSourceMapDirectory(host: EmitHost, mapOptions: SourceMapOptions, filePath: string, sourceFile: SourceFile | undefined) {
727+
if (mapOptions.sourceRoot) return host.getCommonSourceDirectory();
728+
if (mapOptions.mapRoot) {
729+
let sourceMapDir = normalizeSlashes(mapOptions.mapRoot);
730+
if (sourceFile) {
731+
// For modules or multiple emit files the mapRoot will have directory structure like the sources
732+
// 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
733+
sourceMapDir = getDirectoryPath(getSourceFilePathInNewDir(sourceFile.fileName, host, sourceMapDir));
734+
}
735+
if (getRootLength(sourceMapDir) === 0) {
736+
// The relative paths are relative to the common directory
737+
sourceMapDir = combinePaths(host.getCommonSourceDirectory(), sourceMapDir);
738+
}
739+
return sourceMapDir;
740+
}
741+
return getDirectoryPath(normalizePath(filePath));
742+
}
743+
744+
/** @internal */
745+
export function getSourceMappingURL(host: EmitHost, mapOptions: SourceMapOptions, sourceMapGenerator: SourceMapGenerator, filePath: string, sourceMapFilePath: string | undefined, sourceFile: SourceFile | undefined) {
746+
if (mapOptions.inlineSourceMap) {
747+
// Encode the sourceMap into the sourceMap url
748+
const sourceMapText = sourceMapGenerator.toString();
749+
const base64SourceMapText = base64encode(sys, sourceMapText);
750+
return `data:application/json;base64,${base64SourceMapText}`;
751+
}
752+
753+
const sourceMapFile = getBaseFileName(normalizeSlashes(Debug.checkDefined(sourceMapFilePath)));
754+
if (mapOptions.mapRoot) {
755+
let sourceMapDir = normalizeSlashes(mapOptions.mapRoot);
756+
if (sourceFile) {
757+
// For modules or multiple emit files the mapRoot will have directory structure like the sources
758+
// 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
759+
sourceMapDir = getDirectoryPath(getSourceFilePathInNewDir(sourceFile.fileName, host, sourceMapDir));
760+
}
761+
if (getRootLength(sourceMapDir) === 0) {
762+
// The relative paths are relative to the common directory
763+
sourceMapDir = combinePaths(host.getCommonSourceDirectory(), sourceMapDir);
764+
return encodeURI(
765+
getRelativePathToDirectoryOrUrl(
766+
getDirectoryPath(normalizePath(filePath)), // get the relative sourceMapDir path based on jsFilePath
767+
combinePaths(sourceMapDir, sourceMapFile), // this is where user expects to see sourceMap
768+
host.getCurrentDirectory(),
769+
host.getCanonicalFileName,
770+
/*isAbsolutePathAnUrl*/ true,
771+
),
772+
);
773+
}
774+
else {
775+
return encodeURI(combinePaths(sourceMapDir, sourceMapFile));
776+
}
777+
}
778+
return encodeURI(sourceMapFile);
779+
}
780+
724781
/** @internal */
725782
export function getFirstProjectOutput(configFile: ParsedCommandLine, ignoreCase: boolean): string {
726783
if (outFile(configFile.options)) {
@@ -982,7 +1039,7 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
9821039
host,
9831040
getBaseFileName(normalizeSlashes(jsFilePath)),
9841041
getSourceRoot(mapOptions),
985-
getSourceMapDirectory(mapOptions, jsFilePath, sourceFile),
1042+
getSourceMapDirectory(host, mapOptions, jsFilePath, sourceFile),
9861043
mapOptions,
9871044
);
9881045
}
@@ -1004,6 +1061,7 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
10041061
}
10051062

10061063
const sourceMappingURL = getSourceMappingURL(
1064+
host,
10071065
mapOptions,
10081066
sourceMapGenerator,
10091067
jsFilePath,
@@ -1039,15 +1097,6 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
10391097
writer.clear();
10401098
}
10411099

1042-
interface SourceMapOptions {
1043-
sourceMap?: boolean;
1044-
inlineSourceMap?: boolean;
1045-
inlineSources?: boolean;
1046-
sourceRoot?: string;
1047-
mapRoot?: string;
1048-
extendedDiagnostics?: boolean;
1049-
}
1050-
10511100
function shouldEmitSourceMaps(mapOptions: SourceMapOptions, sourceFileOrBundle: SourceFile | Bundle) {
10521101
return (mapOptions.sourceMap || mapOptions.inlineSourceMap)
10531102
&& (sourceFileOrBundle.kind !== SyntaxKind.SourceFile || !fileExtensionIs(sourceFileOrBundle.fileName, Extension.Json));
@@ -1059,60 +1108,6 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
10591108
const sourceRoot = normalizeSlashes(mapOptions.sourceRoot || "");
10601109
return sourceRoot ? ensureTrailingDirectorySeparator(sourceRoot) : sourceRoot;
10611110
}
1062-
1063-
function getSourceMapDirectory(mapOptions: SourceMapOptions, filePath: string, sourceFile: SourceFile | undefined) {
1064-
if (mapOptions.sourceRoot) return host.getCommonSourceDirectory();
1065-
if (mapOptions.mapRoot) {
1066-
let sourceMapDir = normalizeSlashes(mapOptions.mapRoot);
1067-
if (sourceFile) {
1068-
// For modules or multiple emit files the mapRoot will have directory structure like the sources
1069-
// 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
1070-
sourceMapDir = getDirectoryPath(getSourceFilePathInNewDir(sourceFile.fileName, host, sourceMapDir));
1071-
}
1072-
if (getRootLength(sourceMapDir) === 0) {
1073-
// The relative paths are relative to the common directory
1074-
sourceMapDir = combinePaths(host.getCommonSourceDirectory(), sourceMapDir);
1075-
}
1076-
return sourceMapDir;
1077-
}
1078-
return getDirectoryPath(normalizePath(filePath));
1079-
}
1080-
1081-
function getSourceMappingURL(mapOptions: SourceMapOptions, sourceMapGenerator: SourceMapGenerator, filePath: string, sourceMapFilePath: string | undefined, sourceFile: SourceFile | undefined) {
1082-
if (mapOptions.inlineSourceMap) {
1083-
// Encode the sourceMap into the sourceMap url
1084-
const sourceMapText = sourceMapGenerator.toString();
1085-
const base64SourceMapText = base64encode(sys, sourceMapText);
1086-
return `data:application/json;base64,${base64SourceMapText}`;
1087-
}
1088-
1089-
const sourceMapFile = getBaseFileName(normalizeSlashes(Debug.checkDefined(sourceMapFilePath)));
1090-
if (mapOptions.mapRoot) {
1091-
let sourceMapDir = normalizeSlashes(mapOptions.mapRoot);
1092-
if (sourceFile) {
1093-
// For modules or multiple emit files the mapRoot will have directory structure like the sources
1094-
// 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
1095-
sourceMapDir = getDirectoryPath(getSourceFilePathInNewDir(sourceFile.fileName, host, sourceMapDir));
1096-
}
1097-
if (getRootLength(sourceMapDir) === 0) {
1098-
// The relative paths are relative to the common directory
1099-
sourceMapDir = combinePaths(host.getCommonSourceDirectory(), sourceMapDir);
1100-
return encodeURI(
1101-
getRelativePathToDirectoryOrUrl(
1102-
getDirectoryPath(normalizePath(filePath)), // get the relative sourceMapDir path based on jsFilePath
1103-
combinePaths(sourceMapDir, sourceMapFile), // this is where user expects to see sourceMap
1104-
host.getCurrentDirectory(),
1105-
host.getCanonicalFileName,
1106-
/*isAbsolutePathAnUrl*/ true,
1107-
),
1108-
);
1109-
}
1110-
else {
1111-
return encodeURI(combinePaths(sourceMapDir, sourceMapFile));
1112-
}
1113-
}
1114-
return encodeURI(sourceMapFile);
1115-
}
11161111
}
11171112

11181113
/** @internal */

src/compiler/transformers/declarations/transpileDeclaration.ts

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import {
2-
base64encode,
3-
combinePaths,
42
CompilerOptions,
53
createEmitDeclarationResolver,
64
createGetCanonicalFileName,
@@ -15,16 +13,12 @@ import {
1513
getAreDeclarationMapsEnabled,
1614
getBaseFileName,
1715
getDeclarationEmitOutputFilePathWorker,
18-
getDirectoryPath,
1916
getNewLineCharacter,
20-
getRelativePathToDirectoryOrUrl,
21-
getRootLength,
22-
getSourceFilePathInNewDir,
23-
normalizePath,
17+
getSourceMapDirectory,
18+
getSourceMappingURL,
2419
normalizeSlashes,
2520
PrinterOptions,
2621
SourceFile,
27-
SourceMapGenerator,
2822
sys,
2923
TransformationContext,
3024
transformDeclarations,
@@ -126,25 +120,6 @@ export function transpileDeclaration(sourceFile: SourceFile, transpileOptions: T
126120
diagnostics,
127121
};
128122

129-
// logic replicated from emitter.ts
130-
function getSourceMapDirectory(mapOptions: CompilerOptions, filePath: string, sourceFile: SourceFile | undefined) {
131-
if (mapOptions.sourceRoot) return emitHost.getCommonSourceDirectory();
132-
if (mapOptions.mapRoot) {
133-
let sourceMapDir = normalizeSlashes(mapOptions.mapRoot);
134-
if (sourceFile) {
135-
// For modules or multiple emit files the mapRoot will have directory structure like the sources
136-
// 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
137-
sourceMapDir = getDirectoryPath(getSourceFilePathInNewDir(sourceFile.fileName, emitHost as unknown as EmitHost, sourceMapDir));
138-
}
139-
if (getRootLength(sourceMapDir) === 0) {
140-
// The relative paths are relative to the common directory
141-
sourceMapDir = combinePaths(emitHost.getCommonSourceDirectory(), sourceMapDir);
142-
}
143-
return sourceMapDir;
144-
}
145-
return getDirectoryPath(normalizePath(filePath));
146-
}
147-
148123
// logic replicated from emitter.ts
149124
function getSourceMapGenerator(declarationFilePath: string, declarationMapPath: string) {
150125
if (!getAreDeclarationMapsEnabled(compilerOptions)) return;
@@ -161,11 +136,12 @@ export function transpileDeclaration(sourceFile: SourceFile, transpileOptions: T
161136
emitHost,
162137
getBaseFileName(normalizeSlashes(declarationFilePath)),
163138
sourceRoot ? ensureTrailingDirectorySeparator(sourceRoot) : sourceRoot,
164-
getSourceMapDirectory(compilerOptions, declarationFilePath, sourceFile),
139+
getSourceMapDirectory(emitHost, compilerOptions, declarationFilePath, sourceFile),
165140
mapOptions,
166141
);
167142

168143
const sourceMappingURL = getSourceMappingURL(
144+
emitHost,
169145
mapOptions,
170146
sourceMapGenerator,
171147
declarationFilePath,
@@ -174,41 +150,4 @@ export function transpileDeclaration(sourceFile: SourceFile, transpileOptions: T
174150
);
175151
return { sourceMapGenerator, sourceMappingURL: `//# ${"sourceMappingURL"}=${sourceMappingURL}` };
176152
}
177-
178-
// logic replicated from emitter.ts
179-
function getSourceMappingURL(mapOptions: CompilerOptions, sourceMapGenerator: SourceMapGenerator, filePath: string, sourceMapFilePath: string | undefined, sourceFile: SourceFile | undefined) {
180-
if (mapOptions.inlineSourceMap) {
181-
// Encode the sourceMap into the sourceMap url
182-
const sourceMapText = sourceMapGenerator.toString();
183-
const base64SourceMapText = base64encode(sys, sourceMapText);
184-
return `data:application/json;base64,${base64SourceMapText}`;
185-
}
186-
187-
const sourceMapFile = getBaseFileName(normalizeSlashes(Debug.checkDefined(sourceMapFilePath)));
188-
if (mapOptions.mapRoot) {
189-
let sourceMapDir = normalizeSlashes(mapOptions.mapRoot);
190-
if (sourceFile) {
191-
// For modules or multiple emit files the mapRoot will have directory structure like the sources
192-
// 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
193-
sourceMapDir = getDirectoryPath(getSourceFilePathInNewDir(sourceFile.fileName, emitHost as unknown as EmitHost, sourceMapDir));
194-
}
195-
if (getRootLength(sourceMapDir) === 0) {
196-
// The relative paths are relative to the common directory
197-
sourceMapDir = combinePaths(emitHost.getCommonSourceDirectory(), sourceMapDir);
198-
return encodeURI(
199-
getRelativePathToDirectoryOrUrl(
200-
getDirectoryPath(normalizePath(filePath)), // get the relative sourceMapDir path based on jsFilePath
201-
combinePaths(sourceMapDir, sourceMapFile), // this is where user expects to see sourceMap
202-
emitHost.getCurrentDirectory(),
203-
emitHost.getCanonicalFileName,
204-
/*isAbsolutePathAnUrl*/ true,
205-
),
206-
);
207-
}
208-
else {
209-
return encodeURI(combinePaths(sourceMapDir, sourceMapFile));
210-
}
211-
}
212-
return encodeURI(sourceMapFile);
213-
}
214153
}

src/compiler/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9630,6 +9630,16 @@ export interface SourceMapGenerator {
96309630
toString(): string;
96319631
}
96329632

9633+
/** @internal */
9634+
export interface SourceMapOptions {
9635+
sourceMap?: boolean;
9636+
inlineSourceMap?: boolean;
9637+
inlineSources?: boolean;
9638+
sourceRoot?: string;
9639+
mapRoot?: string;
9640+
extendedDiagnostics?: boolean;
9641+
}
9642+
96339643
/** @internal */
96349644
export interface DocumentPositionMapperHost {
96359645
getSourceFileLike(fileName: string): SourceFileLike | undefined;

0 commit comments

Comments
 (0)