From dffa8b1329f97802323c63743f358eca486d6ce9 Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 18 Jan 2018 09:44:30 -0800 Subject: [PATCH] Add a TriviaKind type to allow more specific types (#21237) --- src/compiler/utilities.ts | 8 ++- src/services/classifier.ts | 67 +++++++++++--------- src/services/formatting/formatting.ts | 8 ++- src/services/formatting/formattingScanner.ts | 8 +-- 4 files changed, 55 insertions(+), 36 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index b07ceb46099..4189ef89e75 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2029,7 +2029,13 @@ namespace ts { return token !== undefined && isNonContextualKeyword(token); } - export function isTrivia(token: SyntaxKind) { + export type TriviaKind = SyntaxKind.SingleLineCommentTrivia + | SyntaxKind.MultiLineCommentTrivia + | SyntaxKind.NewLineTrivia + | SyntaxKind.WhitespaceTrivia + | SyntaxKind.ShebangTrivia + | SyntaxKind.ConflictMarkerTrivia; + export function isTrivia(token: SyntaxKind): token is TriviaKind { return SyntaxKind.FirstTriviaToken <= token && token <= SyntaxKind.LastTriviaToken; } diff --git a/src/services/classifier.ts b/src/services/classifier.ts index 3e4c6058a36..aff626ca9fe 100644 --- a/src/services/classifier.ts +++ b/src/services/classifier.ts @@ -619,37 +619,46 @@ namespace ts { return start; } - // Don't bother with newlines/whitespace. - if (kind === SyntaxKind.NewLineTrivia || kind === SyntaxKind.WhitespaceTrivia) { - continue; - } - - // Only bother with the trivia if it at least intersects the span of interest. - if (isComment(kind)) { - classifyComment(token, kind, start, width); - - // Classifying a comment might cause us to reuse the trivia scanner - // (because of jsdoc comments). So after we classify the comment make - // sure we set the scanner position back to where it needs to be. - triviaScanner.setTextPos(end); - continue; - } - - if (kind === SyntaxKind.ConflictMarkerTrivia) { - const text = sourceFile.text; - const ch = text.charCodeAt(start); - - // for the <<<<<<< and >>>>>>> markers, we just add them in as comments - // in the classification stream. - if (ch === CharacterCodes.lessThan || ch === CharacterCodes.greaterThan) { - pushClassification(start, width, ClassificationType.comment); + switch (kind) { + case SyntaxKind.NewLineTrivia: + case SyntaxKind.WhitespaceTrivia: + // Don't bother with newlines/whitespace. continue; - } - // for the ||||||| and ======== markers, add a comment for the first line, - // and then lex all subsequent lines up until the end of the conflict marker. - Debug.assert(ch === CharacterCodes.bar || ch === CharacterCodes.equals); - classifyDisabledMergeCode(text, start, end); + case SyntaxKind.SingleLineCommentTrivia: + case SyntaxKind.MultiLineCommentTrivia: + // Only bother with the trivia if it at least intersects the span of interest. + classifyComment(token, kind, start, width); + + // Classifying a comment might cause us to reuse the trivia scanner + // (because of jsdoc comments). So after we classify the comment make + // sure we set the scanner position back to where it needs to be. + triviaScanner.setTextPos(end); + continue; + + case SyntaxKind.ConflictMarkerTrivia: + const text = sourceFile.text; + const ch = text.charCodeAt(start); + + // for the <<<<<<< and >>>>>>> markers, we just add them in as comments + // in the classification stream. + if (ch === CharacterCodes.lessThan || ch === CharacterCodes.greaterThan) { + pushClassification(start, width, ClassificationType.comment); + continue; + } + + // for the ||||||| and ======== markers, add a comment for the first line, + // and then lex all subsequent lines up until the end of the conflict marker. + Debug.assert(ch === CharacterCodes.bar || ch === CharacterCodes.equals); + classifyDisabledMergeCode(text, start, end); + break; + + case SyntaxKind.ShebangTrivia: + // TODO: Maybe we should classify these. + break; + + default: + Debug.assertNever(kind); } } } diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index db224fecb96..818e2815944 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -14,10 +14,14 @@ namespace ts.formatting { kind: SyntaxKind; } + export interface TextRangeWithTriviaKind extends TextRange { + kind: TriviaKind; + } + export interface TokenInfo { - leadingTrivia: TextRangeWithKind[]; + leadingTrivia: TextRangeWithTriviaKind[]; token: TextRangeWithKind; - trailingTrivia: TextRangeWithKind[]; + trailingTrivia: TextRangeWithTriviaKind[]; } const enum Constants { diff --git a/src/services/formatting/formattingScanner.ts b/src/services/formatting/formattingScanner.ts index f126327fa06..9b9c956f670 100644 --- a/src/services/formatting/formattingScanner.ts +++ b/src/services/formatting/formattingScanner.ts @@ -31,8 +31,8 @@ namespace ts.formatting { scanner.setTextPos(startPos); let wasNewLine = true; - let leadingTrivia: TextRangeWithKind[] | undefined; - let trailingTrivia: TextRangeWithKind[] | undefined; + let leadingTrivia: TextRangeWithTriviaKind[] | undefined; + let trailingTrivia: TextRangeWithTriviaKind[] | undefined; let savedPos: number; let lastScanAction: ScanAction | undefined; @@ -77,7 +77,7 @@ namespace ts.formatting { // consume leading trivia scanner.scan(); - const item = { + const item: TextRangeWithTriviaKind = { pos, end: scanner.getStartPos(), kind: t @@ -188,7 +188,7 @@ namespace ts.formatting { if (!isTrivia(currentToken)) { break; } - const trivia = { + const trivia: TextRangeWithTriviaKind = { pos: scanner.getStartPos(), end: scanner.getTextPos(), kind: currentToken