From 2589c67b0c392a118aba3e3938856ddfead26baa Mon Sep 17 00:00:00 2001 From: Thomas Shafer Date: Wed, 16 Dec 2015 15:40:26 -0800 Subject: [PATCH 1/3] 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 2b860dae..f64fe911 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 9d8199eb..e511f9b1 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 From f7971301b08d8fc870a127167804059ab2b7c71b Mon Sep 17 00:00:00 2001 From: Thomas Shafer Date: Wed, 16 Dec 2015 20:21:38 -0800 Subject: [PATCH 2/3] update examples for style add link to no-param-reassign rule documentation --- README.md | 16 ++++++++-------- .../eslint-config-airbnb/rules/best-practices.js | 1 + 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f64fe911..ba2bcd0a 100644 --- a/README.md +++ b/README.md @@ -637,24 +637,24 @@ Other Style Guides ```javascript // bad - function f(a){ + function f1(a) { a = 1; } - function f(a){ + function f2(a) { if (!a) { a = 1; } } - function f(obj){ + function f3(obj) { obj.key = 1; }; // good - function f(a){ - const b = (a || 1); + function f4(a) { + const b = a || 1; } - function f(a = 1){ + function f5(a = 1) { } - function f(obj){ - const key = obj.hasOwnProperty('key') ? obj.key ? 1; + function f6(obj) { + const key = Object.prototype.hasOwnProperty.call(obj, 'key') ? obj.key : 1; }; ``` diff --git a/packages/eslint-config-airbnb/rules/best-practices.js b/packages/eslint-config-airbnb/rules/best-practices.js index e511f9b1..82e5f9e2 100644 --- a/packages/eslint-config-airbnb/rules/best-practices.js +++ b/packages/eslint-config-airbnb/rules/best-practices.js @@ -75,6 +75,7 @@ module.exports = { 'no-octal-escape': 2, // disallow reassignment of function parameters // disallow parameter object manipulation + // rule: http://eslint.org/docs/rules/no-param-reassign.html 'no-param-reassign': [2, { 'props': true }], // disallow use of process.env 'no-process-env': 0, From a533a4fb93c6f1385c2e3ede07a922df6a50dc6a Mon Sep 17 00:00:00 2001 From: Thomas Shafer Date: Thu, 17 Dec 2015 11:18:36 -0800 Subject: [PATCH 3/3] added newlines after each function --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index ba2bcd0a..98b0bee4 100644 --- a/README.md +++ b/README.md @@ -640,9 +640,11 @@ Other Style Guides function f1(a) { a = 1; } + function f2(a) { if (!a) { a = 1; } } + function f3(obj) { obj.key = 1; }; @@ -651,8 +653,10 @@ Other Style Guides 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; };