diff --git a/README.md b/README.md index 134abe9e..4c304fcf 100644 --- a/README.md +++ b/README.md @@ -1943,6 +1943,38 @@ Other Style Guides const baz = !c; ``` + + - [15.7](#comparison--no-mixed-operators) Enclose operators in parentheses when they are mixed in a statement. When mixing arithmetic operators, do not mix `**` and `%` with themselves or with `+`, `-`, `*`, & `/`. eslint: [`no-mixed-operators`](http://eslint.org/docs/rules/no-mixed-operators.html) + + > Why? This improves readability and clarifies the developer’s intention. + + ```javascript + // bad + const foo = a && b < 0 || c > 0 || d + 1 === 0; + + // bad + const bar = a ** b - 5 % d; + + // bad + if (a || b && c) { + return d; + } + + // good + const foo = (a && b < 0) || c > 0 || (d + 1 === 0); + + // good + const bar = (a ** b) - (5 % d); + + // good + if ((a || b) && c) { + return d; + } + + // good + const bar = a + b / c * d; + ``` + **[⬆ back to top](#table-of-contents)** ## Blocks diff --git a/packages/eslint-config-airbnb-base/rules/style.js b/packages/eslint-config-airbnb-base/rules/style.js index 2ce7582a..fe621239 100644 --- a/packages/eslint-config-airbnb-base/rules/style.js +++ b/packages/eslint-config-airbnb-base/rules/style.js @@ -254,8 +254,18 @@ module.exports = { // disallow un-paren'd mixes of different operators // http://eslint.org/docs/rules/no-mixed-operators 'no-mixed-operators': ['error', { + // the list of arthmetic groups disallows mixing `%` and `**` + // with other arithmetic operators. groups: [ - ['+', '-', '*', '/', '%', '**'], + ['%', '**'], + ['%', '+'], + ['%', '-'], + ['%', '*'], + ['%', '/'], + ['**', '+'], + ['**', '-'], + ['**', '*'], + ['**', '/'], ['&', '|', '^', '~', '<<', '>>', '>>>'], ['==', '!=', '===', '!==', '>', '>=', '<', '<='], ['&&', '||'],