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
13.65k stars 415 forks source link

🐛 `biome-ignore` comments are detached by the formatter #2962

Open Conaclos opened 3 months ago

Conaclos commented 3 months ago

Environment information

Playground

What happened?

Originally posted here: https://github.com/rome/tools/issues/3734

Consider the following code:

export function supported(): boolean {
  return !!(
    // biome-ignore lint/complexity/useOptionalChain: optional chaining creates more complicated ES2019 code
    navigator.credentials &&
    navigator.credentials.create &&
    navigator.credentials.get &&
    window.PublicKeyCredential
  );
}

The biome-ignore comment works as intended.

However, as the playground shows, the formatter introduces parentheses around the && logic:

export function supported(): boolean {
    return !!(
        // biome-ignore lint/complexity/useOptionalChain: optional chaining creates more complicated ES2019 code
        (
            navigator.credentials &&
            navigator.credentials.create &&
            navigator.credentials.get &&
            window.PublicKeyCredential
        )
    );
}

This disconnects the biome-ignore comment, which in turn causes the linter to error again.

Expected result

The suppression comment should be taken into account after the code is formatted.

Code of Conduct

ematipico commented 3 months ago

Yeah, that's a tough one. Maybe the formatter shouldn't move suppression comments.

Conaclos commented 3 months ago

Yeah, that's a tough one. Maybe the formatter shouldn't move suppression comments.

Another possibility is to allow suppression comments to cover a parent node. This could also allow users to suppress a rule at a function level for example. However, I am not sure if it is easily doable with the current implementation of suppression comments.