/
prefer-readonly-parameter-types.test.ts
248 lines (234 loc) · 5.5 KB
/
prefer-readonly-parameter-types.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import rule from '../../src/rules/prefer-readonly-parameter-types';
import { RuleTester, getFixturesRootDir } from '../RuleTester';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import {
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
} from '../../src/util';
type MessageIds = InferMessageIdsTypeFromRule<typeof rule>;
type Options = InferOptionsTypeFromRule<typeof rule>;
const rootPath = getFixturesRootDir();
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: rootPath,
project: './tsconfig.json',
},
});
const primitives = [
'boolean',
'true',
'string',
"'a'",
'number',
'1',
'any',
'unknown',
'never',
'null',
'undefined',
];
const arrays = [
'readonly string[]',
'Readonly<string[]>',
'ReadonlyArray<string>',
'readonly [string]',
'Readonly<[string]>',
];
const objects = [
'{ foo: "" }',
'{ foo: readonly string[] }',
'{ foo(): void }',
];
const weirdIntersections = [
`
interface Test {
(): void
readonly property: boolean
}
function foo(arg: Test) {}
`,
`
type Test = (() => void) & {
readonly property: boolean
};
function foo(arg: Test) {}
`,
];
ruleTester.run('prefer-readonly-parameter-types', rule, {
valid: [
'function foo(arg: { readonly a: string }) {}',
'function foo() {}',
// primitives
...primitives.map(type => `function foo(arg: ${type}) {}`),
// arrays
...arrays.map(type => `function foo(arg: ${type}) {}`),
// nested arrays
'function foo(arg: readonly (readonly string[])[]) {}',
'function foo(arg: Readonly<Readonly<string[]>[]>) {}',
'function foo(arg: ReadonlyArray<ReadonlyArray<string>>) {}',
// functions
'function foo(arg: () => void) {}',
// unions
'function foo(arg: string | null) {}',
'function foo(arg: string | ReadonlyArray<string>) {}',
'function foo(arg: string | (() => void)) {}',
'function foo(arg: ReadonlyArray<string> | ReadonlyArray<number>) {}',
// objects
...objects.map(type => `function foo(arg: Readonly<${type}>) {}`),
`
function foo(arg: {
readonly foo: {
readonly bar: string
}
}) {}
`,
// weird other cases
...weirdIntersections.map(code => code),
`
interface Test extends ReadonlyArray<string> {
readonly property: boolean
}
function foo(arg: Readonly<Test>) {}
`,
`
type Test = (readonly string[]) & {
readonly property: boolean
};
function foo(arg: Readonly<Test>) {}
`,
`
type Test = string & number;
function foo(arg: Test) {}
`,
// declaration merging
`
class Foo {
readonly bang = 1;
}
interface Foo {
readonly prop: string;
}
interface Foo {
readonly prop2: string;
}
function foo(arg: Foo) {}
`,
// method made readonly via Readonly<T>
`
class Foo {
method() {}
}
function foo(arg: Readonly<Foo>) {}
`,
],
invalid: [
// arrays
...arrays.map<TSESLint.InvalidTestCase<MessageIds, Options>>(baseType => {
const type = baseType
.replace(/readonly /g, '')
.replace(/Readonly<(.+?)>/g, '$1')
.replace(/ReadonlyArray/g, 'Array');
return {
code: `function foo(arg: ${type}) {}`,
errors: [
{
messageId: 'shouldBeReadonly',
column: 14,
endColumn: 19 + type.length,
},
],
};
}),
// nested arrays
{
code: 'function foo(arg: readonly (string[])[]) {}',
errors: [
{
messageId: 'shouldBeReadonly',
column: 14,
endColumn: 40,
},
],
},
{
code: 'function foo(arg: Readonly<string[][]>) {}',
errors: [
{
messageId: 'shouldBeReadonly',
column: 14,
endColumn: 39,
},
],
},
{
code: 'function foo(arg: ReadonlyArray<Array<string>>) {}',
errors: [
{
messageId: 'shouldBeReadonly',
column: 14,
endColumn: 47,
},
],
},
// objects
...objects.map<TSESLint.InvalidTestCase<MessageIds, Options>>(type => {
return {
code: `function foo(arg: ${type}) {}`,
errors: [
{
messageId: 'shouldBeReadonly',
column: 14,
endColumn: 19 + type.length,
},
],
};
}),
{
code: `
function foo(arg: {
readonly foo: {
bar: string
}
}) {}
`,
errors: [
{
messageId: 'shouldBeReadonly',
line: 2,
column: 22,
endLine: 6,
endColumn: 10,
},
],
},
// weird intersections
...weirdIntersections.map<TSESLint.InvalidTestCase<MessageIds, Options>>(
baseCode => {
const code = baseCode.replace(/readonly /g, '');
return {
code,
errors: [{ messageId: 'shouldBeReadonly' }],
};
},
),
{
code: `
interface Test extends Array<string> {
readonly property: boolean
}
function foo(arg: Test) {}
`,
errors: [{ messageId: 'shouldBeReadonly' }],
},
{
code: `
interface Test extends Array<string> {
property: boolean
}
function foo(arg: Test) {}
`,
errors: [{ messageId: 'shouldBeReadonly' }],
},
],
});