Skip to content

Leading comment in parenthesized return expression moves to incorrect place in member chain #18843

@Dunqing

Description

@Dunqing

Prettier 3.8.1
Playground link

--parser babel-ts

Input:

function memberInAndOutWithCalls() {
return (
// Reason for a
aFunction.b()
).c.d()
}

function memberInAndOutWithCalls() {
  return (
    // Reason for a
    aFunction.b()
  ).c.d()
}

Output:

function memberInAndOutWithCalls() {
  return (
    aFunction
      .b// Reason for a
      ()
      .c.d()
  );
}

Expected output:

function memberInAndOutWithCalls() {
  return (
    // Reason for a
    aFunction.b()
  ).c.d();
}

Why?

The comment // Reason for a was a leading comment inside parentheses, intended to explain the entire expression being returned. The output moves it to between .b and (), which loses the original intent of explaining "reason for a" (the whole expression)

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