-
Notifications
You must be signed in to change notification settings - Fork 26.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
update best-practices config to prevent parameter object manipulation #627
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -629,6 +629,35 @@ Other Style Guides | |
const y = function a() {}; | ||
``` | ||
|
||
- [7.12](#7.12) <a name="7.12"></a> Never mutate parameters. | ||
|
||
> Why? Overwriting parameters can lead to unexpected behavior, especially when accessing the `arguments` object. Manipulating objects passed in as parameters can cause unwanted variable side effects in the original caller. | ||
|
||
eslint rules: [`no-param-reassign`](http://eslint.org/docs/rules/no-param-reassign.html). | ||
|
||
```javascript | ||
// bad | ||
function f(a){ | ||
a = 1; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add new line after end of function. |
||
function f(a){ | ||
if (!a) { a = 1; } | ||
} | ||
function f(obj){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i can tell these are meant to be 3 separate examples, but to avoid looking like we're redeclaring functions, let's use different names for them |
||
obj.key = 1; | ||
}; | ||
|
||
// good | ||
function f(a){ | ||
const b = (a || 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's remove these redundant parens as well |
||
} | ||
function f(a = 1){ | ||
} | ||
function f(obj){ | ||
const key = obj.hasOwnProperty('key') ? obj.key ? 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is invalid syntax, and relying on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. Oh yeah the ternary is not valid too. Thanks for finding this. Guess I should execute the code before sending it over. |
||
}; | ||
``` | ||
|
||
**[⬆ back to top](#table-of-contents)** | ||
|
||
## Arrow Functions | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,8 @@ module.exports = { | |
// var foo = 'Copyright \251'; | ||
'no-octal-escape': 2, | ||
// disallow reassignment of function parameters | ||
'no-param-reassign': 2, | ||
// disallow parameter object manipulation | ||
'no-param-reassign': [2, { 'props': true }], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please also add a link here, in a comment, to the rule docs |
||
// disallow use of process.env | ||
'no-process-env': 0, | ||
// disallow usage of __proto__ property | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please change
){
to) {
throughout