Skip to content

Commit 0165b71

Browse files
committed
Embed comment emit inside of printer
1 parent 028e4e2 commit 0165b71

File tree

10 files changed

+2791
-2728
lines changed

10 files changed

+2791
-2728
lines changed

Jakefile.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ var compilerSources = [
8484
"sourcemap.ts",
8585
"comments.ts",
8686
"declarationEmitter.ts",
87-
"printer.ts",
8887
"emitter.ts",
8988
"program.ts",
9089
"commandLineParser.ts",
@@ -122,7 +121,6 @@ var servicesSources = [
122121
"sourcemap.ts",
123122
"comments.ts",
124123
"declarationEmitter.ts",
125-
"printer.ts",
126124
"emitter.ts",
127125
"program.ts",
128126
"commandLineParser.ts",

src/compiler/comments.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ namespace ts {
55
export interface CommentWriter {
66
reset(): void;
77
setSourceFile(sourceFile: SourceFile): void;
8+
setWriter(writer: EmitTextWriter): void;
89
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(writer: EmitTextWriter, compilerOptions: CompilerOptions, newLine: string, emitPos: (pos: number) => void): CommentWriter {
14-
const extendedDiagnostics = compilerOptions.extendedDiagnostics;
14+
export function createCommentWriter(printerOptions: PrinterOptions, emitPos: (pos: number) => void): CommentWriter {
15+
const extendedDiagnostics = printerOptions.extendedDiagnostics;
16+
const newLine = getNewLineCharacter(printerOptions);
17+
let writer: EmitTextWriter;
1518
let containerPos = -1;
1619
let containerEnd = -1;
1720
let declarationListContainerEnd = -1;
@@ -20,10 +23,11 @@ namespace ts {
2023
let currentLineMap: number[];
2124
let detachedCommentsInfo: { nodePos: number, detachedCommentEndPos: number}[];
2225
let hasWrittenComment = false;
23-
let disabled: boolean = compilerOptions.removeComments;
26+
let disabled: boolean = printerOptions.removeComments;
2427

2528
return {
2629
reset,
30+
setWriter,
2731
setSourceFile,
2832
emitNodeWithComments,
2933
emitBodyWithDetachedComments,
@@ -194,9 +198,9 @@ namespace ts {
194198
}
195199

196200
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
197-
emitPos(commentPos);
201+
if (emitPos) emitPos(commentPos);
198202
writeCommentRange(currentText, currentLineMap, writer, commentPos, commentEnd, newLine);
199-
emitPos(commentEnd);
203+
if (emitPos) emitPos(commentEnd);
200204

201205
if (hasTrailingNewLine) {
202206
writer.writeLine();
@@ -216,9 +220,9 @@ namespace ts {
216220
writer.write(" ");
217221
}
218222

219-
emitPos(commentPos);
223+
if (emitPos) emitPos(commentPos);
220224
writeCommentRange(currentText, currentLineMap, writer, commentPos, commentEnd, newLine);
221-
emitPos(commentEnd);
225+
if (emitPos) emitPos(commentEnd);
222226

223227
if (hasTrailingNewLine) {
224228
writer.writeLine();
@@ -244,9 +248,9 @@ namespace ts {
244248
function emitTrailingCommentOfPosition(commentPos: number, commentEnd: number, _kind: SyntaxKind, hasTrailingNewLine: boolean) {
245249
// trailing comments of a position are emitted at /*trailing comment1 */space/*trailing comment*/space
246250

247-
emitPos(commentPos);
251+
if (emitPos) emitPos(commentPos);
248252
writeCommentRange(currentText, currentLineMap, writer, commentPos, commentEnd, newLine);
249-
emitPos(commentEnd);
253+
if (emitPos) emitPos(commentEnd);
250254

251255
if (hasTrailingNewLine) {
252256
writer.writeLine();
@@ -282,6 +286,10 @@ namespace ts {
282286
detachedCommentsInfo = undefined;
283287
}
284288

289+
function setWriter(output: EmitTextWriter): void {
290+
writer = output;
291+
}
292+
285293
function setSourceFile(sourceFile: SourceFile) {
286294
currentSourceFile = sourceFile;
287295
currentText = currentSourceFile.text;
@@ -319,9 +327,9 @@ namespace ts {
319327
}
320328

321329
function writeComment(text: string, lineMap: number[], writer: EmitTextWriter, commentPos: number, commentEnd: number, newLine: string) {
322-
emitPos(commentPos);
330+
if (emitPos) emitPos(commentPos);
323331
writeCommentRange(text, lineMap, writer, commentPos, commentEnd, newLine);
324-
emitPos(commentEnd);
332+
if (emitPos) emitPos(commentEnd);
325333
}
326334

327335
/**

src/compiler/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,11 +1447,11 @@ 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

1454-
export function getEmitModuleKind(compilerOptions: CompilerOptions) {
1454+
export function getEmitModuleKind(compilerOptions: CompilerOptions | PrinterOptions) {
14551455
return typeof compilerOptions.module === "number" ?
14561456
compilerOptions.module :
14571457
getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2015 ? ModuleKind.ES2015 : ModuleKind.CommonJS;

0 commit comments

Comments
 (0)