sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-useless-assign`

Geschlossen

#1.693 geöffnet am 14.01.2022

 (3 Kommentare) (3 Reaktionen) (0 zugewiesene Personen)JavaScript (468 Forks)user submission
help wantednew rule

Repository-Metriken

Stars
 (5.022 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 4h 30m) (26 gemergte PRs in 30 T)

Beschreibung

Description

Got the idea from https://lgtm.com/projects/g/prettier/prettier/snapshot/dae9b0317dae184854caf66c44eec17a95200e98/files/src/language-js/parse/postprocess/typescript.js?sort=name&dir=ASC&mode=heatmap#L66

I don't think no-unused-vars rule should check this case, because the variable is actually "used" in some case, only should not assign when it's a last use.

Fail

function foo(bar) {
  bar = doSomething();
}
function foo(bar) {
  bar = doSomething(bar);
}
function foo(bar) {
  bar = doSomething(bar);

  // ... do something else, but not using bar
}

Pass

function foo(bar) {
  bar = doSomething(bar);

  return bar;
}
function foo(bar) {
  bar = doSomething(bar);

  use(bar);
}
function foo(bar) {
  function aInnerFunctionUsesBar() {
   return bar;
  }

  bar = doSomething(bar);

  // bar is not used but calling another function uses bar
  aInnerFunctionUsesBar()
}

Contributor Guide