Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefer-ternary rule #514

Merged
merged 56 commits into from Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
0e75c80
Added rule and tests
jmoore914 Jan 26, 2020
efc968a
Added documentation
jmoore914 Feb 7, 2020
3fffe86
Tweaked language; adding throw, new, yield, await
jmoore914 Feb 14, 2020
80a3a15
Added tests and changed fix function
jmoore914 Feb 17, 2020
3b4d6ab
Added new options to docs; made yield false by default
jmoore914 Feb 17, 2020
b210f43
Delete launch.json
sindresorhus Feb 17, 2020
eb7aace
Simplified rule
jmoore914 Feb 29, 2020
d3a4527
Merge branch 'prefer-ternary' of https://github.com/jmoore914/eslint-…
jmoore914 Feb 29, 2020
52f5344
Added await; check to make sure not nested ternary
jmoore914 Mar 3, 2020
d59f2b4
Update rules/prefer-ternary.js
jmoore914 Mar 3, 2020
bd9e21c
Works without braces
jmoore914 Mar 3, 2020
f0bdc34
Linting
jmoore914 Mar 3, 2020
48a26aa
More linting
jmoore914 Mar 3, 2020
73a2263
More linting
jmoore914 Mar 3, 2020
c600475
Added additional tests
jmoore914 Mar 6, 2020
8beb75e
Merge remote-tracking branch 'upstream/master' into prefer-ternary
jmoore914 Mar 13, 2020
f75101e
Added support for yield*
jmoore914 Mar 13, 2020
64e9a0b
Linting
jmoore914 Mar 13, 2020
503493c
Merge branch 'master' into prefer-ternary
fisker Mar 16, 2020
5860a73
fix style
fisker Mar 16, 2020
a325e56
fix integration test
fisker Mar 16, 2020
3dfcd8e
Merge branch 'master' into prefer-ternary
fisker Mar 26, 2020
25abee5
Merge remote-tracking branch 'remotes/upstream/master' into prefer-te…
fisker May 5, 2020
a99e7ae
Ignore `IfStatement.test=ConditionalExpression`
fisker May 5, 2020
6a3c793
Improve `getNodeBody`
fisker May 5, 2020
ee7f888
Fix logic
fisker May 5, 2020
b8dd909
Nested merge
fisker May 6, 2020
59b9b97
Crazy cases
fisker May 6, 2020
51515d8
Fix lower precedence
fisker May 6, 2020
6688591
Add `()` to yield
fisker May 6, 2020
8c3a70f
Update examples
fisker May 6, 2020
133c2fa
More tests
fisker May 6, 2020
9a7fc8e
Style
fisker May 6, 2020
428b3c0
Test `operator`
fisker May 7, 2020
59a7970
Ignore `IfStatement.alternate`
fisker May 7, 2020
da5dfd5
Handle `return/yield` undefined
fisker Jun 2, 2020
e588687
Fix throw
fisker Jun 2, 2020
aca1f1e
Clean up
fisker Jun 2, 2020
efff08c
Code style
fisker Jun 2, 2020
003acc3
Update docs
fisker Jun 2, 2020
8768b41
Comment
fisker Jun 2, 2020
76c2378
Merge branch 'master' into prefer-ternary
fisker Jun 2, 2020
89906b2
Merge branch 'prefer-ternary' of github.com:jmoore914/eslint-plugin-u…
fisker Jun 2, 2020
853acf9
Fix lint
fisker Jun 2, 2020
397c749
Add extra integration test
fisker Jun 2, 2020
f39f38f
Move to top
fisker Jun 2, 2020
23974b3
ignore
fisker Jun 2, 2020
5b99354
Fix error message
fisker Jun 2, 2020
7cb5ee2
Coverage
fisker Jun 2, 2020
1d06eeb
Merge branch 'master' into prefer-ternary
fisker Sep 27, 2020
dd81f37
Fix test
fisker Sep 27, 2020
cee4f4d
Handle `IfStatement` is body of some node
fisker Sep 27, 2020
b37b4a9
Fix code style
fisker Sep 27, 2020
7cc55ed
Merge branch 'master' into prefer-ternary
fisker Sep 28, 2020
332ae21
Update prefer-ternary.md
sindresorhus Sep 29, 2020
cab9830
Update projects.js
sindresorhus Sep 29, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
122 changes: 122 additions & 0 deletions docs/rules/prefer-ternary.md
@@ -0,0 +1,122 @@
# Prefer ternary expressions over simple `if-else` statements

