prettier icon indicating copy to clipboard operation
prettier copied to clipboard

Leading comments of sequence expressions were placed incorrectly

Open Dunqing opened this issue 1 month ago • 6 comments

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:

  1. Place comments inside the sequence expression
func(
  () =>
    (
      // comment
      a, b, c
    ),
);

func(
  () =>
    (
      // comment
      a, b, c
    ),
);

(
  // comment
  a, b, c
)
  1. 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:

  1. The first one maintains consistency as long as the comments precede the first expression of the sequence.
  2. The second one keeps the comments in the same place as the input.

Dunqing avatar Oct 31 '25 08:10 Dunqing

Do you have a real world example?

fisker avatar Oct 31 '25 10:10 fisker

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

Dunqing avatar Oct 31 '25 12:10 Dunqing

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;

fisker avatar Oct 31 '25 12:10 fisker

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?

Dunqing avatar Oct 31 '25 12:10 Dunqing

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.

fisker avatar Oct 31 '25 12:10 fisker

We can't even tell what the () belongs to in this case.

  1. We can say it belongs to the arrow function body
  2. We can also say it belongs to the assignment/sequence expression

fisker avatar Oct 31 '25 13:10 fisker