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 1 commit
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 f(a){
Copy link
Collaborator

Choose a reason for hiding this comment

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

please change ){ to ) { throughout

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 f(a){
if (!a) { a = 1; }
}
function f(obj){
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is invalid syntax, and relying on hasOwnProperty to not be shadowed is unwise. Can we change this example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
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 }],
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