Skip to content

Commit 66783a3

Browse files
committed
Factor out repeated code and call one from another,
Also make the functions use the new type CoreEmitHost to take into account of methods that are not available from DTE mode. Signed-off-by: Hana Joo <[email protected]>
1 parent ad7d11e commit 66783a3

File tree

4 files changed

+83
-138
lines changed

4 files changed

+83
-138
lines changed

src/compiler/emitter.ts

Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
ConstructSignatureDeclaration,
5858
contains,
5959
ContinueStatement,
60+
CoreEmitHost,
6061
createBinaryExpressionTrampoline,
6162
createDiagnosticCollection,
6263
createGetCanonicalFileName,
@@ -402,6 +403,7 @@ import {
402403
SourceFilePrologueInfo,
403404
SourceMapEmitResult,
404405
SourceMapGenerator,
406+
SourceMapOptions,
405407
SourceMapSource,
406408
SpreadAssignment,
407409
SpreadElement,
@@ -721,6 +723,62 @@ export function getOutputFileNames(commandLine: ParsedCommandLine, inputFileName
721723
return getOutputs();
722724
}
723725

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

10061064
const sourceMappingURL = getSourceMappingURL(
1065+
host,
10071066
mapOptions,
10081067
sourceMapGenerator,
10091068
jsFilePath,
@@ -1039,15 +1098,6 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
10391098
writer.clear();
10401099
}
10411100

1042-
interface SourceMapOptions {
1043-
sourceMap?: boolean;
1044-
inlineSourceMap?: boolean;
1045-
inlineSources?: boolean;
1046-
sourceRoot?: string;
1047-
mapRoot?: string;
1048-
extendedDiagnostics?: boolean;
1049-
}
1050-
10511101
function shouldEmitSourceMaps(mapOptions: SourceMapOptions, sourceFileOrBundle: SourceFile | Bundle) {
10521102
return (mapOptions.sourceMap || mapOptions.inlineSourceMap)
10531103
&& (sourceFileOrBundle.kind !== SyntaxKind.SourceFile || !fileExtensionIs(sourceFileOrBundle.fileName, Extension.Json));
@@ -1059,60 +1109,6 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
10591109
const sourceRoot = normalizeSlashes(mapOptions.sourceRoot || "");
10601110
return sourceRoot ? ensureTrailingDirectorySeparator(sourceRoot) : sourceRoot;
10611111
}
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-
}
11161112
}
11171113

11181114
/** @internal */

