Skip to content

Commit

Permalink
[guide] [eslint config] [base] [breaking] Simplifies `no-mixed-operat…
Browse files Browse the repository at this point in the history
…ors`

- adds warning on mixing `/` and `*` arithmetic operators
- removes warnings on mixing `**` and arithmetic operators
- removes warning on mixing `in` and `instanceof`
- removes warning on mixing `~` with other bitwise operators
- removes mixing of assignment operators with comparison operators
  • Loading branch information
sharmilajesupaul authored and ljharb committed Jul 10, 2018
1 parent 45326cc commit c85639f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 7 additions & 3 deletions README.md
Expand Up @@ -2043,7 +2043,8 @@ Other Style Guides
```

<a name="comparison--no-mixed-operators"></a>
- [15.8](#comparison--no-mixed-operators) When mixing operators, enclose them in parentheses. The only exception is the standard arithmetic operators (`+`, `-`, `*`, & `/`) since their precedence is broadly understood. eslint: [`no-mixed-operators`](https://eslint.org/docs/rules/no-mixed-operators.html)
- [15.8](#comparison--no-mixed-operators) When mixing operators, enclose them in parentheses. The only exception is the standard arithmetic operators: `+`, `-`, and `**` since their precedence is broadly understood. We recommend enclosing `/` and `*` in parentheses because their precedence can be ambiguous when they are mixed.
eslint: [`no-mixed-operators`](https://eslint.org/docs/rules/no-mixed-operators.html)

> Why? This improves readability and clarifies the developer’s intention.
Expand All @@ -2060,19 +2061,22 @@ Other Style Guides
return d;
}

// bad
const bar = a + b / c * d;

// good
const foo = (a && b < 0) || c > 0 || (d + 1 === 0);

// good
const bar = (a ** b) - (5 % d);
const bar = a ** b - (5 % d);

// good
if (a || (b && c)) {
return d;
}

// good
const bar = a + b / c * d;
const bar = a + (b / c) * d;
```

**[⬆ back to top](#table-of-contents)**
Expand Down
10 changes: 3 additions & 7 deletions packages/eslint-config-airbnb-base/rules/style.js
Expand Up @@ -293,14 +293,10 @@ module.exports = {
['%', '-'],
['%', '*'],
['%', '/'],
['**', '+'],
['**', '-'],
['**', '*'],
['**', '/'],
['&', '|', '^', '~', '<<', '>>', '>>>'],
['==', '!=', '===', '!==', '>', '>=', '<', '<='],
['/', '*'],
['&', '|', '<<', '>>', '>>>'],
['==', '!=', '===', '!=='],
['&&', '||'],
['in', 'instanceof']
],
allowSamePrecedence: false
}],
Expand Down

0 comments on commit c85639f

Please sign in to comment.