Skip to content

Commit 87fc46c

Browse files
committed
Moved responsibility for consuming comment ranges.
1 parent 22f3123 commit 87fc46c

2 files changed

Lines changed: 16 additions & 59 deletions

File tree

src/compiler/comments.ts

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace ts {
3131

3232
// This maps start->end for a comment range. See `hasConsumedCommentRange` and
3333
// `consumeCommentRange` for usage.
34-
let consumedCommentRanges: Map<number>;
34+
let consumedCommentRanges: Map<boolean>;
3535
let leadingCommentRangePositions: Map<boolean>;
3636
let trailingCommentRangePositions: Map<boolean>;
3737

@@ -157,8 +157,8 @@ namespace ts {
157157
leadingCommentRangePositions[pos] = true;
158158
const comments = hasDetachedComments(pos)
159159
? getLeadingCommentsWithoutDetachedComments()
160-
: getLeadingCommentRanges(currentText, pos);
161-
return consumeCommentRanges(comments);
160+
: getLeadingCommentRanges(currentText, pos, consumedCommentRanges);
161+
return comments;
162162
}
163163

164164
function getTrailingCommentsOfPosition(pos: number) {
@@ -167,8 +167,8 @@ namespace ts {
167167
}
168168

169169
trailingCommentRangePositions[pos] = true;
170-
const comments = getTrailingCommentRanges(currentText, pos);
171-
return consumeCommentRanges(comments);
170+
const comments = getTrailingCommentRanges(currentText, pos, consumedCommentRanges);
171+
return comments;
172172
}
173173

174174
function emitLeadingComments(range: TextRange, comments: CommentRange[]): void;
@@ -211,53 +211,6 @@ namespace ts {
211211
range = collapseRangeToEnd(range);
212212
emitLeadingComments(range, getLeadingComments(range));
213213
}
214-
215-
function hasConsumedCommentRange(comment: CommentRange) {
216-
return comment.end === consumedCommentRanges[comment.pos];
217-
}
218-
219-
function consumeCommentRange(comment: CommentRange) {
220-
if (!hasConsumedCommentRange(comment)) {
221-
consumedCommentRanges[comment.pos] = comment.end;
222-
return true;
223-
}
224-
225-
return false;
226-
}
227-
228-
function consumeCommentRanges(comments: CommentRange[]) {
229-
let consumed: CommentRange[];
230-
if (comments) {
231-
let commentsSkipped = 0;
232-
let commentsConsumed = 0;
233-
for (let i = 0; i < comments.length; i++) {
234-
const comment = comments[i];
235-
if (consumeCommentRange(comment)) {
236-
commentsConsumed++;
237-
if (commentsSkipped !== 0) {
238-
if (consumed === undefined) {
239-
consumed = [comment];
240-
}
241-
else {
242-
consumed.push(comment);
243-
}
244-
}
245-
}
246-
else {
247-
commentsSkipped++;
248-
if (commentsConsumed !== 0 && consumed === undefined) {
249-
consumed = comments.slice(0, i);
250-
}
251-
}
252-
}
253-
254-
if (commentsConsumed) {
255-
return consumed || comments;
256-
}
257-
}
258-
259-
return noComments;
260-
}
261214
}
262215

263216
function createCommentWriterWithExtendedDiagnostics(writer: CommentWriter): CommentWriter {
@@ -344,7 +297,7 @@ namespace ts {
344297
function getLeadingCommentsWithoutDetachedComments() {
345298
// get the leading comments from detachedPos
346299
const pos = lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos;
347-
const leadingComments = getLeadingCommentRanges(currentText, pos);
300+
const leadingComments = getLeadingCommentRanges(currentText, pos, consumedCommentRanges);
348301
if (detachedCommentsInfo.length - 1) {
349302
detachedCommentsInfo.pop();
350303
}

src/compiler/scanner.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ namespace ts {
591591
* and the next token are returned.
592592
* If true, comments occurring between the given position and the next line break are returned.
593593
*/
594-
function getCommentRanges(text: string, pos: number, trailing: boolean): CommentRange[] {
594+
function getCommentRanges(text: string, pos: number, trailing: boolean, consumedCommentRanges?: Map<boolean>): CommentRange[] {
595595
let result: CommentRange[];
596596
let collecting = trailing || pos === 0;
597597
while (pos >= 0 && pos < text.length) {
@@ -643,13 +643,17 @@ namespace ts {
643643
}
644644
}
645645

646-
if (collecting) {
646+
if (collecting && (!consumedCommentRanges || !(startPos in consumedCommentRanges))) {
647647
if (!result) {
648648
result = [];
649649
}
650650

651651
result.push({ pos: startPos, end: pos, hasTrailingNewLine, kind });
652+
if (consumedCommentRanges) {
653+
consumedCommentRanges[startPos] = true;
654+
}
652655
}
656+
653657
continue;
654658
}
655659
break;
@@ -669,12 +673,12 @@ namespace ts {
669673
return result;
670674
}
671675

672-
export function getLeadingCommentRanges(text: string, pos: number): CommentRange[] {
673-
return getCommentRanges(text, pos, /*trailing*/ false);
676+
export function getLeadingCommentRanges(text: string, pos: number, consumedCommentRanges?: Map<boolean>): CommentRange[] {
677+
return getCommentRanges(text, pos, /*trailing*/ false, consumedCommentRanges);
674678
}
675679

676-
export function getTrailingCommentRanges(text: string, pos: number): CommentRange[] {
677-
return getCommentRanges(text, pos, /*trailing*/ true);
680+
export function getTrailingCommentRanges(text: string, pos: number, consumedCommentRanges?: Map<boolean>): CommentRange[] {
681+
return getCommentRanges(text, pos, /*trailing*/ true, consumedCommentRanges);
678682
}
679683

680684
/** Optionally, get the shebang */

0 commit comments

Comments
 (0)