Leading comments of sequence expressions were placed incorrectly
Prettier 3.6.2 Playground link
--parser acorn
Input:
func(() => // comment
(a , b , c))
func(
() =>
(
// comment
a, b, c
),
);
(
// comment
a, b, c
);
Output:
func(
() =>
// comment
(a, b, c),
);
func(
() =>
// comment
(a, b, c),
);
// comment
(a, b, c);
Expected output:
- Place comments inside the sequence expression
func(
() =>
(
// comment
a, b, c
),
);
func(
() =>
(
// comment
a, b, c
),
);
(
// comment
a, b, c
)
- Keep the comments place as-is
func(
() =>
// comment
(a, b, c),
);
func(
() =>
(
// comment
a, b, c
),
);
(
// comment
a, b, c
)
Why?
I think the comment, if the sequence expression wraps it, then the output should keep it in the same place. However, it was placed before the sequence expression anyway.
Not sure which output is better, here are my thoughts on the two different results:
- The first one maintains consistency as long as the comments precede the first expression of the sequence.
- The second one keeps the comments in the same place as the input.
Do you have a real world example?
Do you have a real world example?
No, I found it from Prettier's tests.
https://github.com/prettier/prettier/blob/011791fd6c8856fb92fafec95930e10383ac267b/tests/format/js/arrows/comment.js#L46-L51
That's not a real-world case... The sequence expressions are rarely seen.
Anyway, maybe an assignment can be a real thing. We can try to improve.
Prettier 3.6.2 Playground link
--parser acorn
Input:
func(() => // comment
(a = 1))
func(
() =>
(
// comment
a = 1
),
);
(
// comment
a = 1
);
Output:
func(
() =>
// comment
(a = 1),
);
func(
() =>
// comment
(a = 1),
);
// comment
a = 1;
That's not a real-world case... The sequence expressions are rarely seen.
Good to know! Oxfmt is aiming to pass all of Prettier's tests, and I saw that this output is a little weird, so I reported it here. This way, I can skip this case if it is indeed wrong.
Anyway, maybe an assignment can be a real thing. We can try to improve.
Prettier 3.6.2 Playground link
--parser acorn Input:
func(() => // comment (a = 1))
func( () => ( // comment a = 1 ), );
( // comment a = 1 ); Output:
func( () => // comment (a = 1), );
func( () => // comment (a = 1), );
// comment a = 1;
What is the expected output for this case, in your opinion?
Can't tell, both look fine to me, I guess people normally won't comment in such a place.
Maybe they will do this when they have to return an assignment
// Explanation for why returning an assignment
return (foo = bar())
So the comment above the assignment in the arrow function body does not really matters.
I can be wrong about this.
We can't even tell what the () belongs to in this case.
- We can say it belongs to the arrow function body
- We can also say it belongs to the assignment/sequence expression