Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💅 noUselessElse recommendation is wrong #1191

Closed
1 task done
anonrig opened this issue Dec 14, 2023 · 9 comments · Fixed by #1219
Closed
1 task done

💅 noUselessElse recommendation is wrong #1191

anonrig opened this issue Dec 14, 2023 · 9 comments · Fixed by #1219
Assignees
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Bug-confirmed Status: report has been confirmed as a valid bug

Comments

@anonrig
Copy link
Contributor

anonrig commented Dec 14, 2023

Environment information

CLI:
  Version:                      1.4.0
  Color support:                true

Platform:
  CPU Architecture:             aarch64
  OS:                           macos

Environment:
  BIOME_LOG_DIR:                unset
  NO_COLOR:                     unset
  TERM:                         "alacritty"
  JS_RUNTIME_VERSION:           "v20.10.0"
  JS_RUNTIME_NAME:              "node"
  NODE_PACKAGE_MANAGER:         "npm/10.2.3"

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

Workspace:
  Open Documents:               0

Rule name

noUselessElse

Playground link

https://github.com/getsentry/sentry-javascript/blob/develop/rollup/plugins/extractPolyfillsPlugin.js

Expected result

The following file has invalid "FIXABLE" output:

https://github.com/getsentry/sentry-javascript/blob/develop/rollup/plugins/extractPolyfillsPlugin.js

./rollup/plugins/extractPolyfillsPlugin.js:121:5 lint/style/noUselessElse  FIXABLE  ━━━━━━━━━━━━━━━━

  ✖ This else clause can be omitted.

    119 │     // or
    120 │     //   `const { dogs } = { dogs: () => "are great" }`
  > 121 │     else if (declarationId.type === 'ObjectPattern') {
        │     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  > 122 │       return declarationId.properties[0].key.name;
         ...
  > 126 │       return 'unknown variable';
  > 127 │     }
        │     ^
    128 │   }
    129 │

  ℹ This if statement uses an early breaking statement.

    112 │     // or
    113 │     //   `const dogs = () => "are great";
  > 114 │     if (declarationId.type === 'Identifier') {
        │     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  > 115 │       return declarationId.name;
         ...
  > 126 │       return 'unknown variable';
  > 127 │     }
        │     ^
    128 │   }
    129 │

  ℹ Unsafe fix: Omit the else clause.

    114 114 │       if (declarationId.type === 'Identifier') {
    115 115 │         return declarationId.name;
    116     │ - ····}
    117     │ - ····//·Declarations·of·the·form
    118     │ - ····//···`const·{·dogs·}·=·{·dogs:·function()·{·return·"are·great";·}·}`
    119     │ - ····//·or
    120     │ - ····//···`const·{·dogs·}·=·{·dogs:·()·=>·"are·great"·}`
    121     │ - ····else·if·(declarationId.type·===·'ObjectPattern')·{
        116 │ + ····}if·(declarationId.type·===·'ObjectPattern')·{
    122 117 │         return declarationId.properties[0].key.name;
    123 118 │       }

Code of Conduct

  • I agree to follow Biome's Code of Conduct
@Conaclos
Copy link
Member

Hi @anonrig!

Can you be a more specific about the bug / the expected result? Is it the removal of the comments?

@anonrig
Copy link
Contributor Author

anonrig commented Dec 14, 2023

Hi @Conaclos, it removes the recommendations and also on line 116, it produces false JavaScript code:

}if (declarationId.type === 'ObjectPattern') {

@Conaclos
Copy link
Member

Conaclos commented Dec 14, 2023

recommendations

I don't see any issue with the emitted JavaScript (leaving apart the removed comments that should definitively be preserved). Do you mean it is not correctly formatted?

@Conaclos Conaclos self-assigned this Dec 14, 2023
@spd789562
Copy link

I'm facing the same issue, but end up I'm realizing it because the main if is using early return like this in your code

if (declarationId.type === 'Identifier') {
  return declarationId.name;
}

So next line dont need to use else if, because if it true, it never run rest of code.
It mean if there has other case, just need to use if

replace this

if (type === 'case A') {
  return 'A'
} else if (type === 'case B') {
  return 'B'
} else {
  return 'C'
}

to this

if (type === 'case A') {
  return 'A'
}
if (type === 'case B') {
  return 'B'
}
return 'C'

@Conaclos
Copy link
Member

@spd789562 Do you think the rules' diagnostics could be improved? Any suggestions?

@spd789562
Copy link

Not sure about it, I got this lint error when use VScode, and it just say

This else clause can be omitted.

But the problem is there has early breaking in that if statement, and some people tent to write else if even it's early breaking statement. So omit else if doesn't make sense at first glance.

#no-useless-else is giving the good example though.
And lint error also should not be too long, so I don't have good improve suggestion, sorry.

@ematipico
Copy link
Member

Yeah maybe we can make our message better and explain why the else can be omitted. Users shouldn't spend too much time understanding the reason of a fix (regardless of its nature)

@Conaclos
Copy link
Member

Conaclos commented Dec 15, 2023

We could add an explanation:

This else clause can be omitted because previous branches break early.

@ematipico
Copy link
Member

Sounds good to me!

@Conaclos Conaclos added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Bug-confirmed Status: report has been confirmed as a valid bug labels Dec 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Bug-confirmed Status: report has been confirmed as a valid bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants