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

linter: no-else-return false positive on wrong branch #6364

Closed
camchenry opened this issue Oct 8, 2024 · 1 comment
Closed

linter: no-else-return false positive on wrong branch #6364

camchenry opened this issue Oct 8, 2024 · 1 comment
Assignees
Labels
A-linter Area - Linter C-bug Category - Bug

Comments

@camchenry
Copy link
Collaborator

What version of Oxlint are you using?

0.9.10

What command did you run?

bunx oxlint@latest -D all -A no-unused-vars -A sort-keys -A no-null -A no-undefined -A explicit-function-return-type -A no-magic-numbers -A no-ternary -A no-explicit-any -A no-const-enum -A prefer-enum-initializers -A no-optional-chaining --ignore-pattern compliance

What does your .oxlint.json config file look like?

N/A

What happened?

While doing some linter testing on the https://github.com/angular/angular repository, I noticed that the no-else-return lint rule was correctly reporting an issue, but was reporting it for a line that is actually an else-if block.

Specifically, this code:

export const getPropType = (prop: unknown): PropType => {
  if (isSignal(prop)) {
    prop = prop();
  }

  if (prop === undefined) {
    return PropType.Undefined;
  }
  if (prop === null) {
    return PropType.Null;
  }
  if (prop instanceof HTMLElement) {
    return PropType.HTMLNode;
  }
  const type = typeof prop;
  if (type in commonTypes) {
    return commonTypes[type as keyof typeof commonTypes];
  }
  if (type === 'object') {
    if (Array.isArray(prop)) {
      return PropType.Array;
    } else if (prop instanceof Set) {
      return PropType.Set;
    } else if (prop instanceof Map) {
      return PropType.Map;
    } else if (Object.prototype.toString.call(prop) === '[object Date]') {
      return PropType.Date;
    } else if (prop instanceof Node) {
      return PropType.HTMLNode;
    } else {
      return PropType.Object;
    }
  }
  return PropType.Unknown;
};

is reported as:

× eslint(no-else-return): Unnecessary 'else' after 'return'.
    ╭─[devtools/projects/ng-devtools-backend/src/lib/state-serializer/prop-type.ts:48:7]
 47 │     if (Array.isArray(prop)) {
 48 │       return PropType.Array;
    ·       ───────────┬──────────
    ·                  ╰── This consequent block always returns,
 49 │     } else if (prop instanceof Set) {
    ·      ───┬──
    ·         ╰── Making this `else` block unnecessary.
 50 │       return PropType.Set;
    ╰────
  help: Remove the `else` block, moving its contents outside of the `if` statement.

I believe this is incorrect, because the following else is part of an if statement, so it doesn't matter if the prior if always returns. It should actually be reporting this code as the error, since all previous if statements return:

} else {
  return PropType.Object;
}
@camchenry camchenry added A-linter Area - Linter C-bug Category - Bug labels Oct 8, 2024
@camchenry
Copy link
Collaborator Author

Ah actually I didn't read the configuration on this rule closely enough, it looks like it already allows for allowElseIf: true which would fix this. Looks to be working as intended.

@camchenry camchenry closed this as not planned Won't fix, can't repro, duplicate, stale Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linter Area - Linter C-bug Category - Bug
Projects
None yet
Development

No branches or pull requests

2 participants