diff --git a/changelog_unreleased/flow/pr-7892.md b/changelog_unreleased/flow/pr-7892.md new file mode 100644 index 000000000000..b03de6750668 --- /dev/null +++ b/changelog_unreleased/flow/pr-7892.md @@ -0,0 +1,19 @@ +#### Print dangling comments for inexact object type ([#7892](https://github.com/prettier/prettier/pull/7892) by [@sosukesuzuki](https://github.com/sosukesuzuki)) + + +```js +// Input +type Foo = { + // comment + ..., +}; + +// Prettier stable +Error: Comment "comment" was not printed. Please report this error! + +// Prettier master +type Foo = { + // comment + ..., +}; +``` diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 0468af2bba66..2b136512a46f 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -1386,7 +1386,31 @@ function printPathNoParens(path, options, print, args) { }); if (n.inexact) { - props.push(concat(separatorParts.concat(group("...")))); + let printed; + if (hasDanglingComments(n)) { + const hasLineComments = !n.comments.every( + handleComments.isBlockComment + ); + const printedDanglingComments = comments.printDanglingComments( + path, + options, + /* sameIndent */ true + ); + printed = concat([ + printedDanglingComments, + hasLineComments || + hasNewline( + options.originalText, + options.locEnd(n.comments[n.comments.length - 1]) + ) + ? hardline + : line, + "...", + ]); + } else { + printed = "..."; + } + props.push(concat(separatorParts.concat(printed))); } const lastElem = getLast(n[propertiesField]); diff --git a/tests/flow_object_inexact/__snapshots__/jsfmt.spec.js.snap b/tests/flow_object_inexact/__snapshots__/jsfmt.spec.js.snap index a5b6abe8aca2..fa0d6af61b93 100644 --- a/tests/flow_object_inexact/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow_object_inexact/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,128 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`comments.js 1`] = ` +====================================options===================================== +parsers: ["flow", "babel"] +printWidth: 80 +trailingComma: "none" + | printWidth +=====================================input====================================== +// @flow + +type Foo = { + // comment + ..., +}; + +type Foo = { + /* comment */ + ..., +}; + +type Foo = { /* comment */ ... }; + +type Foo = { /* comment */ + ...}; + +type Foo = { + // comment0 + // comment1 + ..., +}; + +type Foo = { + /* comment0 */ + /* comment1 */ + ..., +}; + +type Foo = { + // comment + foo: string, + ... +}; + +type Foo = { + // comment0 + // comment1 + foo: string, + ... +}; + +type Foo = { + /* comment */ + foo: string, + ... +}; + +type Foo = { + /* comment0 */ + /* comment1 */ + foo: string, + ... +}; + +=====================================output===================================== +// @flow + +type Foo = { + // comment + ... +}; + +type Foo = { + /* comment */ + ... +}; + +type Foo = { /* comment */ ... }; + +type Foo = { + /* comment */ + ... +}; + +type Foo = { + // comment0 + // comment1 + ... +}; + +type Foo = { + /* comment0 */ + /* comment1 */ + ... +}; + +type Foo = { + // comment + foo: string, + ... +}; + +type Foo = { + // comment0 + // comment1 + foo: string, + ... +}; + +type Foo = { + /* comment */ + foo: string, + ... +}; + +type Foo = { + /* comment0 */ + /* comment1 */ + foo: string, + ... +}; + +================================================================================ +`; + exports[`test.js 1`] = ` ====================================options===================================== parsers: ["flow", "babel"] diff --git a/tests/flow_object_inexact/comments.js b/tests/flow_object_inexact/comments.js new file mode 100644 index 000000000000..17ebf492e322 --- /dev/null +++ b/tests/flow_object_inexact/comments.js @@ -0,0 +1,54 @@ +// @flow + +type Foo = { + // comment + ..., +}; + +type Foo = { + /* comment */ + ..., +}; + +type Foo = { /* comment */ ... }; + +type Foo = { /* comment */ + ...}; + +type Foo = { + // comment0 + // comment1 + ..., +}; + +type Foo = { + /* comment0 */ + /* comment1 */ + ..., +}; + +type Foo = { + // comment + foo: string, + ... +}; + +type Foo = { + // comment0 + // comment1 + foo: string, + ... +}; + +type Foo = { + /* comment */ + foo: string, + ... +}; + +type Foo = { + /* comment0 */ + /* comment1 */ + foo: string, + ... +};