`no-plusplus` rule errors with valid JS code, requires more code as workaround
See original GitHub issueWhat rule do you want to change?
no-plusplus
Does this change cause the rule to produce more or fewer warnings? fewer, depends on approach taken to solve it
How will the change be implemented? (New option, new default behavior, etc.)? I’m here to suggest a change in a high-level, haven’t looked at the source code to suggest a solution implementation.
Please provide some example code that this change will affect:
let a = 1;
let b;
let c = b || a++;
What does the rule currently do for this code?
At the moment the above throws a violation of the no-plusplus
rule. The only workaround for satisfying the rule is to increment a
in a new statement to have the same effect, because:
let c = b || a+=1;
(which is the recommended solution from the docs) throws Uncaught ReferenceError: Invalid left-hand side in assignment
, and
let c = b || (a+1);
increments a
before assigning to c
, which isn’t the intention here.
What will the rule do after it’s changed? It should either allow valid statements like the above or only throw a warning instead of an error
Are you willing to submit a pull request to implement this change? Possibly, haven’t looked at the source code yet.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
I think advocates for the
no-plusplus
rule would argue that code likec = b || a++
is confusing to read. The code is “valid” in that it doesn’t cause a runtime error, but it’s the type of thing that (one might argue) you shouldn’t use. It seems like every piece of code reported byno-plusplus
is similarly “valid” but potentially confusing, so it might be better to simply disable theno-plusplus
rule if you want to use code like that.@platinumazure you’re right indeed, I don’t need to be using this rule. It’s just because I’m using Airbnb’s ESLint as the base, and therefore now need to ‘disable’ the rule as it’s enabled in the base.
Closing this issue.