-
Notifications
You must be signed in to change notification settings - Fork 4
/
no-unsafe-readonly-mutable-assignment.test.ts
633 lines (614 loc) · 16.1 KB
/
no-unsafe-readonly-mutable-assignment.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
import rule from "./no-unsafe-readonly-mutable-assignment";
import { AST_NODE_TYPES } from "@typescript-eslint/utils";
import { ESLintUtils } from "@typescript-eslint/utils";
// eslint-disable-next-line functional/prefer-immutable-types
const ruleTester = new ESLintUtils.RuleTester({
parserOptions: {
sourceType: "module",
project: "./tsconfig.tests.json",
},
parser: "@typescript-eslint/parser",
});
// eslint-disable-next-line functional/no-expression-statements
ruleTester.run("no-unsafe-readonly-mutable-assignment", rule, {
valid: [
/**
* Call expressions
*/
// zero parameters
{
filename: "file.ts",
code: `
const foo = () => {
return undefined;
};
foo();
`,
},
// zero parameters with extra argument (TypeScript will catch this so we don't flag it)
{
filename: "file.ts",
code: `
const foo = () => {
return undefined;
};
foo("");
`,
},
// non-object parameter
{
filename: "file.ts",
code: `
const foo = (a: string) => {
return undefined;
};
foo("a");
`,
},
// missing arguments (TypeScript will catch this so we don't flag it)
{
filename: "file.ts",
code: `
const foo = (a: string) => {
return undefined;
};
foo();
`,
},
// readonly -> readonly (type doesn't change)
{
filename: "file.ts",
code: `
type ReadonlyA = { readonly a: string };
const func = (param: ReadonlyA): void => {
return undefined;
};
const readonlyA: ReadonlyA = { a: "" };
func(readonlyA);
`,
},
// readonly -> readonly (nested object; type doesn't change)
{
filename: "file.ts",
code: `
type ReadonlyA = { readonly a: { readonly b: string } };
const func = (param: ReadonlyA): void => {
return undefined;
};
const readonlyA: ReadonlyA = { a: { b: "" } };
func(readonlyA);
`,
},
// mutable -> mutable (type doesn't change)
{
filename: "file.ts",
code: `
type MutableA = {a: string};
const foo = (mut: MutableA) => {
mut.a = "whoops";
};
const mut: MutableA = { a: "" };
foo(mut);
`,
},
// object literal -> mutable (no reference to object retained)
{
filename: "file.ts",
code: `
type MutableA = {a: string};
const foo = (mut: MutableA) => {
mut.a = "whoops";
};
foo({ a: "" });
`,
},
// object literal -> mutable (mutable reference to property retained)
{
filename: "file.ts",
code: `
type MutableB = { b: string };
type MutableA = { readonly a: MutableB };
const func = (param: MutableA): void => {
return undefined;
};
const b: MutableB = { b: "" };
func({ a: b });
`,
},
// object literal -> readonly (mutable reference to property retained)
{
filename: "file.ts",
code: `
type MutableB = { b: string };
type ReadonlyA = { readonly a: { readonly b: string } };
const func = (param: ReadonlyA): void => {
return undefined;
};
const b: MutableB = { b: "" };
func({ a: b });
`,
},
// object literal -> readonly (readonly reference to property retained)
{
filename: "file.ts",
code: `
type ReadonlyB = { readonly b: string };
type ReadonlyA = { readonly a: ReadonlyB };
const func = (param: ReadonlyA): void => {
return undefined;
};
const b: ReadonlyB = { b: "" };
func({ a: b });
`,
},
// object literal -> readonly (no reference to object or its property retained)
{
filename: "file.ts",
code: `
type ReadonlyB = { readonly b: string };
type ReadonlyA = { readonly a: ReadonlyB };
const func = (param: ReadonlyA): void => {
return undefined;
};
func({ a: { b: "" } });
`,
},
// mutable (union) -> mutable
{
filename: "file.ts",
code: `
type MutableA = {a: string};
const foo = (mut: MutableA) => {
mut.a = "whoops";
};
const mut: MutableA | number = { a: "" };
foo(mut);
`,
},
// mutable -> mutable (union)
{
filename: "file.ts",
code: `
type MutableA = {a: string};
const foo = (mut: MutableA | number): void => {
return;
};
const mut: MutableA = { a: "" };
foo(mut);
`,
},
// multiple type signatures (readonly -> readonly)
{
filename: "file.ts",
code: `
type ReadonlyA = { readonly a: string };
export function func(a: number): number;
export function func(a: ReadonlyA): ReadonlyA;
export function func(a: any): any {
return a;
}
const readonlyA: ReadonlyA = { a: "" };
func(readonlyA);
`,
},
// multiple type signatures (no matching signature)
// we don't bother flagging this because TypeScript itself will catch it
{
filename: "file.ts",
code: `
type ReadonlyA = { readonly a: string };
export function func(a: number): number;
export function func(a: string): string;
export function func(a: any): any {
return a;
}
const readonlyA: ReadonlyA = { a: "" };
func(readonlyA);
`,
},
// readonly array concat.
{
filename: "file.ts",
code: `
const arr: ReadonlyArray<never> = [];
const foo = arr.concat(arr, arr);
`,
},
// mutable array concat.
{
filename: "file.ts",
code: `
const arr: Array<never> = [];
const foo = arr.concat(arr, arr);
`,
},
// Mixed mutable and readonly array concat.
// TODO this should be invalid.
{
filename: "file.ts",
code: `
const ro: ReadonlyArray<never> = [];
const mut: Array<never> = [];
const foo = ro.concat(ro, mut);
`,
},
// mixed (union) -> mixed (union)
// The readonlys align and mutables align, so no surprising mutation can arise.
{
filename: "file.ts",
code: `
type MutableA = { a: string };
type ReadonlyB = { readonly b: string };
const func = (foo: MutableA | ReadonlyB): void => {
return;
};
const foo: MutableA | ReadonlyB = Date.now() > 0 ? { a: "" } : { b: "" };
func(foo);
`,
},
// Recursive type (linting must terminate)
{
filename: "file.ts",
code: `
type Foo = ReadonlyArray<Foo>;
const func = (foo: Foo): void => {
return;
};
const foo: Foo = [[]];
func(foo);
`,
},
{
filename: "file.ts",
code: `
const foo = document.createElement("div");
`,
},
// readonly array of readonly object -> readonly array of readonly object
{
filename: "file.ts",
code: `
type Obj = { readonly foo: string };
const foo = (a: ReadonlyArray<Obj>): number => a.length;
const arr: ReadonlyArray<Obj> = [];
foo(arr);
`,
},
/**
* Assignment expressions
*/
// TODO
/**
* Return statement
*/
// mutable -> mutable (function return)
{
filename: "file.ts",
code: `
type MutableA = { a: string };
type ReadonlyA = { readonly a: string };
function foo(): MutableA {
const ma: MutableA = { a: "" };
return ma;
}
`,
},
// readonly -> readonly (function return)
{
filename: "file.ts",
code: `
type MutableA = { a: string };
type ReadonlyA = { readonly a: string };
function foo(): ReadonlyA {
const ma: ReadonlyA = { a: "" };
return ma;
}
`,
},
// void (function return)
{
filename: "file.ts",
code: `
function foo(): void {
return;
}
`,
},
// https://github.com/danielnixon/eslint-plugin-total-functions/issues/577
{
filename: "file.ts",
code: `
const arr : readonly number[] = [];
const obj : {} = arr;
const it1 : Iterable<number> = arr;
const it2 : Readonly<Iterable<number>> = arr;
`,
},
// multiple call signatures, recursive
// TODO fix stack overflow properly instead of just terminating iteration arbitrarily
{
filename: "file.ts",
code: `
interface Foo<A extends object> {
<B extends {}>(b: Foo<B> & { b: Foo<B> }): Foo<A & B>;
<C extends Foo<any>>(b: Foo<C> & { b: Foo<C> }): Foo<A & C>;
}
declare const a: Foo<{ a: string }>;
const b = a as Foo<{ b: string }>;
`,
},
{
filename: "file.ts",
code: `
interface Foo<A extends object> {
<C extends Foo<any>>(b: Foo<C> & { b: Foo<C> }): Foo<A & C>;
}
declare const a: Foo<{ a: string }>;
const b = a as Foo<{ b: string }>;
`,
},
{
filename: "file.ts",
code: `
declare const foo: string;
const bar: string | { a: string } = foo;
`,
},
{
filename: "file.ts",
code: `
const a: readonly string[] = ["a"] as const;
`,
},
// Literal (immutable) assigned to mutable but safe because no surprising mutation can arise
// see https://github.com/danielnixon/eslint-plugin-total-functions/issues/754
{
filename: "file.ts",
code: `
const o: Record<string, string> = {};
`,
},
],
invalid: [
// object literal -> mutable (readonly reference to property retained)
// this can lead to surprising mutation in the readonly reference that is retained
{
filename: "file.ts",
code: `
type MutableB = { b: string };
type ReadonlyB = { readonly b: string };
type MutableA = { readonly a: MutableB };
const func = (param: MutableA): void => {
return undefined;
};
const b: ReadonlyB = { b: "" };
func({ a: b } as const);
`,
errors: [
{
messageId: "errorStringGeneric",
type: AST_NODE_TYPES.TSAsExpression,
},
],
},
// readonly -> mutable (rest parameter)
{
filename: "file.ts",
code: `
type MutableA = { a: string };
type ReadonlyA = { readonly a: string };
const foo = (...as: readonly MutableA[]): void => {
return;
};
const ma: MutableA = { a: "" };
const ra: ReadonlyA = { a: "" };
foo(ma, ra);
`,
errors: [
{
messageId: "errorStringGeneric",
type: AST_NODE_TYPES.Identifier,
},
],
},
// readonly (union) -> mutable (union)
{
filename: "file.ts",
code: `
type MutableA = { a: string };
type ReadonlyA = { readonly a: string };
type MutableB = { b: string };
type ReadonlyB = { readonly b: string };
const mutate = (mut: MutableA | MutableB): void => {
return;
};
const ro: ReadonlyA | ReadonlyB = { a: "" };
mutate(ro);
`,
errors: [
{
messageId: "errorStringGeneric",
type: AST_NODE_TYPES.Identifier,
},
],
},
// readonly (union) -> mixed (union)
{
filename: "file.ts",
code: `
type ReadonlyA = { readonly a: string };
type MutableB = { b: string };
type ReadonlyB = { readonly b: string };
const mutate = (mut: ReadonlyA | MutableB): void => {
return;
};
const ro: ReadonlyA | ReadonlyB = Date.now() > 0 ? { a: "" } : { b: "" };
mutate(ro);
`,
errors: [
{
messageId: "errorStringGeneric",
type: AST_NODE_TYPES.Identifier,
},
],
},
/**
* Assignment expressions
*/
// readonly -> mutable
{
filename: "file.ts",
code: `
type MutableA = { a: string };
type ReadonlyA = { readonly a: string };
const readonlyA: ReadonlyA = { a: "readonly?" };
let mutableA: MutableA;
mutableA = readonlyA;
`,
errors: [
{
messageId: "errorStringGeneric",
type: AST_NODE_TYPES.AssignmentExpression,
},
],
},
// readonly -> mutable (short-circuiting assignment)
{
filename: "file.ts",
code: `
type MutableA = { a: string };
type ReadonlyA = { readonly a: string };
const readonlyA: ReadonlyA = { a: "readonly?" };
let mutableA: MutableA | undefined;
mutableA ??= readonlyA;
`,
errors: [
{
messageId: "errorStringGeneric",
type: AST_NODE_TYPES.AssignmentExpression,
},
],
},
/**
* Variable declaration
*/
// readonly (type) -> mutable
{
filename: "file.ts",
code: `
type MutableA = { a: string };
type ReadonlyA = { readonly a: string };
const readonlyA: ReadonlyA = { a: "readonly?" };
const mutableA: MutableA = readonlyA;
`,
errors: [
{
messageId: "errorStringGeneric",
type: AST_NODE_TYPES.VariableDeclaration,
},
],
},
// readonly (class) -> mutable
// this is arguably worse than the above because instead of surprise mutation it results in a TypeError
{
filename: "file.ts",
code: `
class Box {
get area(): number {
return 42;
}
}
type Area = {
area: number;
};
const a: Area = new Box();
`,
errors: [
{
messageId: "errorStringGeneric",
type: AST_NODE_TYPES.VariableDeclaration,
},
],
},
// TODO
// is-immutable-type is (correctly) infering that both MutableA
// and ReadonlyA are mutable, so sadly this isn't being flagged.
// readonly (string index type) -> mutable (string index type)
// {
// filename: "file.ts",
// code: `
// type MutableA = Record<string, { a: string }>;
// type ReadonlyA = Record<string, { readonly a: string }>;
// declare const readonlyA: ReadonlyA;
// const mutableA: MutableA = readonlyA;
// `,
// errors: [
// {
// messageId: "errorStringGeneric",
// type: AST_NODE_TYPES.VariableDeclaration,
// },
// ],
// },
// readonly (string index signature) -> mutable (string index signature) (recursive types)
{
filename: "file.ts",
code: `
type MutableA = {
[P in string]: MutableA;
};
type ReadonlyA = {
readonly [P in string]: ReadonlyA;
};
const readonlyA: ReadonlyA = {};
const mutableA: MutableA = readonlyA;
`,
errors: [
{
messageId: "errorStringGeneric",
type: AST_NODE_TYPES.VariableDeclaration,
},
],
},
// readonly (number index signature) -> mutable (number index signature) (recursive types)
{
filename: "file.ts",
code: `
type MutableA = {
[P in number]: MutableA;
};
type ReadonlyA = {
readonly [P in number]: ReadonlyA;
};
const readonlyA: ReadonlyA = {};
const mutableA: MutableA = readonlyA;
`,
errors: [
{
messageId: "errorStringGeneric",
type: AST_NODE_TYPES.VariableDeclaration,
},
],
},
/**
* Return statement
*/
// readonly -> mutable (function return)
{
filename: "file.ts",
code: `
type MutableA = { a: string };
type ReadonlyA = { readonly a: string };
function foo(): MutableA {
const ma: ReadonlyA = { a: "" };
return ma;
}
`,
errors: [
{
messageId: "errorStringGeneric",
type: AST_NODE_TYPES.ReturnStatement,
},
],
},
],
} as const);