sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-else-if`

Fechada

#1.393 aberto em 1 de jul. de 2021

 (5 comentários) (1 reação) (0 responsável)JavaScript (468 forks)user submission
help wantednew rule

Métricas do repositório

Stars
 (5.022 estrelas)
Métricas de merge de PR
 (Mesclagem média 4h 30m) (26 fundiu PRs em 30d)

Description

Fail

if (foo === '1') {
  // ...
}
if (foo === '2') {
  // ...
}

Pass

function a() {
  if (foo === '1') {
    // ...
    return;
  }

  if (foo === '2') {
    // ...
    return;
  }
}
if (foo === '1') {
  // ...
} else if (foo === '2') {
  // ...
}
if (foo === '1') {
  ...
}
if (foo === '2') {
  ...
}
if (foo === '3') {
  ...
}
if (foo === '4') {
  ...
}

To fix this case, we can simply only check if foo is declared as const.

This come from my work, but real case is more complicated, it's like

if (foo.bar === '1') {
  ...
}
if (foo.bar === '2') {
  ...
}
if (foo.bar === '3') {
  ...
}
if (foo.bar === '4') {
  ...
}

To fix this case, we need to check foo.bar and foo didn't get changed in each if block, I feel this is hard (even impossible) to do.

Guia do colaborador