From a5c8a29fa4a64854d4f497c6152f74598487f723 Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Wed, 28 Jun 2017 12:48:14 -0700 Subject: [PATCH] only format opencurly if no intervening tokens --- src/services/formatting/formatting.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index f6eaa812f45..aa127621b58 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -104,7 +104,10 @@ namespace ts.formatting { export function formatOnOpeningCurly(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] { const openingCurly = findImmediatelyPrecedingTokenOfKind(position, SyntaxKind.OpenBraceToken, sourceFile); const curlyBraceRange = openingCurly && openingCurly.parent; - return formatOutermostNodeWithinListLevel(curlyBraceRange, sourceFile, options, rulesProvider, FormattingRequestKind.FormatOnOpeningCurlyBrace); + const nextToken = curlyBraceRange && findNextToken(openingCurly, curlyBraceRange); + return nextToken && nextToken.kind === SyntaxKind.CloseBraceToken ? + formatOutermostNodeWithinListLevel(curlyBraceRange, sourceFile, options, rulesProvider, FormattingRequestKind.FormatOnOpeningCurlyBrace) : + []; } export function formatOnClosingCurly(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] { @@ -130,8 +133,8 @@ namespace ts.formatting { } /** - * Validating `expectedLastToken` ensures the token was typed in the context we expect (eg: not a comment). - * @param expectedLastToken The last token constituting the desired parent node. + * Validating `expectedTokenKind` ensures the token was typed in the context we expect (eg: not a comment). + * @param expectedTokenKind The kind of the last token constituting the desired parent node. */ function findImmediatelyPrecedingTokenOfKind(end: number, expectedTokenKind: SyntaxKind, sourceFile: SourceFile): Node | undefined { const precedingToken = findPrecedingToken(end, sourceFile); @@ -142,8 +145,8 @@ namespace ts.formatting { } /** - * Finds and formats the highest node enclosing `position` whose end does not exceed the given position - * and is at the same list level as the token at `position`. + * Finds and formats the highest node enclosing `node` whose end does not exceed the `node.end` + * and is at the same list level as the token at `node`. * * Consider typing the following * ```