Skip to content

Commit

Permalink
fix(45501): don't remove spaces between empty object literals with en…
Browse files Browse the repository at this point in the history
…abled insertSpaceAfterOpeningAndBeforeClosingEmptyBraces (microsoft#45514)
  • Loading branch information
a-tarasyuk authored and BobobUnicorn committed Oct 23, 2021
1 parent 8411870 commit 5e66a1e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/services/formatting/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ namespace ts.formatting {
// Also should not apply to })
rule("SpaceBetweenCloseBraceAndElse", SyntaxKind.CloseBraceToken, SyntaxKind.ElseKeyword, [isNonJsxSameLineTokenContext], RuleAction.InsertSpace),
rule("SpaceBetweenCloseBraceAndWhile", SyntaxKind.CloseBraceToken, SyntaxKind.WhileKeyword, [isNonJsxSameLineTokenContext], RuleAction.InsertSpace),
rule("NoSpaceBetweenEmptyBraceBrackets", SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, [isNonJsxSameLineTokenContext, isObjectContext], RuleAction.DeleteSpace),

// Add a space after control dec context if the next character is an open bracket ex: 'if (false)[a, b] = [1, 2];' -> 'if (false) [a, b] = [1, 2];'
rule("SpaceAfterConditionalClosingParen", SyntaxKind.CloseParenToken, SyntaxKind.OpenBracketToken, [isControlDeclContext], RuleAction.InsertSpace),
Expand Down Expand Up @@ -270,9 +269,9 @@ namespace ts.formatting {
// Insert a space after { and before } in single-line contexts, but remove space from empty object literals {}.
rule("SpaceAfterOpenBrace", SyntaxKind.OpenBraceToken, anyToken, [isOptionEnabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces"), isBraceWrappedContext], RuleAction.InsertSpace),
rule("SpaceBeforeCloseBrace", anyToken, SyntaxKind.CloseBraceToken, [isOptionEnabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces"), isBraceWrappedContext], RuleAction.InsertSpace),
rule("NoSpaceBetweenEmptyBraceBrackets", SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, [isNonJsxSameLineTokenContext, isObjectContext], RuleAction.DeleteSpace),
rule("NoSpaceAfterOpenBrace", SyntaxKind.OpenBraceToken, anyToken, [isOptionDisabled("insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces"), isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
rule("NoSpaceBeforeCloseBrace", anyToken, SyntaxKind.CloseBraceToken, [isOptionDisabled("insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces"), isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
rule("NoSpaceBetweenEmptyBraceBrackets", SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, [isOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingEmptyBraces"), isNonJsxSameLineTokenContext, isObjectContext], RuleAction.DeleteSpace),

// Insert a space after opening and before closing empty brace brackets
rule("SpaceBetweenEmptyBraceBrackets", SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, [isOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingEmptyBraces")], RuleAction.InsertSpace),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path="fourslash.ts"/>

////const a = { };
////const b = {};

format.setOption("insertSpaceAfterOpeningAndBeforeClosingEmptyBraces", false);
format.document();
verify.currentFileContentIs(
`const a = {};
const b = {};`
);
11 changes: 11 additions & 0 deletions tests/cases/fourslash/formattingSpaceBetweenEmptyBraceBrackets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path="fourslash.ts"/>

////const a = { };
////const b = {};

format.setOption("insertSpaceAfterOpeningAndBeforeClosingEmptyBraces", true);
format.document();
verify.currentFileContentIs(
`const a = { };
const b = { };`
);

0 comments on commit 5e66a1e

Please sign in to comment.