This rule enforces the use of ternary expressions over 'simple' `if-else` statements, where 'simple' means the consequent and alternate are each one line and have the same basic type and form.

Using an `if-else` statement typically results in more lines of code than a single-line ternary expression, which leads to an unnecessarily larger codebase that is more difficult to maintain.

Additionally, using an `if-else` statement can result in defining variables using `let` or `var` solely to be reassigned within the blocks. This leads to variables being unnecessarily mutable and prevents `prefer-const` from flagging the variable.

This rule is fixable.

## Fail

```js
function unicorn() {
if (test) {
return a;
} else {
return b;
}
}
```

```js
function* unicorn() {
if (test) {
yield a;
} else {
yield b;
}
}
```

```js
async function unicorn() {
if (test) {
await a();
} else {
await b();
}
}
```

```js
if (test) {
throw new Error('foo');
} else {
throw new Error('bar');
}
```

```js
let foo;
if (test) {
foo = 1;
} else {
foo = 2;
}
```

## Pass

```js
function unicorn() {
return test ? a : b;
}
```

```js
function* unicorn() {
yield (test ? a : b);
}
```

```js
async function unicorn() {
await (test ? a() : b());
}
```

```js
const error = test ? new Error('foo') : new Error('bar');
throw error;
```

```js
let foo;
foo = test ? 1 : 2;
```

```js
// Multiple expressions
let foo;
let bar;
if (test) {
foo = 1;
bar = 2;
} else{
foo = 2;
}
```

```js
// Different expressions
function unicorn() {
if (test) {
return a;
} else {
throw new Error('error');
}
}
```

```js
// Assign to different variable
let foo;
let bar;
if (test) {
foo = 1;
} else{
baz = 2;
}
```
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -70,6 +70,7 @@ module.exports = {
'unicorn/prefer-spread': 'error',
'unicorn/prefer-starts-ends-with': 'error',
'unicorn/prefer-string-slice': 'error',
'unicorn/prefer-ternary': 'error',
'unicorn/prefer-text-content': 'error',
'unicorn/prefer-trim-start-end': 'error',
'unicorn/prefer-type-error': 'error',
Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -93,6 +93,9 @@
"extends": [
"plugin:eslint-plugin/all"
],
"ignores": [
"test/integration/{fixtures,unicorn}/**"
],
"overrides": [
{
"files": "rules/utils/*.js",
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Expand Up @@ -84,6 +84,7 @@ Configure it in `package.json`.
"unicorn/prefer-spread": "error",
"unicorn/prefer-starts-ends-with": "error",
"unicorn/prefer-string-slice": "error",
"unicorn/prefer-ternary": "off",
"unicorn/prefer-text-content": "error",
"unicorn/prefer-trim-start-end": "error",
"unicorn/prefer-type-error": "error",
Expand Down Expand Up @@ -147,6 +148,7 @@ Configure it in `package.json`.
- [prefer-spread](docs/rules/prefer-spread.md) - Prefer the spread operator over `Array.from()`. *(fixable)*
- [prefer-starts-ends-with](docs/rules/prefer-starts-ends-with.md) - Prefer `String#startsWith()` & `String#endsWith()` over more complex alternatives. *(partly fixable)*
- [prefer-string-slice](docs/rules/prefer-string-slice.md) - Prefer `String#slice()` over `String#substr()` and `String#substring()`. *(partly fixable)*
- [prefer-ternary](docs/rules/prefer-ternary.md) - Prefer ternary expressions over simple `if-else` statements. *(fixable)*
- [prefer-text-content](docs/rules/prefer-text-content.md) - Prefer `.textContent` over `.innerText`. *(fixable)*
- [prefer-trim-start-end](docs/rules/prefer-trim-start-end.md) - Prefer `String#trimStart()` / `String#trimEnd()` over `String#trimLeft()` / `String#trimRight()`. *(fixable)*
- [prefer-type-error](docs/rules/prefer-type-error.md) - Enforce throwing `TypeError` in type checking conditions. *(fixable)*
Expand Down
8 changes: 3 additions & 5 deletions rules/no-for-loop.js
Expand Up @@ -384,11 +384,9 @@ const create = context => {
}

if (elementNode) {
if (removeDeclaration) {
yield fixer.removeRange(getRemovalRange(elementNode, sourceCode));
} else {
yield fixer.replaceText(elementNode.init, element);
}
yield removeDeclaration ?
fixer.removeRange(getRemovalRange(elementNode, sourceCode)) :
fixer.replaceText(elementNode.init, element);
}
};
}
Expand Down