Skip to content

Commit bc611d9

Browse files
authored
Merge pull request microsoft#13761 from Microsoft/extractPrinter
Expose printing functionality of emitter as a public API
2 parents 1530a60 + 9845413 commit bc611d9

25 files changed

+942
-597
lines changed

Jakefile.js

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ var harnessSources = harnessCoreSources.concat([
269269
"projectErrors.ts",
270270
"matchFiles.ts",
271271
"initializeTSConfig.ts",
272+
"printer.ts",
272273
].map(function (f) {
273274
return path.join(unittestsDirectory, f);
274275
})).concat([

src/compiler/comments.ts

+26-22
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ namespace ts {
55
export interface CommentWriter {
66
reset(): void;
77
setSourceFile(sourceFile: SourceFile): void;
8-
emitNodeWithComments(emitContext: EmitContext, node: Node, emitCallback: (emitContext: EmitContext, node: Node) => void): void;
8+
setWriter(writer: EmitTextWriter): void;
9+
emitNodeWithComments(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void): void;
910
emitBodyWithDetachedComments(node: Node, detachedRange: TextRange, emitCallback: (node: Node) => void): void;
1011
emitTrailingCommentsOfPosition(pos: number): void;
1112
}
1213

13-
export function createCommentWriter(host: EmitHost, writer: EmitTextWriter, sourceMap: SourceMapWriter): CommentWriter {
14-
const compilerOptions = host.getCompilerOptions();
15-
const extendedDiagnostics = compilerOptions.extendedDiagnostics;
16-
const newLine = host.getNewLine();
17-
const { emitPos } = sourceMap;
18-
14+
export function createCommentWriter(printerOptions: PrinterOptions, emitPos: ((pos: number) => void) | undefined): CommentWriter {
15+
const extendedDiagnostics = printerOptions.extendedDiagnostics;
16+
const newLine = getNewLineCharacter(printerOptions);
17+
let writer: EmitTextWriter;
1918
let containerPos = -1;
2019
let containerEnd = -1;
2120
let declarationListContainerEnd = -1;
@@ -24,19 +23,20 @@ namespace ts {
2423
let currentLineMap: number[];
2524
let detachedCommentsInfo: { nodePos: number, detachedCommentEndPos: number}[];
2625
let hasWrittenComment = false;
27-
let disabled: boolean = compilerOptions.removeComments;
26+
let disabled: boolean = printerOptions.removeComments;
2827

2928
return {
3029
reset,
30+
setWriter,
3131
setSourceFile,
3232
emitNodeWithComments,
3333
emitBodyWithDetachedComments,
3434
emitTrailingCommentsOfPosition,
3535
};
3636

37-
function emitNodeWithComments(emitContext: EmitContext, node: Node, emitCallback: (emitContext: EmitContext, node: Node) => void) {
37+
function emitNodeWithComments(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) {
3838
if (disabled) {
39-
emitCallback(emitContext, node);
39+
emitCallback(hint, node);
4040
return;
4141
}
4242

@@ -47,11 +47,11 @@ namespace ts {
4747
// Both pos and end are synthesized, so just emit the node without comments.
4848
if (emitFlags & EmitFlags.NoNestedComments) {
4949
disabled = true;
50-
emitCallback(emitContext, node);
50+
emitCallback(hint, node);
5151
disabled = false;
5252
}
5353
else {
54-
emitCallback(emitContext, node);
54+
emitCallback(hint, node);
5555
}
5656
}
5757
else {
@@ -94,11 +94,11 @@ namespace ts {
9494

9595
if (emitFlags & EmitFlags.NoNestedComments) {
9696
disabled = true;
97-
emitCallback(emitContext, node);
97+
emitCallback(hint, node);
9898
disabled = false;
9999
}
100100
else {
101-
emitCallback(emitContext, node);
101+
emitCallback(hint, node);
102102
}
103103

104104
if (extendedDiagnostics) {
@@ -198,9 +198,9 @@ namespace ts {
198198
}
199199

200200
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
201-
emitPos(commentPos);
201+
if (emitPos) emitPos(commentPos);
202202
writeCommentRange(currentText, currentLineMap, writer, commentPos, commentEnd, newLine);
203-
emitPos(commentEnd);
203+
if (emitPos) emitPos(commentEnd);
204204

205205
if (hasTrailingNewLine) {
206206
writer.writeLine();
@@ -220,9 +220,9 @@ namespace ts {
220220
writer.write(" ");
221221
}
222222

223-
emitPos(commentPos);
223+
if (emitPos) emitPos(commentPos);
224224
writeCommentRange(currentText, currentLineMap, writer, commentPos, commentEnd, newLine);
225-
emitPos(commentEnd);
225+
if (emitPos) emitPos(commentEnd);
226226

227227
if (hasTrailingNewLine) {
228228
writer.writeLine();
@@ -248,9 +248,9 @@ namespace ts {
248248
function emitTrailingCommentOfPosition(commentPos: number, commentEnd: number, _kind: SyntaxKind, hasTrailingNewLine: boolean) {
249249
// trailing comments of a position are emitted at /*trailing comment1 */space/*trailing comment*/space
250250

251-
emitPos(commentPos);
251+
if (emitPos) emitPos(commentPos);
252252
writeCommentRange(currentText, currentLineMap, writer, commentPos, commentEnd, newLine);
253-
emitPos(commentEnd);
253+
if (emitPos) emitPos(commentEnd);
254254

255255
if (hasTrailingNewLine) {
256256
writer.writeLine();
@@ -286,6 +286,10 @@ namespace ts {
286286
detachedCommentsInfo = undefined;
287287
}
288288

289+
function setWriter(output: EmitTextWriter): void {
290+
writer = output;
291+
}
292+
289293
function setSourceFile(sourceFile: SourceFile) {
290294
currentSourceFile = sourceFile;
291295
currentText = currentSourceFile.text;
@@ -323,9 +327,9 @@ namespace ts {
323327
}
324328

325329
function writeComment(text: string, lineMap: number[], writer: EmitTextWriter, commentPos: number, commentEnd: number, newLine: string) {
326-
emitPos(commentPos);
330+
if (emitPos) emitPos(commentPos);
327331
writeCommentRange(text, lineMap, writer, commentPos, commentEnd, newLine);
328-
emitPos(commentEnd);
332+
if (emitPos) emitPos(commentEnd);
329333
}
330334

331335
/**

src/compiler/core.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ namespace ts {
14471447
return /^\.\.?($|[\\/])/.test(moduleName);
14481448
}
14491449

1450-
export function getEmitScriptTarget(compilerOptions: CompilerOptions) {
1450+
export function getEmitScriptTarget(compilerOptions: CompilerOptions | PrinterOptions) {
14511451
return compilerOptions.target || ScriptTarget.ES3;
14521452
}
14531453

src/compiler/declarationEmitter.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ namespace ts {
3535
forEachEmittedFile(host, getDeclarationDiagnosticsFromFile, targetSourceFile);
3636
return declarationDiagnostics.getDiagnostics(targetSourceFile ? targetSourceFile.fileName : undefined);
3737

38-
function getDeclarationDiagnosticsFromFile({ declarationFilePath }: EmitFileNames, sources: SourceFile[], isBundledEmit: boolean) {
39-
emitDeclarations(host, resolver, declarationDiagnostics, declarationFilePath, sources, isBundledEmit, /*emitOnlyDtsFiles*/ false);
38+
function getDeclarationDiagnosticsFromFile({ declarationFilePath }: EmitFileNames, sourceFileOrBundle: SourceFile | Bundle) {
39+
emitDeclarations(host, resolver, declarationDiagnostics, declarationFilePath, sourceFileOrBundle, /*emitOnlyDtsFiles*/ false);
4040
}
4141
}
4242

4343
function emitDeclarations(host: EmitHost, resolver: EmitResolver, emitterDiagnostics: DiagnosticCollection, declarationFilePath: string,
44-
sourceFiles: SourceFile[], isBundledEmit: boolean, emitOnlyDtsFiles: boolean): DeclarationEmit {
44+
sourceFileOrBundle: SourceFile | Bundle, emitOnlyDtsFiles: boolean): DeclarationEmit {
45+
const sourceFiles = sourceFileOrBundle.kind === SyntaxKind.Bundle ? sourceFileOrBundle.sourceFiles : [sourceFileOrBundle];
46+
const isBundledEmit = sourceFileOrBundle.kind === SyntaxKind.Bundle;
4547
const newLine = host.getNewLine();
4648
const compilerOptions = host.getCompilerOptions();
4749

@@ -1803,8 +1805,9 @@ namespace ts {
18031805
}
18041806
return addedBundledEmitReference;
18051807

1806-
function getDeclFileName(emitFileNames: EmitFileNames, _sourceFiles: SourceFile[], isBundledEmit: boolean) {
1808+
function getDeclFileName(emitFileNames: EmitFileNames, sourceFileOrBundle: SourceFile | Bundle) {
18071809
// Dont add reference path to this file if it is a bundled emit and caller asked not emit bundled file path
1810+
const isBundledEmit = sourceFileOrBundle.kind === SyntaxKind.Bundle;
18081811
if (isBundledEmit && !addBundledFileReference) {
18091812
return;
18101813
}
@@ -1817,10 +1820,11 @@ namespace ts {
18171820
}
18181821

18191822
/* @internal */
1820-
export function writeDeclarationFile(declarationFilePath: string, sourceFiles: SourceFile[], isBundledEmit: boolean, host: EmitHost, resolver: EmitResolver, emitterDiagnostics: DiagnosticCollection, emitOnlyDtsFiles: boolean) {
1821-
const emitDeclarationResult = emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit, emitOnlyDtsFiles);
1823+
export function writeDeclarationFile(declarationFilePath: string, sourceFileOrBundle: SourceFile | Bundle, host: EmitHost, resolver: EmitResolver, emitterDiagnostics: DiagnosticCollection, emitOnlyDtsFiles: boolean) {
1824+
const emitDeclarationResult = emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFileOrBundle, emitOnlyDtsFiles);
18221825
const emitSkipped = emitDeclarationResult.reportedDeclarationError || host.isEmitBlocked(declarationFilePath) || host.getCompilerOptions().noEmit;
18231826
if (!emitSkipped) {
1827+
const sourceFiles = sourceFileOrBundle.kind === SyntaxKind.Bundle ? sourceFileOrBundle.sourceFiles : [sourceFileOrBundle];
18241828
const declarationOutput = emitDeclarationResult.referencesOutput
18251829
+ getDeclarationOutput(emitDeclarationResult.synchronousDeclarationOutput, emitDeclarationResult.moduleElementDeclarationEmitInfo);
18261830
writeFile(host, emitterDiagnostics, declarationFilePath, declarationOutput, host.getCompilerOptions().emitBOM, sourceFiles);

0 commit comments

Comments
 (0)