-
-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
517 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# 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 lined 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 varaibles being unnecessarily mutable and prevents `prefer-const` from flagging the variable. | ||
|
||
This rule is fixable. | ||
|
||
|
||
## Fail | ||
|
||
```js | ||
let foo ='' | ||
if(bar){ | ||
foo = 3 | ||
} | ||
else{ | ||
foo = 4 | ||
} | ||
``` | ||
|
||
```js | ||
if(bar){ | ||
return 3 | ||
} | ||
else{ | ||
return 4 | ||
} | ||
``` | ||
|
||
```js | ||
if(bar){ | ||
await firstPromise() | ||
} | ||
else{ | ||
await secondPromise() | ||
} | ||
``` | ||
|
||
```js | ||
if(bar){ | ||
yield bat | ||
} | ||
else{ | ||
yield baz | ||
} | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
let foo = bar ? 3 : 4 | ||
``` | ||
|
||
```js | ||
return bar ? 3 : 4 | ||
``` | ||
|
||
```js | ||
await (bar ? 3 : 4) | ||
``` | ||
|
||
```js | ||
yield (bar ? 3 : 4) | ||
``` | ||
|
||
```js | ||
let foo = '' | ||
if(bar){ | ||
baz() | ||
foo = 3 | ||
} | ||
else{ | ||
foo = 4 | ||
} | ||
``` | ||
|
||
```js | ||
if(bar){ | ||
foo = 3 | ||
} | ||
else{ | ||
return 4 | ||
} | ||
``` | ||
|
||
```js | ||
if(bar){ | ||
foo = 3 | ||
} | ||
else{ | ||
baz = 4 | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
'use strict'; | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
const create = context => { | ||
function checkIfStatement(node) { | ||
if (isSingleBlockStatement(node) && | ||
isSameType(node) && | ||
checkConsequentAndAlternateType(node)) { | ||
context.report({ | ||
node, | ||
message: 'This `if` statement can be replaced by a ternary operator.', | ||
fix(fixer) { | ||
return fixFunction(node, fixer); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
function fixFunction(node, fixer) { | ||
const sourceCode = context.getSourceCode(); | ||
let prefix = ''; | ||
const ifCondition = sourceCode.getText(node.test); | ||
let left = ''; | ||
let right = ''; | ||
if (getNodeBody(node.consequent).type === 'ExpressionStatement') { | ||
const expressionType = getNodeBody(node.consequent).expression.type; | ||
if (expressionType === 'AssignmentExpression') { | ||
prefix = sourceCode.getText(getNodeBody(node.consequent).expression.left) + ' = '; | ||
left = sourceCode.getText(getNodeBody(node.consequent).expression.right); | ||
right = sourceCode.getText(getNodeBody(node.alternate).expression.right); | ||
} else { | ||
if (expressionType === 'AwaitExpression') { | ||
prefix = 'await '; | ||
} else { | ||
prefix = getNodeBody(node.consequent).expression.delegate ? 'yield* ' : 'yield '; | ||
} | ||
|
||
left = sourceCode.getText(getNodeBody(node.consequent).expression.argument); | ||
right = sourceCode.getText(getNodeBody(node.alternate).expression.argument); | ||
} | ||
} else { | ||
prefix = 'return '; | ||
left = sourceCode.getText(getNodeBody(node.consequent).argument); | ||
right = sourceCode.getText(getNodeBody(node.alternate).argument); | ||
} | ||
|
||
const replacement = prefix + '(' + ifCondition + ' ? ' + left + ' : ' + right + ')'; | ||
return fixer.replaceText(node, replacement); | ||
} | ||
|
||
return { | ||
IfStatement: checkIfStatement | ||
}; | ||
}; | ||
|
||
function isSingleBlockStatement(node) { | ||
return [node.consequent, node.alternate].every(node => { | ||
return node && (node.type !== 'BlockStatement' || node.body.length === 1); | ||
}); | ||
} | ||
|
||
function getNodeBody(node) { | ||
return node.type === 'BlockStatement' ? node.body[0] : node; | ||
} | ||
|
||
function isSameType(node) { | ||
return getNodeBody(node.consequent).type === getNodeBody(node.alternate).type; | ||
} | ||
|
||
function checkConsequentAndAlternateType(node) { | ||
return ( | ||
(getNodeBody(node.consequent).type === 'ReturnStatement' || | ||
(getNodeBody(node.consequent).type === 'ExpressionStatement' && checkConsequentAndAlternateExpressionStatement(node)))); | ||
} | ||
|
||
function checkConsequentAndAlternateExpressionStatement(node) { | ||
const consequentType = getNodeBody(node.consequent).expression.type; | ||
return consequentType === getNodeBody(node.alternate).expression.type && | ||
( | ||
consequentType === 'AwaitExpression' || | ||
(consequentType === 'YieldExpression' && compareYieldExpressions(node)) || | ||
(consequentType === 'AssignmentExpression' && compareConsequentAndAlternateAssignments(node)) | ||
) && | ||
checkNotAlreadyTernary(node); | ||
} | ||
|
||
function compareYieldExpressions(node) { | ||
return getNodeBody(node.consequent).expression.delegate === getNodeBody(node.alternate).expression.delegate; | ||
} | ||
|
||
function compareConsequentAndAlternateAssignments(node) { | ||
return getNodeBody(node.consequent).expression.left.name === getNodeBody(node.alternate).expression.left.name; | ||
} | ||
|
||
function checkNotAlreadyTernary(node) { | ||
return getNodeBody(node.consequent).expression.type === 'AssignmentExpression' ? | ||
getNodeBody(node.consequent).expression.right.type !== 'ConditionalExpression' && | ||
getNodeBody(node.alternate).expression.right.type !== 'ConditionalExpression' : | ||
getNodeBody(node.consequent).expression.argument.type !== 'ConditionalExpression' && | ||
getNodeBody(node.alternate).expression.argument.type !== 'ConditionalExpression'; | ||
} | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
fixable: 'code' | ||
} | ||
}; |
Oops, something went wrong.