fix(top-level-function): support async function

This commit is contained in:
Anthony Fu 2023-03-30 10:51:29 +02:00
parent 3fa861730d
commit 3a74c8e351
2 changed files with 9 additions and 3 deletions

View File

@ -36,6 +36,10 @@ const invalids = [
'export const foo = () => ({})', 'export const foo = () => ({})',
'export function foo () { return {} }', 'export function foo () { return {} }',
], ],
[
'export const foo = async () => ({})',
'export async function foo () { return {} }',
],
] ]
it('runs', () => { it('runs', () => {

View File

@ -67,12 +67,14 @@ export default createEslintRule<Options, MessageIds>({
const textTypeReturn = arrowFn.returnType const textTypeReturn = arrowFn.returnType
? code.slice(arrowFn.returnType.range[0], arrowFn.returnType.range[1]) ? 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({ // console.log({
// input: code.slice(node.range[0], node.range[1]), // 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)
}, },
}) })
}, },