src/compiler/transformers/declarations/transpileDeclaration.ts

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
import {
2-
base64encode,
3-
combinePaths,
42
CompilerOptions,
53
createEmitDeclarationResolver,
64
createGetCanonicalFileName,
75
createPrinter,
86
createSourceMapGenerator,
97
createTextWriter,
10-
Debug,
118
Diagnostic,
12-
EmitHost,
139
ensureTrailingDirectorySeparator,
1410
getAreDeclarationMapsEnabled,
1511
getBaseFileName,
1612
getDeclarationEmitOutputFilePathWorker,
17-
getDirectoryPath,
1813
getNewLineCharacter,
19-
getRelativePathToDirectoryOrUrl,
20-
getRootLength,
21-
getSourceFilePathInNewDir,
22-
normalizePath,
14+
getSourceMapDirectory,
15+
getSourceMappingURL,
2316
normalizeSlashes,
2417
nullTransformationContext,
2518
PrinterOptions,
2619
SourceFile,
27-
SourceMapGenerator,
28-
sys,
2920
transformDeclarations,
3021
TranspileDeclarationsOptions,
3122
TranspileDeclarationsOutput,
@@ -98,25 +89,6 @@ export function transpileDeclaration(sourceFile: SourceFile, transpileOptions: T
9889
diagnostics,
9990
};
10091

101-
// logic replicated from emitter.ts
102-
function getSourceMapDirectory(mapOptions: CompilerOptions, filePath: string, sourceFile: SourceFile | undefined) {
103-
if (mapOptions.sourceRoot) return emitHost.getCommonSourceDirectory();
104-
if (mapOptions.mapRoot) {
105-
let sourceMapDir = normalizeSlashes(mapOptions.mapRoot);
106-
if (sourceFile) {
107-
// For modules or multiple emit files the mapRoot will have directory structure like the sources
108-
// 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
109-
sourceMapDir = getDirectoryPath(getSourceFilePathInNewDir(sourceFile.fileName, emitHost as unknown as EmitHost, sourceMapDir));
110-
}
111-
if (getRootLength(sourceMapDir) === 0) {
112-
// The relative paths are relative to the common directory
113-
sourceMapDir = combinePaths(emitHost.getCommonSourceDirectory(), sourceMapDir);
114-
}
115-
return sourceMapDir;
116-
}
117-
return getDirectoryPath(normalizePath(filePath));
118-
}
119-
12092
// logic replicated from emitter.ts
12193
function getSourceMapGenerator(declarationFilePath: string, declarationMapPath: string) {
12294
if (!getAreDeclarationMapsEnabled(compilerOptions)) return;
@@ -133,11 +105,12 @@ export function transpileDeclaration(sourceFile: SourceFile, transpileOptions: T
133105
emitHost,
134106
getBaseFileName(normalizeSlashes(declarationFilePath)),
135107
sourceRoot ? ensureTrailingDirectorySeparator(sourceRoot) : sourceRoot,
136-
getSourceMapDirectory(compilerOptions, declarationFilePath, sourceFile),
108+
getSourceMapDirectory(emitHost, compilerOptions, declarationFilePath, sourceFile),
137109
mapOptions,
138110
);
139111

140112
const sourceMappingURL = getSourceMappingURL(
113+
emitHost,
141114
mapOptions,
142115
sourceMapGenerator,
143116
declarationFilePath,
@@ -146,41 +119,4 @@ export function transpileDeclaration(sourceFile: SourceFile, transpileOptions: T
146119
);
147120
return { sourceMapGenerator, sourceMappingURL: `//# ${"sourceMappingURL"}=${sourceMappingURL}` };
148121
}
149-
150-
// logic replicated from emitter.ts
151-
function getSourceMappingURL(mapOptions: CompilerOptions, sourceMapGenerator: SourceMapGenerator, filePath: string, sourceMapFilePath: string | undefined, sourceFile: SourceFile | undefined) {
152-
if (mapOptions.inlineSourceMap) {
153-
// Encode the sourceMap into the sourceMap url
154-
const sourceMapText = sourceMapGenerator.toString();
155-
const base64SourceMapText = base64encode(sys, sourceMapText);
156-
return `data:application/json;base64,${base64SourceMapText}`;
157-
}
158-
159-
const sourceMapFile = getBaseFileName(normalizeSlashes(Debug.checkDefined(sourceMapFilePath)));
160-
if (mapOptions.mapRoot) {
161-
let sourceMapDir = normalizeSlashes(mapOptions.mapRoot);
162-
if (sourceFile) {
163-
// For modules or multiple emit files the mapRoot will have directory structure like the sources
164-
// 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
165-
sourceMapDir = getDirectoryPath(getSourceFilePathInNewDir(sourceFile.fileName, emitHost as unknown as EmitHost, sourceMapDir));
166-
}
167-
if (getRootLength(sourceMapDir) === 0) {
168-
// The relative paths are relative to the common directory
169-
sourceMapDir = combinePaths(emitHost.getCommonSourceDirectory(), sourceMapDir);
170-
return encodeURI(
171-
getRelativePathToDirectoryOrUrl(
172-
getDirectoryPath(normalizePath(filePath)), // get the relative sourceMapDir path based on jsFilePath
173-
combinePaths(sourceMapDir, sourceMapFile), // this is where user expects to see sourceMap
174-
emitHost.getCurrentDirectory(),
175-
emitHost.getCanonicalFileName,
176-
/*isAbsolutePathAnUrl*/ true,
177-
),
178-
);
179-
}
180-
else {
181-
return encodeURI(combinePaths(sourceMapDir, sourceMapFile));
182-
}
183-
}
184-
return encodeURI(sourceMapFile);
185-
}
186122
}

src/compiler/types.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8179,18 +8179,20 @@ export interface SourceFileMayBeEmittedHost {
81798179
getCanonicalFileName: GetCanonicalFileName;
81808180
useCaseSensitiveFileNames(): boolean;
81818181
}
8182+
/** @internal */
8183+
export interface CoreEmitHost {
8184+
getCurrentDirectory(): string;
8185+
getCommonSourceDirectory(): string;
8186+
getCanonicalFileName(fileName: string): string;
8187+
}
81828188

81838189
/** @internal */
8184-
export interface EmitHost extends ScriptReferenceHost, ModuleSpecifierResolutionHost, SourceFileMayBeEmittedHost {
8190+
export interface EmitHost extends ScriptReferenceHost, ModuleSpecifierResolutionHost, SourceFileMayBeEmittedHost, CoreEmitHost {
81858191
getSourceFiles(): readonly SourceFile[];
81868192
useCaseSensitiveFileNames(): boolean;
8187-
getCurrentDirectory(): string;
81888193

81898194
getLibFileFromReference(ref: FileReference): SourceFile | undefined;
81908195

8191-
getCommonSourceDirectory(): string;
8192-
getCanonicalFileName(fileName: string): string;
8193-
81948196
isEmitBlocked(emitFileName: string): boolean;
81958197

81968198
/** @deprecated */ getPrependNodes(): readonly (InputFiles | UnparsedSource)[];
@@ -9635,6 +9637,16 @@ export interface SourceMapGenerator {
96359637
toString(): string;
96369638
}
96379639

9640+
/** @internal */
9641+
export interface SourceMapOptions {
9642+
sourceMap?: boolean;
9643+
inlineSourceMap?: boolean;
9644+
inlineSources?: boolean;
9645+
sourceRoot?: string;
9646+
mapRoot?: string;
9647+
extendedDiagnostics?: boolean;
9648+
}
9649+
96389650
/** @internal */
96399651
export interface DocumentPositionMapperHost {
96409652
getSourceFileLike(fileName: string): SourceFileLike | undefined;

src/compiler/utilities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import {
7979
ContainerFlags,
8080
contains,
8181
containsPath,
82+
CoreEmitHost,
8283
createGetCanonicalFileName,
8384
createMultiMap,
8485
createScanner,
@@ -6391,7 +6392,7 @@ export function sourceFileMayBeEmitted(sourceFile: SourceFile, host: SourceFileM
63916392
}
63926393

63936394
/** @internal */
6394-
export function getSourceFilePathInNewDir(fileName: string, host: EmitHost, newDirPath: string): string {
6395+
export function getSourceFilePathInNewDir(fileName: string, host: CoreEmitHost, newDirPath: string): string {
63956396
return getSourceFilePathInNewDirWorker(fileName, newDirPath, host.getCurrentDirectory(), host.getCommonSourceDirectory(), f => host.getCanonicalFileName(f));
63966397
}
63976398

0 commit comments

Comments
 (0)