Conversation
|
It's good to avoid the footgun of a falsy non-undefined value when using Separately, is there an eslint rule that would enforce this? |
|
Does this rule not handle this case? |
|
@goatslacker that talks about reusing function argument names, which deopts in v8 and hurts readability, but this PR seems to be talking about use of |
1ee18bd to
dd90d55
Compare
|
Thanks for feedback @ljharb @goatslacker.
That's true,
Rule 7.7 mentions that using |
README.md
Outdated
There was a problem hiding this comment.
this is a syntax error
There was a problem hiding this comment.
My bad, sorry. I'm correcting it.
82aaf0d to
30ec484
Compare
Defaulting omitted variables using `||` is like using `==` instead of
`===`. If a function's argument is falsy (`0`, `false`, `null`,
`''`...), it will be defaulted while it shouldn't be.
Example:
```javascript
// bad
function f(arg, optionalArg, opts = {}) {
const message = optionalArg || 'not set'; // what if arg === ''?
const rate = opts.rate || 0.1; // what if arg === 0.0?
}
// bad
const useUnicode = ENV['USE_UTF8'] || true; // what if arg === false?
// good
function f(arg, optionalArg = 'not set', opts = {}) {
const rate = opts.rate !== undefined ? opts.rate : 0.1;
}
// good
const useUnicode = ENV['USE_UTF8'] !== undefined : ENV['USE_UTF8'] || true;
```
More reading:
http://www.codereadability.com/javascript-default-parameters-with-or-operator/
30ec484 to
81c7c53
Compare
I did not see any rule like that. It would be hard to enforce, since |
|
It seems like an eslint rule could look for assignments where the right hand side contained a |
|
@ljharb then we have to tell if the values involved in the right-hand side are nullable or something -- it is possible to have this sort of check with Flow or some other null-aware type system but I don't think you could write a heuristic in ESLint that did anything but cause sadness. eg |
|
@justjake the idea would be to always disallow |
Actually what I proposed was less strict: disallow
I agree. |
|
@adrienverge that means there's no way we could enforce a lint rule then, because there's no way to tell at compile time that |
|
@ljharb Absolutely. But in my opinion, not being able to enforce it does not mean we shouldn't recommend it. |
|
#188 generalizes this to include |
Defaulting omitted variables using
||is like using==instead of===. If a function's argument is falsy (0,false,null,''...), it will be defaulted while it shouldn't be.Example:
More reading: http://www.codereadability.com/javascript-default-parameters-with-or-operator/