Rule proposal: disallow chained variable assignment
See original GitHub issueWhen the rules will warn. Include a description as well as sample code.
Chained variable assignment creates implicit global variables. Although these should be caught by the no-undef
rule, people may want to prevent chained variable assignment for readability purposes.
// Bad (this fails no-undef)
var a = b = c = 1;
// Bad (this passes no-undef)
var b;
var c;
var a = b = c = 1;
// Good
var a = 1;
var b = a;
var c = a;
Whether the rule prevents an error or is stylistic.
This rule prevents an error if the no-undef
rule is not enabled, otherwise it is stylistic and similar to the one-var
rule.
Why the rule should be in the core instead of creating a custom rule.
This is a basic JavaScript syntax rule that is generally applicable to anyone writing JavaScript.
Are you willing to create the rule yourself?
Sure.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Disallow assignments in conditions and remove the Yoda ...
Drop the handbook rule that requires Yoda conditions and instead explicitly disallow using them. Remove Yoda condition as a requirement.
Read more >no-multi-assign - Pluggable JavaScript Linter - ESLint
Chaining the assignment of variables can lead to unexpected results and be ... This rule disallows using multiple assignments within a single statement....
Read more >Proposed rule: Private Fund Advisers - SEC.gov
We propose to require registered investment advisers to private funds to provide transparency to their investors regarding the full cost of ...
Read more >how to break a long line of chained assignments in python
As a single statement, there are no subexpressions to parenthesize. I'm not a fan of explicit line continuations, but this is probably a ......
Read more >Assignment operators (C# reference) - Microsoft Learn
C# assignment operators assign an expression to a variable. Assignment sets the value of the expression. `ref` assignment sets the reference ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ljharb What you’re saying makes sense and is basically what I had been thinking in my head earlier. I just wanted to give the ESLint team a chance to jump in and let me know if I had missed something.
I’ll probably champion this, once we figure out how exactly we want to do it.
I think it makes the most sense as a new rule. one-var might work, but even when you don’t want to require or disallow multiple var statements, you may want to govern this pattern. no-undef also might work, but this is not saying “don’t use undefined/global vars”, it’s saying “don’t accidentally create some global vars with one local”.