-
-
Notifications
You must be signed in to change notification settings - Fork 353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prefer default parameters #29
Comments
Too bad I can't use default parameters in reusable modules in the near future... |
The bad and the good examples are pretty different actually, as default parameters only apply when |
@IssueHunt has funded $60.00 to this issue.
|
It would be nice if this rule had an "always" or "never" configuration option. |
@MrHen Why would you want |
I've stopped using default parameters entirely because of function foo({ bar = 123 } = {}) {
console.log(bar);
}
foo(null); // TypeError: Cannot destructure property `bar` of 'undefined' or 'null'. function foo(foo) {
const { bar = 123 } = (foo || {});
console.log(bar);
}
foo(null); // 123 It's also super confusing using defaults with destructuring because there are different ways to do it with subtle differences: function foo2({ bar } = { bar: 123 }) {
console.log(bar);
}
foo2(); // 123
foo2({}); // undefined
function foo3({ bar = 123 } = {}) {
console.log(bar);
}
foo3(); // 123
foo3({}); // 123
function foo4({ bar = 123 } = { bar: 456 }) {
console.log(bar);
}
foo4(); // 456
foo4({}); // 123 I guess what I really care about is not using destructuring in function parameter defaults. Maybe that should just be a different rule. |
There's a solution to that: I have completely stopped using
I would argue the only correct way to do it is this variant
Or maybe enforce only using |
Sure, but from a defensive programming standpoint, I'd still rather not blow up on
If the intent is to provide a default value for
I guess for the scope of this issue / rule, which of these statements are true / approved?
In the end, I would personally prefer not to mess with default parameters at all. I think the code is more readable and less error prone without them. Thus, having a "never" option to invert the check. For a real world example, I've recently had to go through a bunch of JS React code fixing
A lint rule to catch this would have been handy. |
Ugh, I copied the wrong code... I meant to copy |
I just realized that #208 is also open and has a similar discussion. |
Alright, between the two tickets covering this area, here is my recommendation:
|
The let a = "outside";
function f(b) {
let a = "inside";
b = b || a;
return b;
}
f() // inside The fixed code let a = "outside";
function f(b = a) {
let a = "inside";
return b;
}
f() // outside would change the semantics and print "outside" when evaluating I suggest we limit the rule and fixer capacity that we only fix those with |
I would argue blowing up on
I don't really want this. Default parameters are not going away. I don't think it makes sense to fight it. I would prefer the rule to be named
👍 I agree with the Allow/Disallow's here. |
I agree with not fixing such cases, but why can we not report? The user could still rework it to work as a default parameter. |
Should we close the other issue then? Personally, I would use |
@MrHen this doesn't seem to be valid ES: function fn (foo: { bar = 123 } = {}) { }
// SyntaxError: missing ) after formal parameters |
@sindresorhus has rewarded $54.00 to @medusalix. See it on IssueHunt
|
Applies only to
es6
mode:Bad:
Good:
IssueHunt Summary
medusalix has been rewarded.
Backers (Total: $60.00)
Submitted pull Requests
prefer-default-parameters
ruleTips
The text was updated successfully, but these errors were encountered: