/
top-level-function.test.ts
51 lines (47 loc) · 1.35 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
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
';(() => {})()',
]
const invalids = [
[
'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',
'function foo <K, T extends Boolean>(as: string, bar: number): Omit<T, K> { return as + bar }',
],
[
'export const foo = () => {}',
'export function foo () {}',
],
[
'export const foo = () => ({})',
'export function foo () { return {} }',
],
]
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' }],
})),
})
})