Skip to content

Commit be56670

Browse files
committed
Adding getLeadingComments and getTrailingComments methods to scanner.
1 parent 1a4a822 commit be56670

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

src/compiler/scanner.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,81 @@ module ts {
354354
}
355355
}
356356

357+
// Extract comments from the given source text starting at the given position. If trailing is false, whitespace is skipped until
358+
// the first line break and comments between that location and the next token are returned. If trailing is true, comments occurring
359+
// between the given position and the next line break are returned. The return value is an array containing a TextRange for each
360+
// comment. Single-line comment ranges include the the beginning '//' characters but not the ending line break. Multi-line comment
361+
// ranges include the beginning '/* and ending '*/' characters. The return value is undefined if no comments were found.
362+
function getCommentRanges(text: string, pos: number, trailing: boolean): TextRange[] {
363+
var result: TextRange[];
364+
var collecting = trailing;
365+
while (true) {
366+
var ch = text.charCodeAt(pos);
367+
switch (ch) {
368+
case CharacterCodes.carriageReturn:
369+
if (text.charCodeAt(pos + 1) === CharacterCodes.lineFeed) pos++;
370+
case CharacterCodes.lineFeed:
371+
pos++;
372+
if (trailing) {
373+
return result;
374+
}
375+
collecting = true;
376+
continue;
377+
case CharacterCodes.tab:
378+
case CharacterCodes.verticalTab:
379+
case CharacterCodes.formFeed:
380+
case CharacterCodes.space:
381+
pos++;
382+
continue;
383+
case CharacterCodes.slash:
384+
var nextChar = text.charCodeAt(pos + 1);
385+
if (nextChar === CharacterCodes.slash || nextChar === CharacterCodes.asterisk) {
386+
var startPos = pos;
387+
var comment: string = undefined;
388+
pos += 2;
389+
if (nextChar === CharacterCodes.slash) {
390+
while (pos < text.length) {
391+
if (isLineBreak(text.charCodeAt(pos))) {
392+
break;
393+
}
394+
pos++;
395+
}
396+
}
397+
else {
398+
while (pos < text.length) {
399+
if (text.charCodeAt(pos) === CharacterCodes.asterisk && text.charCodeAt(pos + 1) === CharacterCodes.slash) {
400+
pos += 2;
401+
break;
402+
}
403+
pos++;
404+
}
405+
}
406+
if (collecting) {
407+
if (!result) result = [];
408+
result.push({ pos: startPos, end: pos });
409+
}
410+
continue;
411+
}
412+
break;
413+
default:
414+
if (ch > CharacterCodes.maxAsciiCharacter && (isWhiteSpace(ch) || isLineBreak(ch))) {
415+
pos++;
416+
continue;
417+
}
418+
break;
419+
}
420+
return result;
421+
}
422+
}
423+
424+
export function getLeadingComments(text: string, pos: number): TextRange[] {
425+
return getCommentRanges(text, pos, /*trailing*/ false);
426+
}
427+
428+
export function getTrailingComments(text: string, pos: number): TextRange[] {
429+
return getCommentRanges(text, pos, /*trailing*/ true);
430+
}
431+
357432
export function createScanner(languageVersion: ScriptTarget, text?: string, onError?: ErrorCallback, onComment?: CommentCallback): Scanner {
358433
var pos: number; // Current position (end position of text of current token)
359434
var len: number; // Length of text

0 commit comments

Comments
 (0)