From 0f56b4aa62bb2c199f58a179fb06db0e9b5ac1fa Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Tue, 31 Mar 2020 20:26:16 +0900 Subject: [PATCH] Flow: Print dangling comments for empty inexact object type (#7892) * Remove unnecessary group * Print dangling comment for inexact type * Add changelog * Fix changelog --- changelog_unreleased/flow/pr-7892.md | 19 +++ src/language-js/printer-estree.js | 26 +++- .../__snapshots__/jsfmt.spec.js.snap | 123 ++++++++++++++++++ tests/flow_object_inexact/comments.js | 54 ++++++++ 4 files changed, 221 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/flow/pr-7892.md create mode 100644 tests/flow_object_inexact/comments.js diff --git a/changelog_unreleased/flow/pr-7892.md b/changelog_unreleased/flow/pr-7892.md new file mode 100644 index 000000000..b03de6750 --- /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 0468af2bb..2b136512a 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 a5b6abe8a..fa0d6af61 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 000000000..17ebf492e --- /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, + ... +};