Moved responsibility for consuming comment ranges.

This commit is contained in:
Ron Buckton 2016-05-26 10:34:34 -07:00
parent 22f3123228
commit 87fc46c013
2 changed files with 16 additions and 59 deletions

View File

@ -31,7 +31,7 @@ namespace ts {
// This maps start->end for a comment range. See `hasConsumedCommentRange` and
// `consumeCommentRange` for usage.
let consumedCommentRanges: Map<number>;
let consumedCommentRanges: Map<boolean>;
let leadingCommentRangePositions: Map<boolean>;
let trailingCommentRangePositions: Map<boolean>;
@ -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();
}

View File

@ -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<boolean>): 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<boolean>): 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<boolean>): CommentRange[] {
return getCommentRanges(text, pos, /*trailing*/ true, consumedCommentRanges);
}
/** Optionally, get the shebang */