Skip to content

Single line comments are merged on trailing trivia #9959

@OmarTawfik

Description

@OmarTawfik

Prettier 2.1.2
Playground link

--parser babel

Input:

function x() {
} // first
; // second

Output:

function x() {} // first // second

Expected behavior:

function x() {} // first
// second

Details:

As you can see, the two comments (that should be on their own lines) are merged on a single line. I think this has to do with the lineSuffix command being used on two tokens on the same line. When that happens, both the suffixes are appended to the same line.

This is obviously very dangerous, and destroys the semantic meaning of the comment. Not only it makes it much harder to read and reason about (since comments cannot be broken), it also means that it will break any tools/parsers that read the comment contents and expect certain strings to be there/not there, like ignores and other directives.

- First Second
Before first second
After first // second removed

This does not just affect JavaScript, but it also affects all other languages, as they have no other means AFAICT of fixing this correctly using other available commands.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:commentsIssues with how Prettier prints commentslang:javascriptIssues affecting JS

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions