From 55d044c7c17d22fc0e4299a23481a72347c55cba Mon Sep 17 00:00:00 2001 From: scott stern Date: Sun, 14 Aug 2016 15:35:05 -0700 Subject: [PATCH] [eslint config] [base] [breaking] Add no-plusplus in style.js and added explanation in README. --- README.md | 29 +++++++++++++++++++ .../eslint-config-airbnb-base/rules/style.js | 3 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 728f3647..2ed3f9f1 100644 --- a/README.md +++ b/README.md @@ -1278,6 +1278,35 @@ Other Style Guides } ``` + - [13.5](#variables--unary-increment-decrement) Avoid using unary increments and decrements (++, --). eslint [`no-plusplus`](http://eslint.org/docs/rules/no-plusplus) + + > Why? Per the eslint documentation, unary increment and decrement statements are subject to automatic semicolon insertion and can cause silent errors with incrementing or decrementing values within an application. It is also more expressive to mutate your values with statements like `num += 1` instead of `num ++`. Disallowing unary increment and decrement statements also prevents you from pre-incrementing/pre-decrementing values unintentionally which can also cause unexpected behavior in your programs. + + ```javascript + // bad + + let array = [1, 2, 3]; + let num = 1; + let increment = num ++; + let decrement = -- num; + + for(let i = 0; i < array.length; i++){ + let value = array[i]; + ++value; + } + + // good + + let array = [1, 2, 3]; + let num = 1; + let increment = num += 1; + let decrement = num -= 1; + + array.forEach((value) => { + value += 1; + }); + ``` + **[⬆ back to top](#table-of-contents)** diff --git a/packages/eslint-config-airbnb-base/rules/style.js b/packages/eslint-config-airbnb-base/rules/style.js index f4fb7dbd..faac007c 100644 --- a/packages/eslint-config-airbnb-base/rules/style.js +++ b/packages/eslint-config-airbnb-base/rules/style.js @@ -180,7 +180,8 @@ module.exports = { 'no-new-object': 'error', // disallow use of unary operators, ++ and -- - 'no-plusplus': 'off', + // http://eslint.org/docs/rules/no-plusplus + 'no-plusplus': 2, // disallow certain syntax forms // http://eslint.org/docs/rules/no-restricted-syntax