Skip to content

Commit 506d17c

Browse files
authoredApr 18, 2023
feat(plugin): allow some one-line arrow functions (#172)
1 parent ff74d69 commit 506d17c

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed
 

‎packages/eslint-plugin-antfu/src/rules/top-level-function.test.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,36 @@ const valids = [
1717
// allow export default
1818
'export default () => {}',
1919
'export default defineConfig(() => {})',
20+
// allow one-line arrow function
21+
'const foo = (x, y) => x + y',
22+
'const foo = async (x, y) => x + y',
23+
'const foo = () => String(123)',
24+
'const foo = () => ({})',
2025
]
2126

2227
const invalids = [
28+
[
29+
'const foo = (x, y) => \nx + y',
30+
'function foo (x, y) {\n return x + y\n}',
31+
],
2332
[
2433
'const foo = (as: string, bar: number) => { return as + bar }',
2534
'function foo (as: string, bar: number) { return as + bar }',
2635
],
2736
[
28-
'const foo = <K, T extends Boolean>(as: string, bar: number): Omit<T, K> => as + bar',
37+
'const foo = <K, T extends Boolean>(as: string, bar: number): Omit<T, K> => \nas + bar',
2938
'function foo <K, T extends Boolean>(as: string, bar: number): Omit<T, K> {\n return as + bar\n}',
3039
],
3140
[
3241
'export const foo = () => {}',
3342
'export function foo () {}',
3443
],
3544
[
36-
'export const foo = () => ({})',
45+
'export const foo = () => \n({})',
3746
'export function foo () {\n return {}\n}',
3847
],
3948
[
40-
'export const foo = async () => ({})',
49+
'export const foo = async () => \n({})',
4150
'export async function foo () {\n return {}\n}',
4251
],
4352
]

‎packages/eslint-plugin-antfu/src/rules/top-level-function.ts

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export default createEslintRule<Options, MessageIds>({
4040
return
4141
if (declaration.id.typeAnnotation)
4242
return
43+
if (
44+
declaration.init.body.type !== 'BlockStatement'
45+
&& declaration.id?.loc.start.line === declaration.init?.body.loc.end.line
46+
)
47+
return
4348

4449
const arrowFn = declaration.init
4550
const body = declaration.init.body

0 commit comments

Comments
 (0)
Please sign in to comment.