Skip to content

fix(45501): split insertSpaceAfterOpeningAndBeforeClosingEmptyBraces to insertSpaceInEmptyObjects/insertSpaceInEmptyBlocks #45779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3418,6 +3418,7 @@ export interface FormatCodeSettings extends EditorSettings {
insertSpaceAfterConstructor?: boolean;
insertSpaceAfterKeywordsInControlFlowStatements?: boolean;
insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean;
/** @deprecated Use insertSpaceInEmptyBlocks instead. */
insertSpaceAfterOpeningAndBeforeClosingEmptyBraces?: boolean;
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean;
Expand All @@ -3426,6 +3427,8 @@ export interface FormatCodeSettings extends EditorSettings {
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean;
insertSpaceAfterTypeAssertion?: boolean;
insertSpaceBeforeFunctionParenthesis?: boolean;
insertSpaceInEmptyObjects?: boolean;
insertSpaceInEmptyBlocks?: boolean;
placeOpenBraceOnNewLineForFunctions?: boolean;
placeOpenBraceOnNewLineForControlBlocks?: boolean;
insertSpaceBeforeTypeAnnotation?: boolean;
Expand Down
14 changes: 8 additions & 6 deletions src/services/formatting/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import {
BinaryExpression, contains, findAncestor, findNextToken, FormatCodeSettings, hasDecorators, hasProperty, isArray,
isExpression, isFunctionLikeKind, isNumericLiteral, isPropertyAccessExpression, isPropertyDeclaration,
isPropertySignature, isTrivia, Node, positionIsASICandidate, SemicolonPreference, SyntaxKind, typeKeywords,
isPropertySignature, isTrivia, Node, or, positionIsASICandidate, SemicolonPreference, SyntaxKind, typeKeywords,
YieldExpression,
} from "../_namespaces/ts";

Expand Down Expand Up @@ -106,7 +106,6 @@ export function getAllRules(): RuleSpec[] {
// 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 @@ -282,13 +281,16 @@ export function getAllRules(): RuleSpec[] {
// 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),

// Insert a space after opening and before closing empty brace brackets
rule("SpaceBetweenEmptyBraceBrackets", SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, [isOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingEmptyBraces")], RuleAction.InsertSpace),
rule("NoSpaceBetweenEmptyBraceBrackets", SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, [isOptionDisabled("insertSpaceAfterOpeningAndBeforeClosingEmptyBraces"), isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
// Insert a space after opening and before closing empty block brackets
rule("SpaceBetweenEmptyBlockBrackets", SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, [or(isOptionEnabled("insertSpaceInEmptyBlocks"), isOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingEmptyBraces"))], RuleAction.InsertSpace),
rule("NoSpaceBetweenEmptyBlockBrackets", SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, [or(isOptionDisabled("insertSpaceInEmptyBlocks"), isOptionDisabled("insertSpaceAfterOpeningAndBeforeClosingEmptyBraces")), isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),

// Insert a space after opening and before closing empty object brackets
rule("SpaceBetweenEmptyObjectBrackets", SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, [isOptionEnabled("insertSpaceInEmptyObjects"), isObjectContext], RuleAction.InsertSpace),
rule("NoSpaceBetweenEmptyObjectBrackets", SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, [isOptionDisabledOrUndefined("insertSpaceInEmptyObjects"), isObjectContext, isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),

// Insert space after opening and before closing template string braces
rule("SpaceAfterTemplateHeadAndMiddle", [SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle], anyToken, [isOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), isNonJsxTextContext], RuleAction.InsertSpace, RuleFlags.CanDeleteNewLines),
Expand Down
3 changes: 3 additions & 0 deletions src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1046,11 +1046,14 @@ export interface FormatCodeSettings extends EditorSettings {
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces?: boolean;
/** @deprecated Use insertSpaceInEmptyBlocks instead. */
readonly insertSpaceAfterOpeningAndBeforeClosingEmptyBraces?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean;
readonly insertSpaceAfterTypeAssertion?: boolean;
readonly insertSpaceBeforeFunctionParenthesis?: boolean;
readonly insertSpaceInEmptyObjects?: boolean;
readonly insertSpaceInEmptyBlocks?: boolean;
readonly placeOpenBraceOnNewLineForFunctions?: boolean;
readonly placeOpenBraceOnNewLineForControlBlocks?: boolean;
readonly insertSpaceBeforeTypeAnnotation?: boolean;
Expand Down
6 changes: 6 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2719,6 +2719,7 @@ declare namespace ts {
insertSpaceAfterConstructor?: boolean;
insertSpaceAfterKeywordsInControlFlowStatements?: boolean;
insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean;
/** @deprecated Use insertSpaceInEmptyBlocks instead. */
insertSpaceAfterOpeningAndBeforeClosingEmptyBraces?: boolean;
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean;
Expand All @@ -2727,6 +2728,8 @@ declare namespace ts {
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean;
insertSpaceAfterTypeAssertion?: boolean;
insertSpaceBeforeFunctionParenthesis?: boolean;
insertSpaceInEmptyObjects?: boolean;
insertSpaceInEmptyBlocks?: boolean;
placeOpenBraceOnNewLineForFunctions?: boolean;
placeOpenBraceOnNewLineForControlBlocks?: boolean;
insertSpaceBeforeTypeAnnotation?: boolean;
Expand Down Expand Up @@ -10466,11 +10469,14 @@ declare namespace ts {
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces?: boolean;
/** @deprecated Use insertSpaceInEmptyBlocks instead. */
readonly insertSpaceAfterOpeningAndBeforeClosingEmptyBraces?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean;
readonly insertSpaceAfterTypeAssertion?: boolean;
readonly insertSpaceBeforeFunctionParenthesis?: boolean;
readonly insertSpaceInEmptyObjects?: boolean;
readonly insertSpaceInEmptyBlocks?: boolean;
readonly placeOpenBraceOnNewLineForFunctions?: boolean;
readonly placeOpenBraceOnNewLineForControlBlocks?: boolean;
readonly insertSpaceBeforeTypeAnnotation?: boolean;
Expand Down
3 changes: 3 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6601,11 +6601,14 @@ declare namespace ts {
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces?: boolean;
/** @deprecated Use insertSpaceInEmptyBlocks instead. */
readonly insertSpaceAfterOpeningAndBeforeClosingEmptyBraces?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean;
readonly insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean;
readonly insertSpaceAfterTypeAssertion?: boolean;
readonly insertSpaceBeforeFunctionParenthesis?: boolean;
readonly insertSpaceInEmptyObjects?: boolean;
readonly insertSpaceInEmptyBlocks?: boolean;
readonly placeOpenBraceOnNewLineForFunctions?: boolean;
readonly placeOpenBraceOnNewLineForControlBlocks?: boolean;
readonly insertSpaceBeforeTypeAnnotation?: boolean;
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/fourslash/formattingOptionsChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/////*placeOpenBraceOnNewLineForControlBlocks*/if (true) {
////}
/////*insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces*/{ var t = 1}; var {a,b } = { a: 'sw', b:'r' };function f( { a, b}) { }
/////*insertSpaceInEmptyBlocks*/constructor() { }
/////*insertSpaceAfterOpeningAndBeforeClosingEmptyBraces*/constructor() { }

const defaultFormatOption = format.copyFormatOptions();
Expand All @@ -32,6 +33,7 @@ runTest("insertSpaceBeforeTypeAnnotation", "const bar : number = 1;", "const bar
runTest("placeOpenBraceOnNewLineForFunctions", "class foo", "class foo {");
runTest("placeOpenBraceOnNewLineForControlBlocks", "if (true)", "if (true) {");
runTest("insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces", "{ var t = 1 }; var { a, b } = { a: 'sw', b: 'r' }; function f({ a, b }) { }", "{var t = 1}; var {a, b} = {a: 'sw', b: 'r'}; function f({a, b}) {}");
runTest("insertSpaceInEmptyBlocks", "constructor() { }", "constructor() {}");
runTest("insertSpaceAfterOpeningAndBeforeClosingEmptyBraces", "constructor() { }", "constructor() {}");

function runTest(propertyName: string, expectedStringWhenTrue: string, expectedStringWhenFalse: string) {
Expand Down
11 changes: 11 additions & 0 deletions tests/cases/fourslash/formattingSpaceInEmptyObjects1.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("insertSpaceInEmptyObjects", true);
format.document();
verify.currentFileContentIs(
`const a = { };
const b = { };`
);
11 changes: 11 additions & 0 deletions tests/cases/fourslash/formattingSpaceInEmptyObjects2.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("insertSpaceInEmptyObjects", false);
format.document();
verify.currentFileContentIs(
`const a = {};
const b = {};`
);
10 changes: 10 additions & 0 deletions tests/cases/fourslash/formattingSpaceInEmptyObjects3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path="fourslash.ts"/>

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

format.document();
verify.currentFileContentIs(
`const a = {};
const b = {};`
);