[eslint config] [base] [breaking] no-mixed-operators: only warn on ** and % mixed with arithmetic operators; removes violation against mixing common math operators.

Fixes #1071.
This commit is contained in:
Sharmila
2017-10-20 16:09:16 -07:00
committed by Jordan Harband
parent 97a6883621
commit 8161f32f1f
2 changed files with 43 additions and 1 deletions

View File

@@ -1943,6 +1943,38 @@ Other Style Guides
const baz = !c;
```
<a name="comparison--no-mixed-operators"></a><a name="15.8"></a>
- [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 developers 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

View File

@@ -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: [
['+', '-', '*', '/', '%', '**'],
['%', '**'],
['%', '+'],
['%', '-'],
['%', '*'],
['%', '/'],
['**', '+'],
['**', '-'],
['**', '*'],
['**', '/'],
['&', '|', '^', '~', '<<', '>>', '>>>'],
['==', '!=', '===', '!==', '>', '>=', '<', '<='],
['&&', '||'],