diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index b1ffe3fdb22..4a7033c6f3e 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -445,7 +445,7 @@ namespace ts.formatting { if ((node).asteriskToken) { return SyntaxKind.AsteriskToken; } - // fall-through + // fall-through case SyntaxKind.PropertyDeclaration: case SyntaxKind.Parameter: @@ -788,7 +788,7 @@ namespace ts.formatting { } else { lineAdded = - processPair(range, rangeStart.line, parent, previousRange, previousRangeStartLine, previousParent, contextNode, dynamicIndentation) + processPair(range, rangeStart.line, parent, previousRange, previousRangeStartLine, previousParent, contextNode, dynamicIndentation) } } @@ -934,7 +934,7 @@ namespace ts.formatting { let lineEndPosition = getEndLinePosition(line, sourceFile); // do not trim whitespaces in comments or template expression - if (range && (isComment(range.kind) || isTemplate(range.kind)) && range.pos <= lineEndPosition && range.end > lineEndPosition) { + if (range && (isComment(range.kind) || isStringOrRegularExpressionOrTemplateLiteral(range.kind)) && range.pos <= lineEndPosition && range.end > lineEndPosition) { continue; } diff --git a/src/services/utilities.ts b/src/services/utilities.ts index f08f3f38460..431e4286eec 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -9,7 +9,7 @@ namespace ts { export function getEndLinePosition(line: number, sourceFile: SourceFile): number { Debug.assert(line >= 0); let lineStarts = sourceFile.getLineStarts(); - + let lineIndex = line; if (lineIndex + 1 === lineStarts.length) { // last line - return EOF @@ -170,7 +170,7 @@ namespace ts { case SyntaxKind.VoidExpression: case SyntaxKind.YieldExpression: case SyntaxKind.SpreadElementExpression: - let unaryWordExpression = (n); + let unaryWordExpression = (n); return isCompletedNode(unaryWordExpression.expression, sourceFile); case SyntaxKind.TaggedTemplateExpression: @@ -252,7 +252,7 @@ namespace ts { }); // Either we didn't find an appropriate list, or the list must contain us. - Debug.assert(!syntaxList || contains(syntaxList.getChildren(), node)); + Debug.assert(!syntaxList || contains(syntaxList.getChildren(), node)); return syntaxList; } @@ -388,7 +388,7 @@ namespace ts { // if this is the case - then we should assume that token in question is located in previous child. if (position < child.end && (nodeHasTokens(child) || child.kind === SyntaxKind.JsxText)) { const start = child.getStart(sourceFile); - const lookInPreviousChild = + const lookInPreviousChild = (start >= position) || // cursor in the leading trivia (child.kind === SyntaxKind.JsxText && start === child.end); // whitespace only JsxText @@ -425,7 +425,7 @@ namespace ts { } } } - + export function isInString(sourceFile: SourceFile, position: number) { let token = getTokenAtPosition(sourceFile, position); return token && token.kind === SyntaxKind.StringLiteral && position > token.getStart(); @@ -473,7 +473,7 @@ namespace ts { let commentRanges = getLeadingCommentRanges(sourceFile.text, token.pos); return forEach(commentRanges, jsDocPrefix); - + function jsDocPrefix(c: CommentRange): boolean { var text = sourceFile.text; return text.length >= c.pos + 3 && text[c.pos] === '/' && text[c.pos + 1] === '*' && text[c.pos + 2] === '*'; @@ -562,8 +562,13 @@ namespace ts { return kind === SyntaxKind.SingleLineCommentTrivia || kind === SyntaxKind.MultiLineCommentTrivia; } - export function isTemplate(kind: SyntaxKind): boolean { - return kind >= SyntaxKind.FirstTemplateToken && kind <= SyntaxKind.LastTemplateToken; + export function isStringOrRegularExpressionOrTemplateLiteral(kind: SyntaxKind): boolean { + if (kind === SyntaxKind.StringLiteral + || kind === SyntaxKind.RegularExpressionLiteral + || isTemplateLiteralKind(kind)) { + return true; + } + return false; } export function isPunctuation(kind: SyntaxKind): boolean { @@ -693,7 +698,7 @@ namespace ts { } export function displayPart(text: string, kind: SymbolDisplayPartKind, symbol?: Symbol): SymbolDisplayPart { - return { + return { text: text, kind: SymbolDisplayPartKind[kind] }; diff --git a/tests/cases/fourslash/whiteSpaceTrimming3.ts b/tests/cases/fourslash/whiteSpaceTrimming3.ts new file mode 100644 index 00000000000..93d236773e4 --- /dev/null +++ b/tests/cases/fourslash/whiteSpaceTrimming3.ts @@ -0,0 +1,10 @@ +/// + +////let t = "foo \ +////bar \ +////"/*1*/ + +goTo.marker('1'); +edit.insert(";"); + +verify.currentFileContentIs("let t = \"foo \\\nbar \\ \n\";"); diff --git a/tests/cases/fourslash/whiteSpaceTrimming4.ts b/tests/cases/fourslash/whiteSpaceTrimming4.ts new file mode 100644 index 00000000000..59223197681 --- /dev/null +++ b/tests/cases/fourslash/whiteSpaceTrimming4.ts @@ -0,0 +1,8 @@ +/// + +////var re = /\w+ /*1*//; + +goTo.marker('1'); +edit.insert("\n"); + +verify.currentFileContentIs("var re = /\\w+ \n /;");