Skip to content

Commit 3ca2a7d

Browse files
committed
Merge pull request #696 from Microsoft/format_code
add 'skipTrivia' parameter to scanner
2 parents dfeaabe + 7188ab6 commit 3ca2a7d

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

src/compiler/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3815,7 +3815,7 @@ module ts {
38153815
: undefined);
38163816
}
38173817

3818-
scanner = createScanner(languageVersion, sourceText, scanError, onComment);
3818+
scanner = createScanner(languageVersion, /*skipTrivia*/ true, sourceText, scanError, onComment);
38193819
var rootNodeFlags: NodeFlags = 0;
38203820
if (fileExtensionIs(filename, ".d.ts")) {
38213821
rootNodeFlags = NodeFlags.DeclarationFile;

src/compiler/scanner.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ module ts {
460460
ch > CharacterCodes.maxAsciiCharacter && isUnicodeIdentifierPart(ch, languageVersion);
461461
}
462462

463-
export function createScanner(languageVersion: ScriptTarget, text?: string, onError?: ErrorCallback, onComment?: CommentCallback): Scanner {
463+
export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean, text?: string, onError?: ErrorCallback, onComment?: CommentCallback): Scanner {
464464
var pos: number; // Current position (end position of text of current token)
465465
var len: number; // Length of text
466466
var startPos: number; // Start position of whitespace before current token
@@ -694,12 +694,34 @@ module ts {
694694
case CharacterCodes.lineFeed:
695695
case CharacterCodes.carriageReturn:
696696
precedingLineBreak = true;
697+
if (skipTrivia) {
698+
pos++;
699+
continue;
700+
}
701+
else {
702+
if (ch === CharacterCodes.carriageReturn && pos + 1 < len && text.charCodeAt(pos + 1) === CharacterCodes.lineFeed) {
703+
// consume both CR and LF
704+
pos += 2;
705+
}
706+
else {
707+
pos++;
708+
}
709+
return token = SyntaxKind.NewLineTrivia;
710+
}
697711
case CharacterCodes.tab:
698712
case CharacterCodes.verticalTab:
699713
case CharacterCodes.formFeed:
700714
case CharacterCodes.space:
701-
pos++;
702-
continue;
715+
if (skipTrivia) {
716+
pos++;
717+
continue;
718+
}
719+
else {
720+
while (pos < len && isWhiteSpace(text.charCodeAt(pos))) {
721+
pos++;
722+
}
723+
return token = SyntaxKind.WhitespaceTrivia;
724+
}
703725
case CharacterCodes.exclamation:
704726
if (text.charCodeAt(pos + 1) === CharacterCodes.equals) {
705727
if (text.charCodeAt(pos + 2) === CharacterCodes.equals) {
@@ -776,7 +798,13 @@ module ts {
776798
if (onComment) {
777799
onComment(tokenPos, pos);
778800
}
779-
continue;
801+
802+
if (skipTrivia) {
803+
continue;
804+
}
805+
else {
806+
return token = SyntaxKind.SingleLineCommentTrivia;
807+
}
780808
}
781809
// Multi-line comment
782810
if (text.charCodeAt(pos + 1) === CharacterCodes.asterisk) {
@@ -806,7 +834,12 @@ module ts {
806834
onComment(tokenPos, pos);
807835
}
808836

809-
continue;
837+
if (skipTrivia) {
838+
continue;
839+
}
840+
else {
841+
return token = SyntaxKind.MultiLineCommentTrivia;
842+
}
810843
}
811844

812845
if (text.charCodeAt(pos + 1) === CharacterCodes.equals) {

src/compiler/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ module ts {
1212
export enum SyntaxKind {
1313
Unknown,
1414
EndOfFileToken,
15+
SingleLineCommentTrivia,
16+
MultiLineCommentTrivia,
17+
NewLineTrivia,
18+
WhitespaceTrivia,
1519
// Literals
1620
NumericLiteral,
1721
StringLiteral,

src/services/services.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module ts {
7575
update(scriptSnapshot: TypeScript.IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TypeScript.TextChangeRange): SourceFile;
7676
}
7777

78-
var scanner: Scanner = createScanner(ScriptTarget.ES5);
78+
var scanner: Scanner = createScanner(ScriptTarget.ES5, /*skipTrivia*/ true);
7979

8080
var emptyArray: any[] = [];
8181

@@ -4070,7 +4070,7 @@ module ts {
40704070
entries: []
40714071
};
40724072

4073-
scanner = createScanner(ScriptTarget.ES5, text, onError, processComment);
4073+
scanner = createScanner(ScriptTarget.ES5, /*skipTrivia*/ true, text, onError, processComment);
40744074

40754075
var token = SyntaxKind.Unknown;
40764076
do {

0 commit comments

Comments
 (0)