biomejs / biome

A toolchain for web projects, aimed to provide functionalities to maintain them. Biome offers formatter and linter, usable via CLI and LSP.
https://biomejs.dev
Apache License 2.0
14.71k stars 458 forks source link

📝 Line comment for `case:` statement is dissociated by unstable formatting #2786

Open lgarron opened 5 months ago

lgarron commented 5 months ago

Environment information

Reproduces in playground (presumably v1.7.3).

Configuration

Default config.

Playground link

https://biomejs.dev/playground/?code=ZgB1AG4AYwB0AGkAbwBuACAAYwBvAG8AbAAoAHgAKQAgAHsACgAgACAAcwB3AGkAdABjAGgAIAAoAHgAKQAgAHsACgAgACAAIAAgAGMAYQBzAGUAIAA0ADoAIAAvAC8AIABnAHUAYQByAGEAbgB0AGUAZQBkACAAdABvACAAYgBlACAAcgBhAG4AZABvAG0ACgAgACAAIAAgAGMAYQBzAGUAIAA0ADIAOgAgAC8ALwAgAGMAbABhAHMAcwBpAGMACgAgACAAIAAgAGMAYQBzAGUAIAAxADMAMwA3ADoAIAAvAC8AIABFAEwASQBUAEUACgAgACAAIAAgAHsACgAgACAAIAAgACAAIABjAG8AbgBzAG8AbABlAC4AbABvAGcAKAAiAHgAIABpAHMAIABjAG8AbwBsACIAKQA7AAoAIAAgACAAIAAgACAAYgByAGUAYQBrADsACgAgACAAIAAgAH0ACgAgACAAIAAgAGQAZQBmAGEAdQBsAHQAOgAgAHsACgAgACAAIAAgACAAIABjAG8AbgBzAG8AbABlAC4AZQByAHIAbwByACgAIgB4ACAAaQBzACAAbgBvAHQAIABjAG8AbwBsACIAKQA7AAoAIAAgACAAIAB9AAoAIAAgAH0ACgB9AA%3D%3D

function cool(x) {
  switch (x) {
    case 4: // guaranteed to be random
    case 42: // classic
    case 1337: // ELITE
    {
      console.log("x is cool");
      break;
    }
    default: {
      console.error("x is not cool");
    }
  }
}

… is reformatted into:

// …
        case 42: // classic
        case 1337: { // ELITE
            console.log("x is cool");
// …

However, this is not stable. Running the formatter again produces:

// …
        case 42: // classic
        case 1337: {
            // ELITE
            console.log("x is cool");

// …

The comment now appears to refer to the contents of the case block rather than the case statement. I would suggest that the original formatting is preserved, else it becomes impossible to annotate the final case statement in a situation like this.

Code of Conduct

Conaclos commented 5 months ago

Biome has the same behavior as Prettier here, however I accept that it is an undesirable behavior since we got a formatting instability. Thanks for reporting this!