From 87fc46c013691b97e846ea84c140ab19602b8746 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Thu, 26 May 2016 10:34:34 -0700 Subject: [PATCH] Moved responsibility for consuming comment ranges. --- src/compiler/comments.ts | 59 ++++------------------------------------ src/compiler/scanner.ts | 16 +++++++---- 2 files changed, 16 insertions(+), 59 deletions(-) diff --git a/src/compiler/comments.ts b/src/compiler/comments.ts index dbe2299ca7a..9ed15335541 100644 --- a/src/compiler/comments.ts +++ b/src/compiler/comments.ts @@ -31,7 +31,7 @@ namespace ts { // This maps start->end for a comment range. See `hasConsumedCommentRange` and // `consumeCommentRange` for usage. - let consumedCommentRanges: Map; + let consumedCommentRanges: Map; let leadingCommentRangePositions: Map; let trailingCommentRangePositions: Map; @@ -157,8 +157,8 @@ namespace ts { leadingCommentRangePositions[pos] = true; const comments = hasDetachedComments(pos) ? getLeadingCommentsWithoutDetachedComments() - : getLeadingCommentRanges(currentText, pos); - return consumeCommentRanges(comments); + : getLeadingCommentRanges(currentText, pos, consumedCommentRanges); + return comments; } function getTrailingCommentsOfPosition(pos: number) { @@ -167,8 +167,8 @@ namespace ts { } trailingCommentRangePositions[pos] = true; - const comments = getTrailingCommentRanges(currentText, pos); - return consumeCommentRanges(comments); + const comments = getTrailingCommentRanges(currentText, pos, consumedCommentRanges); + return comments; } function emitLeadingComments(range: TextRange, comments: CommentRange[]): void; @@ -211,53 +211,6 @@ namespace ts { range = collapseRangeToEnd(range); emitLeadingComments(range, getLeadingComments(range)); } - - 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 createCommentWriterWithExtendedDiagnostics(writer: CommentWriter): CommentWriter { @@ -344,7 +297,7 @@ namespace ts { function getLeadingCommentsWithoutDetachedComments() { // get the leading comments from detachedPos const pos = lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos; - const leadingComments = getLeadingCommentRanges(currentText, pos); + const leadingComments = getLeadingCommentRanges(currentText, pos, consumedCommentRanges); if (detachedCommentsInfo.length - 1) { detachedCommentsInfo.pop(); } diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index ec3b1234adf..254923a981e 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -591,7 +591,7 @@ namespace ts { * and the next token are returned. * If true, comments occurring between the given position and the next line break are returned. */ - function getCommentRanges(text: string, pos: number, trailing: boolean): CommentRange[] { + function getCommentRanges(text: string, pos: number, trailing: boolean, consumedCommentRanges?: Map): CommentRange[] { let result: CommentRange[]; let collecting = trailing || pos === 0; while (pos >= 0 && pos < text.length) { @@ -643,13 +643,17 @@ namespace ts { } } - if (collecting) { + if (collecting && (!consumedCommentRanges || !(startPos in consumedCommentRanges))) { if (!result) { result = []; } result.push({ pos: startPos, end: pos, hasTrailingNewLine, kind }); + if (consumedCommentRanges) { + consumedCommentRanges[startPos] = true; + } } + continue; } break; @@ -669,12 +673,12 @@ namespace ts { return result; } - export function getLeadingCommentRanges(text: string, pos: number): CommentRange[] { - return getCommentRanges(text, pos, /*trailing*/ false); + export function getLeadingCommentRanges(text: string, pos: number, consumedCommentRanges?: Map): CommentRange[] { + return getCommentRanges(text, pos, /*trailing*/ false, consumedCommentRanges); } - export function getTrailingCommentRanges(text: string, pos: number): CommentRange[] { - return getCommentRanges(text, pos, /*trailing*/ true); + export function getTrailingCommentRanges(text: string, pos: number, consumedCommentRanges?: Map): CommentRange[] { + return getCommentRanges(text, pos, /*trailing*/ true, consumedCommentRanges); } /** Optionally, get the shebang */