Skip to content

Commit

Permalink
feat: inline return statement of arrow function (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <[email protected]>
  • Loading branch information
sxzz and antfu authored May 3, 2024
1 parent 4290c90 commit 99b3c5e
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ const foo = (msg: string): void => {
}
```

### `inline-arrow`

Inline return statement of arrow function.

Triggers:
- `/// inline-arrow`
- `/// ia`

```js
/// inline-arrow
const foo = async (msg: string): void => {
return fn(msg)
}
```

Will be converted to:

```js
const foo = async (msg: string): void => fn(msg)
```

### `keep-sorted`

Keep the object keys or array items sorted.
Expand Down
3 changes: 3 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { toForOf } from './to-for-of'
import { toDynamicImport } from './to-dynamic-import'
import { toStringLiteral } from './to-string-literal'
import { toTemplateLiteral } from './to-template-literal'
import { inlineArrow } from './inline-arrow'

// @keep-sorted
export {
inlineArrow,
keepSorted,
toArrow,
toDynamicImport,
Expand All @@ -21,6 +23,7 @@ export {

// @keep-sorted
export const builtinCommands = [
inlineArrow,
keepSorted,
toArrow,
toDynamicImport,
Expand Down
53 changes: 53 additions & 0 deletions src/commands/inline-arrow.test.ts
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'],
},
)
34 changes: 34 additions & 0 deletions src/commands/inline-arrow.ts
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')
},
})
},
}

0 comments on commit 99b3c5e

Please sign in to comment.