Skip to content

Commit 88484ce

Browse files
committed
Merge pull request airbnb#627 from amplii/no-param-reassign-with-props
[eslint config] [breaking] update best-practices config to prevent parameter object manipulation
2 parents fcc41ee + a533a4f commit 88484ce

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,39 @@ 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 f1(a) {
641+
a = 1;
642+
}
643+
644+
function f2(a) {
645+
if (!a) { a = 1; }
646+
}
647+
648+
function f3(obj) {
649+
obj.key = 1;
650+
};
651+
652+
// good
653+
function f4(a) {
654+
const b = a || 1;
655+
}
656+
657+
function f5(a = 1) {
658+
}
659+
660+
function f6(obj) {
661+
const key = Object.prototype.hasOwnProperty.call(obj, 'key') ? obj.key : 1;
662+
};
663+
```
664+
632665
**[⬆ back to top](#table-of-contents)**
633666
634667
## Arrow Functions

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ 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+
// rule: http://eslint.org/docs/rules/no-param-reassign.html
79+
'no-param-reassign': [2, { 'props': true }],
7880
// disallow use of process.env
7981
'no-process-env': 0,
8082
// disallow usage of __proto__ property

0 commit comments

Comments
 (0)