Skip to content
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

Merged
merged 3 commits into from
Dec 17, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 f1(a) {
a = 1;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new line after end of function.

function f2(a) {
if (!a) { a = 1; }
}
function f3(obj) {
obj.key = 1;
};

// good
function f4(a) {
const b = a || 1;
}
function f5(a = 1) {
}
function f6(obj) {
const key = Object.prototype.hasOwnProperty.call(obj, 'key') ? obj.key : 1;
};
```

**[⬆ back to top](#table-of-contents)**

## Arrow Functions
Expand Down
4 changes: 3 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,9 @@ module.exports = {
// var foo = 'Copyright \251';
'no-octal-escape': 2,
// disallow reassignment of function parameters
'no-param-reassign': 2,
// disallow parameter object manipulation
// rule: http://eslint.org/docs/rules/no-param-reassign.html
'no-param-reassign': [2, { 'props': true }],
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Down