-
-
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.
feat: inline return statement of arrow function (#4)
Co-authored-by: Anthony Fu <[email protected]>
- Loading branch information
Showing
4 changed files
with
111 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
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,53 @@ | ||
import { inlineArrow as command } from './inline-arrow' | ||
import { d, run } from './_test-utils' | ||
|
||
run( | ||
command, | ||
// no arrow function | ||
{ | ||
code: d` | ||
///inline-arrow | ||
const a = 1`, | ||
output: null, | ||
errors: 'command-error', | ||
}, | ||
// multi statement | ||
{ | ||
code: d` | ||
/// inline-arrow | ||
export const foo = arg => { | ||
const a = 1 | ||
return a | ||
}`, | ||
output: null, | ||
errors: 'command-error', | ||
}, | ||
{ | ||
code: d` | ||
/// inline-arrow | ||
export const foo = <T = 1>(arg: Z): Bar => { | ||
return arg | ||
}`, | ||
output: d` | ||
export const foo = <T = 1>(arg: Z): Bar => arg`, | ||
errors: ['command-removal', 'command-fix'], | ||
}, | ||
// no return statement | ||
{ | ||
code: d` | ||
///inline-arrow | ||
const foo = () => {}`, | ||
output: d` | ||
const foo = () => undefined`, | ||
errors: ['command-removal', 'command-fix'], | ||
}, | ||
// without return argument | ||
{ | ||
code: d` | ||
// /ia | ||
export default <T = 1>(arg: Z): Bar => { return }`, | ||
output: d` | ||
export default <T = 1>(arg: Z): Bar => undefined`, | ||
errors: ['command-removal', '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,34 @@ | ||
import type { Command, Tree } from '../types' | ||
|
||
export const inlineArrow: Command = { | ||
name: 'inline-arrow', | ||
match: /^[\/:@]\s*(inline-arrow|ia)$/, | ||
action(ctx) { | ||
const arrowFn = ctx.findNodeBelow('ArrowFunctionExpression') | ||
if (!arrowFn) | ||
return ctx.reportError('Unable to find arrow function to convert') | ||
const body = arrowFn.body | ||
if (body.type !== 'BlockStatement') | ||
return ctx.reportError('Arrow function body must have a block statement') | ||
|
||
const statements = body.body | ||
if ( | ||
(statements.length !== 1 || statements[0].type !== 'ReturnStatement') | ||
&& (statements.length !== 0) | ||
) | ||
return ctx.reportError('Arrow function body must have a single statement') | ||
const statement = statements[0] as Tree.ReturnStatement | undefined | ||
|
||
ctx.removeComment() | ||
ctx.report({ | ||
node: arrowFn, | ||
loc: body.loc, | ||
message: 'Inline arrow function', | ||
fix(fixer) { | ||
return fixer.replaceTextRange(body.range, statement && statement.argument | ||
? ctx.getTextOf(statement.argument) | ||
: 'undefined') | ||
}, | ||
}) | ||
}, | ||
} |