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

This commit is contained in:
曾明健 2023-04-18 19:54:50 +08:00 committed by GitHub
parent ff74d69719
commit 506d17c3f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View File

@ -17,15 +17,24 @@ const valids = [
// allow export default // allow export default
'export default () => {}', 'export default () => {}',
'export default defineConfig(() => {})', '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 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 }', 'const foo = (as: string, bar: number) => { return as + bar }',
'function 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}', 'function foo <K, T extends Boolean>(as: string, bar: number): Omit<T, K> {\n return as + bar\n}',
], ],
[ [
@ -33,11 +42,11 @@ const invalids = [
'export function foo () {}', 'export function foo () {}',
], ],
[ [
'export const foo = () => ({})', 'export const foo = () => \n({})',
'export function foo () {\n return {}\n}', 'export function foo () {\n return {}\n}',
], ],
[ [
'export const foo = async () => ({})', 'export const foo = async () => \n({})',
'export async function foo () {\n return {}\n}', 'export async function foo () {\n return {}\n}',
], ],
] ]

View File

@ -40,6 +40,11 @@ export default createEslintRule<Options, MessageIds>({
return return
if (declaration.id.typeAnnotation) if (declaration.id.typeAnnotation)
return return
if (
declaration.init.body.type !== 'BlockStatement'
&& declaration.id?.loc.start.line === declaration.init?.body.loc.end.line
)
return
const arrowFn = declaration.init const arrowFn = declaration.init
const body = declaration.init.body const body = declaration.init.body