Skip to content
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

Flow: Print dangling comments for empty inexact object type #7892

Merged
merged 4 commits into from Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions 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))

<!-- 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
Expand Up @@ -1382,7 +1382,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
@@ -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
@@ -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,
...
};