Skip to content

Commit

Permalink
fix(top-level-function): support async function
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Mar 30, 2023
1 parent 3fa8617 commit 3a74c8e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Expand Up @@ -36,6 +36,10 @@ const invalids = [
'export const foo = () => ({})',
'export function foo () { return {} }',
],
[
'export const foo = async () => ({})',
'export async function foo () { return {} }',
],
]

it('runs', () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/eslint-plugin-antfu/src/rules/top-level-function.ts
Expand Up @@ -67,12 +67,14 @@ export default createEslintRule<Options, MessageIds>({
const textTypeReturn = arrowFn.returnType
? code.slice(arrowFn.returnType.range[0], arrowFn.returnType.range[1])
: ''
const text = `function ${textName} ${textGeneric}(${textArgs})${textTypeReturn} ${textBody}`
const textAsync = arrowFn.async ? 'async ' : ''

const final = `${textAsync}function ${textName} ${textGeneric}(${textArgs})${textTypeReturn} ${textBody}`
// console.log({
// input: code.slice(node.range[0], node.range[1]),
// output: text,
// output: final,
// })
return fixer.replaceTextRange([node.range[0], node.range[1]], text)
return fixer.replaceTextRange([node.range[0], node.range[1]], final)
},
})
},
Expand Down

0 comments on commit 3a74c8e

Please sign in to comment.