Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin): allow some one-line arrow functions #172

Merged
merged 1 commit into from Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const fun = () => { return 'abc' }

Code above (block statement) will be formatted.

&& declaration.id?.loc.start.line === declaration.init?.body.loc.end.line
)
return

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