question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New rule proposal: no-unassigned-vars

See original GitHub issue

Please describe what the rule should do:

Reports var and let variables that are never assigned a value. In other words, variables that have zero write references.

What category of rule is this? (place an “X” next to just one item)

[X] Warns about a potential error (problem)

Provide 2-3 code examples that this rule will warn about:

var obj;
obj.x = 1;
obj.y = 2;

let a, b, c;
if (something) {
    a = 0;
    b = false;
} else {
    a = 1;
    b = true;
}
f(a, b, c);

Why should this rule be included in ESLint (instead of a plugin)?

This rule looks like a nice addition to the Variables category of rules, a complement to no-unused-vars to detect possible errors in the code.

An unassigned variable is a possible error because it always has undefined value. Even if that is the intention, it’s more clear to simply inline undefined instead of reading from the variable, or to make the intention explicit with const foo = undefined instead of just let foo which looks more like an error in the code where it’s never assigned a value.

no-undef-init does not disallow const.

prefer-const does not report these cases.

To avoid overlapping with no-unused-vars, this rule would report only variables that have at least 1 read reference.

Are you willing to submit a pull request to implement this rule?

Yes

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
mysticateacommented, Oct 29, 2019

In that case, I think that explicit assignment (c = undefined) is a better choice for maintainability.

1reaction
mdjermanoviccommented, May 29, 2020

I’m willing to work on this, for sure! On the other hand, I believe that this still needs a lot of additional analysis & design, and would end up being a fairly complex rule, so I definitely wouldn’t mind closing this in regard to the reasons for the new rules policy.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SEC Proposed Rules
SEC Proposed Rules · Regulation NMS: Minimum Pricing Increments, Access Fees, and Transparency of Better Priced Orders · File No: S7-30-22 · Comments...
Read more >
Proposed Rules
Administration (FDA or Agency) is proposing to amend its regulations to modernize, simplify, and enhance the current system for oversight of ...
Read more >
SEC Proposes New Rule Relating to Outsourcing of ...
[1] The New Proposed Rule does not distinguish between third-party providers and affiliated service providers (other than supervised persons) ...
Read more >
Simple Yet Efficient Improvements of SAT Based Bounded ...
if (!decide()) // if no unassigned vars ... The experiments we present here were conducted to offer a better ... get the best...
Read more >
SEC Proposes New Rules on Adviser Oversight of Service ...
On October 26, 2022, the SEC proposed, by a vote of 3-2, a new rule (“Proposed Rule”) that, if adopted, would require SEC-registered ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found