-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
4 changed files
with
210 additions
and
0 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
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,37 @@ | ||
# `to-ternary` | ||
|
||
Convert an `if-else` statement to a [`ternary expression`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator). | ||
|
||
## Triggers | ||
|
||
- `/// to-ternary` | ||
- `/// to-3` | ||
- `/// 2ternary` | ||
- `/// 23` | ||
|
||
## Examples | ||
|
||
```js | ||
/// to-ternary | ||
if (condition) | ||
foo() | ||
else | ||
bar = 1 | ||
|
||
// For conditional assignments to the same variable | ||
/// to-ternary | ||
if (condition1) | ||
foo = 1 | ||
else if (condition2) | ||
foo = bar | ||
else | ||
foo = baz() | ||
``` | ||
|
||
Will be converted to (the command comment will be removed along the way): | ||
|
||
```js | ||
condition ? foo() : bar = 1 | ||
|
||
foo = condition1 ? 1 : condition2 ? bar : baz() | ||
``` |
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 @@ | ||
import { toTernary as command } from './to-ternary' | ||
import { $, run } from './_test-utils' | ||
|
||
run( | ||
command, | ||
// no `else` | ||
{ | ||
code: $` | ||
/// to-ternary | ||
if (c1) | ||
foo() | ||
else if (c2) | ||
bar = 1 | ||
`, | ||
errors: ['command-error'], | ||
}, | ||
// too many lines in a `if` | ||
{ | ||
code: $` | ||
/// 2ternary | ||
if (c1) { | ||
foo() | ||
bar = 1 | ||
} | ||
else { | ||
bar = 2 | ||
} | ||
`, | ||
errors: ['command-error'], | ||
}, | ||
// normal | ||
{ | ||
code: $` | ||
/// to-3 | ||
if (c1) | ||
foo() | ||
else | ||
bar = 1 | ||
`, | ||
output: $` | ||
c1 ? foo() : bar = 1 | ||
`, | ||
errors: ['command-fix'], | ||
}, | ||
// more `else-if` and block | ||
{ | ||
code: $` | ||
/// 23 | ||
if (a > b) { | ||
foo() | ||
} | ||
else if (c2) { | ||
bar = 1 | ||
} | ||
else { | ||
baz() | ||
} | ||
`, | ||
output: $` | ||
a > b ? foo() : c2 ? bar = 1 : baz() | ||
`, | ||
errors: ['command-fix'], | ||
}, | ||
// same name assignment | ||
{ | ||
code: $` | ||
/// to-ternary | ||
if (c1) | ||
foo = 1 | ||
else if (c2) | ||
foo = bar | ||
else | ||
foo = baz() | ||
`, | ||
output: $` | ||
foo = c1 ? 1 : c2 ? bar : baz() | ||
`, | ||
errors: ['command-fix'], | ||
}, | ||
// different names assignment | ||
{ | ||
code: $` | ||
/// to-ternary | ||
if (c1) | ||
foo = 1 | ||
else if (c2) | ||
bar = 2 | ||
else | ||
baz() | ||
`, | ||
output: $` | ||
c1 ? foo = 1 : c2 ? bar = 2 : baz() | ||
`, | ||
errors: ['command-fix'], | ||
}, | ||
) |
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,74 @@ | ||
import type { Command, Tree } from '../types' | ||
|
||
export const toTernary: Command = { | ||
name: 'to-ternary', | ||
match: /^\s*[/:@]\s*(?:to-|2)(?:ternary|3)$/, | ||
action(ctx) { | ||
const node = ctx.findNodeBelow('IfStatement') | ||
|
||
if (!node) | ||
return ctx.reportError('Unable to find an `if` statement to convert') | ||
|
||
let result = '' | ||
let isAssignment = true | ||
|
||
const normalizeStatement = (n: Tree.Statement | null) => { | ||
if (!n) | ||
return ctx.reportError('Unable to convert `if` statement without an `else` clause') | ||
if (n.type === 'BlockStatement') { | ||
if (n.body.length !== 1) | ||
return ctx.reportError('Unable to convert statement contains more than one expression') | ||
else return n.body[0] | ||
} | ||
else { | ||
return n | ||
} | ||
} | ||
|
||
const getAssignmentId = (n: Tree.Statement) => { | ||
if (n.type === 'IfStatement') | ||
n = n.consequent | ||
if (n.type !== 'ExpressionStatement' || n.expression.type !== 'AssignmentExpression' || n.expression.left.type !== 'Identifier') | ||
return | ||
return ctx.getTextOf(n.expression.left) | ||
} | ||
|
||
let ifNode: Tree.IfStatement = node | ||
while (ifNode) { | ||
const consequent = normalizeStatement(ifNode.consequent) | ||
const alternate = normalizeStatement(ifNode.alternate) | ||
|
||
if (!consequent || !alternate) | ||
return | ||
|
||
if (isAssignment) { | ||
const ifId = getAssignmentId(consequent) | ||
const elseId = getAssignmentId(alternate) | ||
|
||
if (!ifId || ifId !== elseId) | ||
isAssignment = false | ||
} | ||
|
||
result += `${ctx.getTextOf(ifNode.test)} ? ${ctx.getTextOf(consequent)} : ` | ||
|
||
if (alternate.type !== 'IfStatement') { | ||
result += ctx.getTextOf(alternate) | ||
break | ||
} | ||
else { | ||
ifNode = alternate | ||
} | ||
} | ||
|
||
if (isAssignment) { | ||
const id = getAssignmentId(normalizeStatement(node.consequent)!) | ||
result = `${id} = ${result.replaceAll(`${id} = `, '')}` | ||
} | ||
|
||
ctx.report({ | ||
node, | ||
message: 'Convert to ternary', | ||
fix: fix => fix.replaceTextRange(node.range, result), | ||
}) | ||
}, | ||
} |