diff --git a/changelog_unreleased/typescript/19583.md b/changelog_unreleased/typescript/19583.md new file mode 100644 index 000000000..694965c5c --- /dev/null +++ b/changelog_unreleased/typescript/19583.md @@ -0,0 +1,20 @@ +#### Remove space in comment-only object type (#19583 by @fisker) + + +```tsx +// Input +var foo = { + /* comment */ +}; +type Foo = { + /* comment */ +}; + +// Prettier stable +var foo = {/* comment */}; +type Foo = { /* comment */ }; + +// Prettier main +var foo = {/* comment */}; +type Foo = {/* comment */}; +``` diff --git a/src/language-js/print/class-body.js b/src/language-js/print/class-body.js index 0c9833bd4..56fb7d258 100644 --- a/src/language-js/print/class-body.js +++ b/src/language-js/print/class-body.js @@ -39,10 +39,12 @@ function printClassBody(path, options, print) { const [openingBrace, closingBrace] = isFlowTypeAnnotation && node.exact ? ["{|", "|}"] : "{}"; + let isEmpty = true; let firstMember; iterateClassMembersPath(path, ({ node, next, isLast }) => { firstMember ??= node; + isEmpty &&= false; parts.push(print()); if (!isFlowRecordDeclaration && isObjectType && isFlowTypeAnnotation) { @@ -83,6 +85,7 @@ function printClassBody(path, options, print) { // TODO: this part can unify with the similar part in `printObject` if (node.type === "ObjectTypeAnnotation" && node.inexact) { + isEmpty &&= false; let printed; if (hasComment(node, CommentCheckFlags.Dangling)) { const hasLineComments = hasComment(node, CommentCheckFlags.Line); @@ -114,7 +117,8 @@ function printClassBody(path, options, print) { if (parts.length === 0) { content = openingBrace + closingBrace; } else { - const spacing = options.bracketSpacing ? line : softline; + const spacing = + !options.bracketSpacing || (isEmpty && !shouldBreak) ? softline : line; content = [ openingBrace, indent([spacing, ...parts]), diff --git a/tests/format/flow/object-inexact/comments/__snapshots__/format.test.js.snap b/tests/format/flow/object-inexact/comments/__snapshots__/format.test.js.snap index 3bdecda49..5566296cc 100644 --- a/tests/format/flow/object-inexact/comments/__snapshots__/format.test.js.snap +++ b/tests/format/flow/object-inexact/comments/__snapshots__/format.test.js.snap @@ -43,16 +43,16 @@ T6 = {/* comment */ // comment }; =====================================output===================================== -type T1 = {| /* comment */ |}; -type T12 = { /* comment */ }; +type T1 = {|/* comment */|}; +type T12 = {/* comment */}; T1 = {/* comment */}; -type T2 = {| /* comment */ |}; -type T22 = { /* comment */ }; +type T2 = {|/* comment */|}; +type T22 = {/* comment */}; T2 = {/* comment */}; -type T3 = {| /* comment */ |}; -type T32 = { /* comment */ }; +type T3 = {|/* comment */|}; +type T32 = {/* comment */}; T3 = {/* comment */}; type T4 = {| diff --git a/tests/format/flow/object-type/empty/__snapshots__/format.test.js.snap b/tests/format/flow/object-type/empty/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..762839b0a --- /dev/null +++ b/tests/format/flow/object-type/empty/__snapshots__/format.test.js.snap @@ -0,0 +1,97 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`empty.js - {"bracketSpacing":false} format 1`] = ` +====================================options===================================== +bracketSpacing: false +parsers: ["flow"] + printWidth: 80 (default) | +=====================================input====================================== +a = []; +a = [/*c*/]; +type A11 = []; +type A12 = [/*c*/]; + +o = {}; +o = {/*c*/}; +type O21 = {}; +type O22 = {/*c*/}; +type O23 = {...}; +type O24 = {/*c*/ ...}; +type O31 = {||}; +type O32 = {|/*c*/|}; +// type O33 = {|...|}; +// type O34 = {|/*c*/ ...|}; +interface I21 {} +interface I22 {/*c*/} + +=====================================output===================================== +a = []; +a = [/*c*/]; +type A11 = []; +type A12 = [/*c*/]; + +o = {}; +o = {/*c*/}; +type O21 = {}; +type O22 = {/*c*/}; +type O23 = {...}; +type O24 = {/*c*/ ...}; +type O31 = {||}; +type O32 = {|/*c*/|}; +// type O33 = {|...|}; +// type O34 = {|/*c*/ ...|}; +interface I21 {} +interface I22 { + /*c*/ +} + +================================================================================ +`; + +exports[`empty.js - {"bracketSpacing":true} format 1`] = ` +====================================options===================================== +bracketSpacing: true +parsers: ["flow"] + printWidth: 80 (default) | +=====================================input====================================== +a = []; +a = [/*c*/]; +type A11 = []; +type A12 = [/*c*/]; + +o = {}; +o = {/*c*/}; +type O21 = {}; +type O22 = {/*c*/}; +type O23 = {...}; +type O24 = {/*c*/ ...}; +type O31 = {||}; +type O32 = {|/*c*/|}; +// type O33 = {|...|}; +// type O34 = {|/*c*/ ...|}; +interface I21 {} +interface I22 {/*c*/} + +=====================================output===================================== +a = []; +a = [/*c*/]; +type A11 = []; +type A12 = [/*c*/]; + +o = {}; +o = {/*c*/}; +type O21 = {}; +type O22 = {/*c*/}; +type O23 = { ... }; +type O24 = { /*c*/ ... }; +type O31 = {||}; +type O32 = {|/*c*/|}; +// type O33 = {|...|}; +// type O34 = {|/*c*/ ...|}; +interface I21 {} +interface I22 { + /*c*/ +} + +================================================================================ +`; diff --git a/tests/format/flow/object-type/empty/empty.js b/tests/format/flow/object-type/empty/empty.js new file mode 100644 index 000000000..2ebddd523 --- /dev/null +++ b/tests/format/flow/object-type/empty/empty.js @@ -0,0 +1,17 @@ +a = []; +a = [/*c*/]; +type A11 = []; +type A12 = [/*c*/]; + +o = {}; +o = {/*c*/}; +type O21 = {}; +type O22 = {/*c*/}; +type O23 = {...}; +type O24 = {/*c*/ ...}; +type O31 = {||}; +type O32 = {|/*c*/|}; +// type O33 = {|...|}; +// type O34 = {|/*c*/ ...|}; +interface I21 {} +interface I22 {/*c*/} diff --git a/tests/format/flow/object-type/empty/format.test.js b/tests/format/flow/object-type/empty/format.test.js new file mode 100644 index 000000000..fe5806c79 --- /dev/null +++ b/tests/format/flow/object-type/empty/format.test.js @@ -0,0 +1,2 @@ +runFormatTest(import.meta, ["flow"], { bracketSpacing: true }); +runFormatTest(import.meta, ["flow"], { bracketSpacing: false }); diff --git a/tests/format/typescript/class-and-interface/empty/__snapshots__/format.test.js.snap b/tests/format/typescript/class-and-interface/empty/__snapshots__/format.test.js.snap index e42bba8ce..7450176b2 100644 --- a/tests/format/typescript/class-and-interface/empty/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/class-and-interface/empty/__snapshots__/format.test.js.snap @@ -63,10 +63,10 @@ type T2 = { type T3 = { //comment }; -type T4 = { /*comment*/ }; -type T5 = { /*comment*/ }; -type T6 = { /*comment*/ }; -type T7 = { /*comment*/ }; +type T4 = {/*comment*/}; +type T5 = {/*comment*/}; +type T6 = {/*comment*/}; +type T7 = {/*comment*/}; ================================================================================ `; diff --git a/tests/format/typescript/object-type/empty/__snapshots__/format.test.js.snap b/tests/format/typescript/object-type/empty/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..6258985b1 --- /dev/null +++ b/tests/format/typescript/object-type/empty/__snapshots__/format.test.js.snap @@ -0,0 +1,73 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`empty.ts - {"bracketSpacing":false} format 1`] = ` +====================================options===================================== +bracketSpacing: false +parsers: ["typescript"] + printWidth: 80 (default) | +=====================================input====================================== +a = []; +a = [/*c*/]; +type A11 = []; +type A12 = [/*c*/]; + +o = {}; +o = {/*c*/}; +type O21 = {}; +type O22 = {/*c*/}; +interface I21 {} +interface I22 {/*c*/} + +=====================================output===================================== +a = []; +a = [/*c*/]; +type A11 = []; +type A12 = [/*c*/]; + +o = {}; +o = {/*c*/}; +type O21 = {}; +type O22 = {/*c*/}; +interface I21 {} +interface I22 { + /*c*/ +} + +================================================================================ +`; + +exports[`empty.ts - {"bracketSpacing":true} format 1`] = ` +====================================options===================================== +bracketSpacing: true +parsers: ["typescript"] + printWidth: 80 (default) | +=====================================input====================================== +a = []; +a = [/*c*/]; +type A11 = []; +type A12 = [/*c*/]; + +o = {}; +o = {/*c*/}; +type O21 = {}; +type O22 = {/*c*/}; +interface I21 {} +interface I22 {/*c*/} + +=====================================output===================================== +a = []; +a = [/*c*/]; +type A11 = []; +type A12 = [/*c*/]; + +o = {}; +o = {/*c*/}; +type O21 = {}; +type O22 = {/*c*/}; +interface I21 {} +interface I22 { + /*c*/ +} + +================================================================================ +`; diff --git a/tests/format/typescript/object-type/empty/empty.ts b/tests/format/typescript/object-type/empty/empty.ts new file mode 100644 index 000000000..3b07c2f46 --- /dev/null +++ b/tests/format/typescript/object-type/empty/empty.ts @@ -0,0 +1,11 @@ +a = []; +a = [/*c*/]; +type A11 = []; +type A12 = [/*c*/]; + +o = {}; +o = {/*c*/}; +type O21 = {}; +type O22 = {/*c*/}; +interface I21 {} +interface I22 {/*c*/} diff --git a/tests/format/typescript/object-type/empty/format.test.js b/tests/format/typescript/object-type/empty/format.test.js new file mode 100644 index 000000000..b81e6a929 --- /dev/null +++ b/tests/format/typescript/object-type/empty/format.test.js @@ -0,0 +1,2 @@ +runFormatTest(import.meta, ["typescript"], { bracketSpacing: true }); +runFormatTest(import.meta, ["typescript"], { bracketSpacing: false });