-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Rule suggestion: Don't reassign function parameters #1599
Comments
Sounds good to me. New rules are fairly low priority right now, so this would probably have to come from an outside contribution. |
@nzakas Should this rule be combined with |
Good question. My initial reaction is to keep it separate. |
This wouldn't be able to protect you entirely from re-assigning a parameter due to the relationship between |
I think that's fine. Arguably assigning to |
Agreed. It's just unfortunate. Maybe this rule could check for both? |
If it checks for both, then we probably need a way to specify each option individually. I sometimes reassign to named parameters but never to |
Then turn the rule off entirely. If you never mutate |
That's not acceptable. If a rule checks two different patterns, we can't force people to choose all or nothing. Either we have two rules here or we have one with options. Or we just go with one rule for the original suggestion. |
I like the idea to check that The behavior of this rule is already mentioned in the webstorm rollup (#667):
So we should also check for increment, decrement or other modifying operation. |
Agreed on all counts. |
Completely agree on all counts. They should be separate rules and they are both code smells, IMHO. |
I'm working on this, should have a PR in the next day or two. |
Awesome! |
What about things like: function myFunc(boo) {
// basic variations of default parameters
boo = boo || 13;
boo = boo === undefined ? 13 : boo;
boo = typeof boo === 'undefined' ? 13 : boo;
// defaults for an options object
Object.assign(boo, {
value: 13
});
} |
@xjamundx I would consider your examples as a code smell and would prefer the rule to warn on such patterns. If you want to reassign function parameters, you can simple disable this rule. |
I'd just suggest if that's intentional to specifically test and document that the default parameters style is considered bad. I think for many of us, it might be a surprise, though not necessarily worth carving out an exception for. In ES6 environments the answer will just be to use proper default parameters anyway. |
@lo1tuma - Yep. The exact purpose of this rule is to point out that this is a code smell. |
@xjamundx many do consider that as bad. This is why rules are configurable, if you disagree, you can turn it off. :) |
New: no-param-reassign rule (fixes #1599)
Guys, but what about .map? Lint saying this is bad, when I use reassignment in .map |
Why would you ever want to do reassignment in |
You mean when I'm doing like this:
It is considered bad practice? Old surveyWithIds.questions is gone anyway :/ |
Yeah so for example, what about |
@xjamundx I suggest you to fix your example by adding additional curly brackets and return statement, because short function syntax won't work that way. Yep, that's actually the stuff. Thanks! |
@idchlife this is valid ES6 |
@xjamundx oh, I see. I did not know about () around {} when returning object in short function. That's actually just flowtype, not typescript. |
I agree with the previous comments here that:
I'm currently evaluating no-param-reassign for a fairly large code base, because it's easy to perpetuate a pattern of mutating a single large argument blob over time, but it makes the code quite difficult to read and debug. The rule matches over 400 times, 90% of which could be considered extraneous to the core logic (stuff like assigning things to the global object, or to a parameter called scope (this is an Angular app). In practice that means it is difficult to employ this rule because those few arguments are "OK" to modify for structural reasons which would be difficult to refactor. I'd like to propose adding another parameter to this rule for excluding specific property paths.
|
Can we please create a new issue for enhancement requests to this rule? Thanks! |
Reusing function parameters feels often like a code smell for me. If you consider something like the following example it could even cost you some debugging time (especially if you have long legacy functions):
I suggest the implementation of a rule that is similar to no-ex-assign but for functions instead of try-catch-blocks and is turned off by default.
What do you think about it?
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: