Skip to content

Commit

Permalink
feat(plugin): allow some one-line arrow functions (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Apr 18, 2023
1 parent ff74d69 commit 506d17c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/eslint-plugin-antfu/src/rules/top-level-function.test.ts
Expand Up @@ -17,27 +17,36 @@ const valids = [
// allow export default
'export default () => {}',
'export default defineConfig(() => {})',
// allow one-line arrow function
'const foo = (x, y) => x + y',
'const foo = async (x, y) => x + y',
'const foo = () => String(123)',
'const foo = () => ({})',
]

const invalids = [
[
'const foo = (x, y) => \nx + y',
'function foo (x, y) {\n return x + y\n}',
],
[
'const foo = (as: string, bar: number) => { return as + bar }',
'function foo (as: string, bar: number) { return as + bar }',
],
[
'const foo = <K, T extends Boolean>(as: string, bar: number): Omit<T, K> => as + bar',
'const foo = <K, T extends Boolean>(as: string, bar: number): Omit<T, K> => \nas + bar',
'function foo <K, T extends Boolean>(as: string, bar: number): Omit<T, K> {\n return as + bar\n}',
],
[
'export const foo = () => {}',
'export function foo () {}',
],
[
'export const foo = () => ({})',
'export const foo = () => \n({})',
'export function foo () {\n return {}\n}',
],
[
'export const foo = async () => ({})',
'export const foo = async () => \n({})',
'export async function foo () {\n return {}\n}',
],
]
Expand Down
5 changes: 5 additions & 0 deletions packages/eslint-plugin-antfu/src/rules/top-level-function.ts
Expand Up @@ -40,6 +40,11 @@ export default createEslintRule<Options, MessageIds>({
return
if (declaration.id.typeAnnotation)
return
if (
declaration.init.body.type !== 'BlockStatement'
&& declaration.id?.loc.start.line === declaration.init?.body.loc.end.line
)
return

const arrowFn = declaration.init
const body = declaration.init.body
Expand Down

0 comments on commit 506d17c

Please sign in to comment.