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.95k stars 464 forks source link

💅 noUselessLoneBlockStatements collides with useSingleCaseStatement when using switch #834

Closed hougesen closed 11 months ago

hougesen commented 11 months ago

Environment information

CLI:
  Version:                      1.3.3
  Color support:                true

Platform:
  CPU Architecture:             x86_64
  OS:                           linux

Environment:
  BIOME_LOG_DIR:                unset
  NO_COLOR:                     unset
  TERM:                         "rio"
  JS_RUNTIME_VERSION:           "v18.18.0"
  JS_RUNTIME_NAME:              "node"
  NODE_PACKAGE_MANAGER:         "npm/9.8.1"

Biome Configuration:
  Status:                       Loaded successfully
  Formatter disabled:           true
  Linter disabled:              false
  Organize imports disabled:    false
  VCS disabled:                 true

Workspace:
  Open Documents:               0

Rule name

noUselessLoneBlockStatements

Playground link

https://biomejs.dev/playground/?indentStyle=space&quoteStyle=single&trailingComma=none&lintRules=all&code=cwB3AGkAdABjAGgAIAAoADEAKQAgAHsACgAgACAALwAvACAAdQBzAGUAUwBpAG4AZwBsAGUAQwBhAHMAZQBTAHQAYQB0AGUAbQBlAG4AdAAgAGkAcwAgAHQAcgBpAGcAZwBlAHIAZQBkAAoAIAAgAGQAZQBmAGEAdQBsAHQAOgAKACAAIAAgACAAYwBvAG4AcwBvAGwAZQAuAGkAbgBmAG8AKAAnADEAJwApADsACgAgACAAIAAgAGMAbwBuAHMAbwBsAGUALgBpAG4AZgBvACgAJwAyACcAKQA7AAoAIAAgACAAIABiAHIAZQBhAGsAOwAKAH0ACgAKAHMAdwBpAHQAYwBoACAAKAAxACkAIAB7AAoAIAAgAC8ALwAgAG4AbwBVAHMAZQBsAGUAcwBzAEwAbwBuAGUAQgBsAG8AYwBrAFMAdABhAHQAZQBtAGUAbgB0AHMAIABpAHMAIAB0AHIAaQBnAGcAZQByAGUAZAAKACAAIABkAGUAZgBhAHUAbAB0ADoAIAB7AAoAIAAgACAAIABjAG8AbgBzAG8AbABlAC4AaQBuAGYAbwAoACcAMQAnACkAOwAKACAAIAAgACAAYwBvAG4AcwBvAGwAZQAuAGkAbgBmAG8AKAAnADIAJwApADsACgAgACAAIAAgAGIAcgBlAGEAawA7AAoAIAAgAH0ACgB9AAoACgBzAHcAaQB0AGMAaAAgACgAMQApACAAewAKACAAIAAvAC8AIABuAG8AIAByAHUAbABlAHMAIABhAHIAZQAgAHQAcgBpAGcAZwBlAHIAZQBkAAoAIAAgAGQAZQBmAGEAdQBsAHQAOgAgAHsACgAgACAAIAAgAGMAbwBuAHMAdAAgAF8AIAA9ACAAJwAnADsACgAgACAAIAAgAGMAbwBuAHMAbwBsAGUALgBpAG4AZgBvACgAJwAxACcAKQA7AAoAIAAgACAAIABjAG8AbgBzAG8AbABlAC4AaQBuAGYAbwAoACcAMgAnACkAOwAKACAAIAAgACAAYgByAGUAYQBrADsACgAgACAAfQAKAH0ACgA%3D

Expected result

When both noUselessLoneBlockStatements and useSingleCaseStatement are turned on there will be an endless loop of "fixes" since they try to change the fix for each other.

The following code triggers the useSingleCaseStatement ("A switch clause should only have a single statement"):

switch (1) {
  // useSingleCaseStatement is triggered
  default:
    console.info("1");
    console.info("2");
    break;
}

When using the auto-fix the code is turned into the following:

switch (1) {
  // noUselessLoneBlockStatements is triggered
  default: {
    console.info("1");
    console.info("2");
    break;
  }
}

Which then triggers noUselessLoneBlockStatements. Using the auto-fix for noUselessLoneBlockStatementswill turn the code into the first code snippet (again).

The noUselessLoneBlockStatements rule works as expected when there is a variable declaration:

switch (1) {
  // no rules are triggered
  default: {
    const _ = "";
    console.info("1");
    console.info("2");
    break;
  }
}

Code of Conduct

vasucp1207 commented 11 months ago

Can I take this issue. Also which rule is better to address this problem, check condition in useSingleCaseStatement when there is no declaration in it or to check noUselessLoneBlockStatements.

Conaclos commented 11 months ago

Also which rule is better to address this problem, check condition in useSingleCaseStatement when there is no declaration in it or to check noUselessLoneBlockStatements.

Assigned :) noUselessLoneBlockStatements should ignore a block statement on a case.