From 2589c67b0c392a118aba3e3938856ddfead26baa Mon Sep 17 00:00:00 2001 From: Thomas Shafer Date: Wed, 16 Dec 2015 15:40:26 -0800 Subject: [PATCH] update best-practices config to prevent parameter object manipulation added good/bad examples of parameter mutation to the readme --- README.md | 29 +++++++++++++++++++ .../rules/best-practices.js | 3 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b860dae56..f64fe911a0 100644 --- a/README.md +++ b/README.md @@ -629,6 +629,35 @@ Other Style Guides const y = function a() {}; ``` +- [7.12](#7.12) 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 diff --git a/packages/eslint-config-airbnb/rules/best-practices.js b/packages/eslint-config-airbnb/rules/best-practices.js index 9d8199eb77..e511f9b15e 100644 --- a/packages/eslint-config-airbnb/rules/best-practices.js +++ b/packages/eslint-config-airbnb/rules/best-practices.js @@ -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