Skip to content

Commit

Permalink
update best-practices config to prevent parameter object manipulation
Browse files Browse the repository at this point in the history
added good/bad examples of parameter mutation to the readme
  • Loading branch information
trshafer committed Dec 16, 2015
1 parent fcc41ee commit 2589c67
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
function f(a){
if (!a) { a = 1; }
}
function f(obj){
obj.key = 1;
};

// good
function f(a){
const b = (a || 1);
}
function f(a = 1){
}
function f(obj){
const key = obj.hasOwnProperty('key') ? obj.key ? 1;
};
```
**[⬆ back to top](#table-of-contents)**
## Arrow Functions
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-config-airbnb/rules/best-practices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }],
// disallow use of process.env
'no-process-env': 0,
// disallow usage of __proto__ property
Expand Down

0 comments on commit 2589c67

Please sign in to comment.