Skip to content

Commit 2589c67

Browse files
committed
update best-practices config to prevent parameter object manipulation
added good/bad examples of parameter mutation to the readme
1 parent fcc41ee commit 2589c67

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,35 @@ Other Style Guides
629629
const y = function a() {};
630630
```
631631
632+
- [7.12](#7.12) <a name="7.12"></a> Never mutate parameters.
633+
634+
> 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.
635+
636+
eslint rules: [`no-param-reassign`](http://eslint.org/docs/rules/no-param-reassign.html).
637+
638+
```javascript
639+
// bad
640+
function f(a){
641+
a = 1;
642+
}
643+
function f(a){
644+
if (!a) { a = 1; }
645+
}
646+
function f(obj){
647+
obj.key = 1;
648+
};
649+
650+
// good
651+
function f(a){
652+
const b = (a || 1);
653+
}
654+
function f(a = 1){
655+
}
656+
function f(obj){
657+
const key = obj.hasOwnProperty('key') ? obj.key ? 1;
658+
};
659+
```
660+
632661
**[⬆ back to top](#table-of-contents)**
633662
634663
## Arrow Functions

packages/eslint-config-airbnb/rules/best-practices.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ module.exports = {
7474
// var foo = 'Copyright \251';
7575
'no-octal-escape': 2,
7676
// disallow reassignment of function parameters
77-
'no-param-reassign': 2,
77+
// disallow parameter object manipulation
78+
'no-param-reassign': [2, { 'props': true }],
7879
// disallow use of process.env
7980
'no-process-env': 0,
8081
// disallow usage of __proto__ property

0 commit comments

Comments
 (0)