From 3a74c8e3519f3fd0c0ee8899a702bb3f7dbb6b14 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 30 Mar 2023 10:51:29 +0200 Subject: [PATCH] fix(top-level-function): support async function --- .../src/rules/top-level-function.test.ts | 4 ++++ .../eslint-plugin-antfu/src/rules/top-level-function.ts | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin-antfu/src/rules/top-level-function.test.ts b/packages/eslint-plugin-antfu/src/rules/top-level-function.test.ts index c4b917a..e975e45 100644 --- a/packages/eslint-plugin-antfu/src/rules/top-level-function.test.ts +++ b/packages/eslint-plugin-antfu/src/rules/top-level-function.test.ts @@ -36,6 +36,10 @@ const invalids = [ 'export const foo = () => ({})', 'export function foo () { return {} }', ], + [ + 'export const foo = async () => ({})', + 'export async function foo () { return {} }', + ], ] it('runs', () => { diff --git a/packages/eslint-plugin-antfu/src/rules/top-level-function.ts b/packages/eslint-plugin-antfu/src/rules/top-level-function.ts index 9a5056e..0986f4a 100644 --- a/packages/eslint-plugin-antfu/src/rules/top-level-function.ts +++ b/packages/eslint-plugin-antfu/src/rules/top-level-function.ts @@ -67,12 +67,14 @@ export default createEslintRule({ 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) }, }) },