forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-unsafe-array-reduce.test.ts
67 lines (64 loc) · 1.53 KB
/
no-unsafe-array-reduce.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 path from 'path';
import rule from '../../src/rules/no-unsafe-array-reduce';
import { RuleTester } from '../RuleTester';
const rootDir = path.resolve(__dirname, '../fixtures/');
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
tsconfigRootDir: rootDir,
project: './tsconfig.json',
},
});
ruleTester.run('no-unsafe-array-reduce', rule, {
valid: [
{
// Type parameter is fine
code: `
type Dict = { [key in string]?: string }
[].reduce<Dict>((acc, _) => acc, {});`,
},
{
// Typed accumulator is fine
code: `
type Dict = { [key in string]?: string }
[].reduce((acc: Dict, _) => acc, {});`,
},
{
// Typed init value is fine
code: `
type Dict = { [key in string]?: string }
[].reduce((acc, _) => acc, {} as Dict);`,
},
{
// Non-object init value is fine
code: `
type Dict = { [key in string]?: string }
[].reduce((acc, _) => acc, []);`,
},
{
// Non-reduce is fine
code: `[].filter(() => true);`,
},
{
// Non-array reduce is fine
code: `reduce((acc, _) => acc, {});`,
},
],
invalid: [
{
code: `[].reduce((acc, cur) => acc, {});`,
output: `[].reduce((acc, cur) => acc, {});`,
errors: [{ messageId: 'unsafeArrayReduce' }],
},
{
code: `
const arr = []
arr.reduce((acc, cur) => acc, {});`,
output: `
const arr = []
arr.reduce((acc, cur) => acc, {});`,
errors: [{ messageId: 'unsafeArrayReduce' }],
},
],
});