Skip to content

Commit

Permalink
Flow: Print dangling comments for empty inexact object type (prettier…
Browse files Browse the repository at this point in the history
…#7892)

* Remove unnecessary group

* Print dangling comment for inexact type

* Add changelog

* Fix changelog
  • Loading branch information
sosukesuzuki authored Mar 31, 2020
1 parent 8a48ca2 commit 0f56b4a
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 1 deletion.
19 changes: 19 additions & 0 deletions changelog_unreleased/flow/pr-7892.md
Original file line number Diff line number Diff line change
@@ -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))

<!-- prettier-ignore -->
```js
// Input
type Foo = {
// comment
...,
};

// Prettier stable
Error: Comment "comment" was not printed. Please report this error!

// Prettier master
type Foo = {
// comment
...,
};
```
26 changes: 25 additions & 1 deletion src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
123 changes: 123 additions & 0 deletions tests/flow_object_inexact/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
54 changes: 54 additions & 0 deletions tests/flow_object_inexact/comments.js
Original file line number Diff line number Diff line change
@@ -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,
...
};

0 comments on commit 0f56b4a

Please sign in to comment.