-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathcomments.ts
More file actions
214 lines (182 loc) · 8.87 KB
/
comments.ts
File metadata and controls
214 lines (182 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/// <reference path="sourcemap.ts" />
/* @internal */
namespace ts {
export interface CommentWriter {
reset(): void;
setSourceFile(sourceFile: SourceFile): void;
getLeadingCommentsToEmit(node: TextRange): CommentRange[];
getTrailingCommentsToEmit(node: TextRange): CommentRange[];
emitDetachedComments(node: TextRange): void;
emitLeadingComments(node: TextRange, comments?: CommentRange[]): void;
emitTrailingComments(node: TextRange, comments?: CommentRange[]): void;
}
export function createCommentWriter(host: EmitHost, writer: EmitTextWriter, sourceMap: SourceMapWriter): CommentWriter {
const compilerOptions = host.getCompilerOptions();
const newLine = host.getNewLine();
const { emitPos } = sourceMap;
let currentSourceFile: SourceFile;
let currentText: string;
let currentLineMap: number[];
let detachedCommentsInfo: { nodePos: number, detachedCommentEndPos: number}[];
// This maps start->end for a comment range. See `hasConsumedCommentRange` and
// `consumeCommentRange` for usage.
let consumedCommentRanges: number[];
let leadingCommentRangeNodeStarts: boolean[];
let trailingCommentRangeNodeEnds: boolean[];
return compilerOptions.removeComments
? createCommentRemovingWriter()
: createCommentPreservingWriter();
function createCommentRemovingWriter(): CommentWriter {
return {
reset,
setSourceFile,
getLeadingCommentsToEmit(node: TextRange): CommentRange[] { return undefined; },
getTrailingCommentsToEmit(node: TextRange): CommentRange[] { return undefined; },
emitDetachedComments,
emitLeadingComments(node: TextRange, comments?: CommentRange[]): void { },
emitTrailingComments(node: TextRange, comments?: CommentRange[]): void { },
};
function emitDetachedComments(node: TextRange): void {
emitDetachedCommentsAndUpdateCommentsInfo(node, /*removeComments*/ true);
}
}
function createCommentPreservingWriter(): CommentWriter {
const noComments: CommentRange[] = [];
return {
reset,
setSourceFile,
getLeadingCommentsToEmit,
getTrailingCommentsToEmit,
emitDetachedComments,
emitLeadingComments,
emitTrailingComments,
};
function getLeadingCommentsToEmit(node: TextRange) {
if (nodeIsSynthesized(node)) {
return;
}
if (!leadingCommentRangeNodeStarts[node.pos]) {
leadingCommentRangeNodeStarts[node.pos] = true;
const comments = hasDetachedComments(node.pos)
? getLeadingCommentsWithoutDetachedComments()
: getLeadingCommentRanges(currentText, node.pos);
return consumeCommentRanges(comments);
}
return noComments;
}
function getTrailingCommentsToEmit(node: TextRange) {
if (nodeIsSynthesized(node)) {
return;
}
if (!trailingCommentRangeNodeEnds[node.end]) {
trailingCommentRangeNodeEnds[node.end] = true;
const comments = getTrailingCommentRanges(currentText, node.end);
return consumeCommentRanges(comments);
}
return noComments;
}
function hasConsumedCommentRange(comment: CommentRange) {
return comment.end === consumedCommentRanges[comment.pos];
}
function consumeCommentRange(comment: CommentRange) {
if (!hasConsumedCommentRange(comment)) {
consumedCommentRanges[comment.pos] = comment.end;
return true;
}
return false;
}
function consumeCommentRanges(comments: CommentRange[]) {
let consumed: CommentRange[];
if (comments) {
let commentsSkipped = 0;
let commentsConsumed = 0;
for (let i = 0; i < comments.length; i++) {
const comment = comments[i];
if (consumeCommentRange(comment)) {
commentsConsumed++;
if (commentsSkipped !== 0) {
if (consumed === undefined) {
consumed = [comment];
}
else {
consumed.push(comment);
}
}
}
else {
commentsSkipped++;
if (commentsConsumed !== 0 && consumed === undefined) {
consumed = comments.slice(0, i);
}
}
}
if (commentsConsumed) {
return consumed || comments;
}
}
return noComments;
}
function emitLeadingComments(range: TextRange, leadingComments: CommentRange[] = getLeadingCommentsToEmit(range)) {
emitNewLineBeforeLeadingComments(currentLineMap, writer, range, leadingComments);
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
emitComments(currentText, currentLineMap, writer, leadingComments, /*trailingSeparator*/ true, newLine, writeComment);
}
function emitTrailingComments(range: TextRange, trailingComments = getTrailingCommentsToEmit(range)) {
// trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/
emitComments(currentText, currentLineMap, writer, trailingComments, /*trailingSeparator*/ false, newLine, writeComment);
}
function emitDetachedComments(range: TextRange) {
emitDetachedCommentsAndUpdateCommentsInfo(range, /*removeComments*/ false);
}
}
function reset() {
currentSourceFile = undefined;
currentText = undefined;
currentLineMap = undefined;
detachedCommentsInfo = undefined;
consumedCommentRanges = undefined;
trailingCommentRangeNodeEnds = undefined;
leadingCommentRangeNodeStarts = undefined;
}
function setSourceFile(sourceFile: SourceFile) {
currentSourceFile = sourceFile;
currentText = sourceFile.text;
currentLineMap = getLineStarts(sourceFile);
detachedCommentsInfo = undefined;
consumedCommentRanges = [];
leadingCommentRangeNodeStarts = [];
trailingCommentRangeNodeEnds = [];
}
function hasDetachedComments(pos: number) {
return detachedCommentsInfo !== undefined && lastOrUndefined(detachedCommentsInfo).nodePos === pos;
}
function getLeadingCommentsWithoutDetachedComments() {
// get the leading comments from detachedPos
const pos = lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos;
const leadingComments = getLeadingCommentRanges(currentText, pos);
if (detachedCommentsInfo.length - 1) {
detachedCommentsInfo.pop();
}
else {
detachedCommentsInfo = undefined;
}
return leadingComments;
}
function emitDetachedCommentsAndUpdateCommentsInfo(node: TextRange, removeComments: boolean) {
const currentDetachedCommentInfo = emitDetachedComments(currentText, currentLineMap, writer, writeComment, node, newLine, removeComments);
if (currentDetachedCommentInfo) {
if (detachedCommentsInfo) {
detachedCommentsInfo.push(currentDetachedCommentInfo);
}
else {
detachedCommentsInfo = [currentDetachedCommentInfo];
}
}
}
function writeComment(text: string, lineMap: number[], writer: EmitTextWriter, comment: CommentRange, newLine: string) {
emitPos(comment.pos);
writeCommentRange(text, lineMap, writer, comment, newLine);
emitPos(comment.end);
}
}
}