Skip to content

Commit 028e4e2

Browse files
committed
Extract printer from emitter for reusability.
1 parent 64dd806 commit 028e4e2

21 files changed

+2944
-2855
lines changed

Jakefile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ var compilerSources = [
8484
"sourcemap.ts",
8585
"comments.ts",
8686
"declarationEmitter.ts",
87+
"printer.ts",
8788
"emitter.ts",
8889
"program.ts",
8990
"commandLineParser.ts",
@@ -121,6 +122,7 @@ var servicesSources = [
121122
"sourcemap.ts",
122123
"comments.ts",
123124
"declarationEmitter.ts",
125+
"printer.ts",
124126
"emitter.ts",
125127
"program.ts",
126128
"commandLineParser.ts",

src/compiler/comments.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@ 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+
emitNodeWithComments(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void): void;
99
emitBodyWithDetachedComments(node: Node, detachedRange: TextRange, emitCallback: (node: Node) => void): void;
1010
emitTrailingCommentsOfPosition(pos: number): void;
1111
}
1212

13-
export function createCommentWriter(host: EmitHost, writer: EmitTextWriter, sourceMap: SourceMapWriter): CommentWriter {
14-
const compilerOptions = host.getCompilerOptions();
13+
export function createCommentWriter(writer: EmitTextWriter, compilerOptions: CompilerOptions, newLine: string, emitPos: (pos: number) => void): CommentWriter {
1514
const extendedDiagnostics = compilerOptions.extendedDiagnostics;
16-
const newLine = host.getNewLine();
17-
const { emitPos } = sourceMap;
18-
1915
let containerPos = -1;
2016
let containerEnd = -1;
2117
let declarationListContainerEnd = -1;
@@ -34,9 +30,9 @@ namespace ts {
3430
emitTrailingCommentsOfPosition,
3531
};
3632

37-
function emitNodeWithComments(emitContext: EmitContext, node: Node, emitCallback: (emitContext: EmitContext, node: Node) => void) {
33+
function emitNodeWithComments(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) {
3834
if (disabled) {
39-
emitCallback(emitContext, node);
35+
emitCallback(hint, node);
4036
return;
4137
}
4238

@@ -47,11 +43,11 @@ namespace ts {
4743
// Both pos and end are synthesized, so just emit the node without comments.
4844
if (emitFlags & EmitFlags.NoNestedComments) {
4945
disabled = true;
50-
emitCallback(emitContext, node);
46+
emitCallback(hint, node);
5147
disabled = false;
5248
}
5349
else {
54-
emitCallback(emitContext, node);
50+
emitCallback(hint, node);
5551
}
5652
}
5753
else {
@@ -94,11 +90,11 @@ namespace ts {
9490

9591
if (emitFlags & EmitFlags.NoNestedComments) {
9692
disabled = true;
97-
emitCallback(emitContext, node);
93+
emitCallback(hint, node);
9894
disabled = false;
9995
}
10096
else {
101-
emitCallback(emitContext, node);
97+
emitCallback(hint, node);
10298
}
10399

104100
if (extendedDiagnostics) {

src/compiler/declarationEmitter.ts

Lines changed: 10 additions & 6 deletions
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)