Skip to content

Commit 7e15ccf

Browse files
committed
[eslint config] [breaking] disallow unneeded ternary expressions.
1 parent fa4925b commit 7e15ccf

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,24 +1295,40 @@ Other Style Guides
12951295
eslint rules: [`no-nested-ternary`](http://eslint.org/docs/rules/no-nested-ternary.html).
12961296

12971297
```javascript
1298-
//bad
1298+
// bad
12991299
const foo = maybe1 > maybe2
13001300
? "bar"
13011301
: value1 > value2 ? "baz" : null;
13021302
1303-
//better
1303+
// better
13041304
const maybeNull = value1 > value2 ? 'baz' : null;
13051305
13061306
const foo = maybe1 > maybe2
13071307
? 'bar'
13081308
: maybeNull;
13091309
1310-
//best
1310+
// best
13111311
const maybeNull = value1 > value2 ? 'baz' : null;
13121312
13131313
const foo = maybe1 > maybe2 ? 'bar' : maybeNull;
13141314
```
13151315

1316+
- [15.6](#15.6) <a name='15.6'></a> Avoid unneeded ternary statements.
1317+
1318+
eslint rules: [`no-unneeded-ternary`](http://eslint.org/docs/rules/no-unneeded-ternary.html).
1319+
1320+
```javascript
1321+
// bad
1322+
const foo = a ? a : b;
1323+
const bar = c ? true : false;
1324+
const baz = c ? false : true;
1325+
1326+
// good
1327+
const foo = a || b;
1328+
const bar = !!c;
1329+
const baz = !c;
1330+
```
1331+
13161332
**[⬆ back to top](#table-of-contents)**
13171333

13181334

packages/eslint-config-airbnb/rules/style.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ module.exports = {
7474
// disallow dangling underscores in identifiers
7575
'no-underscore-dangle': 0,
7676
// disallow the use of Boolean literals in conditional expressions
77-
'no-unneeded-ternary': 0,
77+
// also, prefer `a || b` over `a ? a : b`
78+
// http://eslint.org/docs/rules/no-unneeded-ternary
79+
'no-unneeded-ternary': [2, { "defaultAssignment": false }],
7880
// require padding inside curly braces
7981
'object-curly-spacing': [2, 'always'],
8082
// allow just one var statement per function

0 commit comments

Comments
 (0)