/
top-level-function.test.ts
67 lines (63 loc) · 1.83 KB
/
top-level-function.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint'
import { it } from 'vitest'
import rule, { RULE_NAME } from './top-level-function'
const valids = [
'function foo() {}',
// allow arrow function inside function
'function foo() { const bar = () => {} }',
// allow arrow function when type is specified
'const Foo: Bar = () => {}',
// allow let/var
'let foo = () => {}',
// allow arrow function in as
'const foo = (() => {}) as any',
// allow iife
';(() => {})()',
// allow export default
'export default () => {}',
'export default defineConfig(() => {})',
// allow one-line arrow function
'const foo = (x, y) => x + y',
'const foo = () => String(123)',
'const foo = async (x, y) => x + y',
'const foo = async () => String(123)',
]
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> => \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 = () => \n({})',
'export function foo () {\n return {}\n}',
],
[
'export const foo = async () => \n({})',
'export async function foo () {\n return {}\n}',
],
]
it('runs', () => {
const ruleTester: RuleTester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
})
ruleTester.run(RULE_NAME, rule, {
valid: valids,
invalid: invalids.map(i => ({
code: i[0],
output: i[1],
errors: [{ messageId: 'topLevelFunctionDeclaration' }],
})),
})
})