Skip to content

Commit 74eb08d

Browse files
test(prefer-readonly-type): update rule test for new behavior
re #153
1 parent bd0a8d2 commit 74eb08d

File tree

2 files changed

+87
-119
lines changed

2 files changed

+87
-119
lines changed

tests/rules/prefer-readonly-type/ts/invalid.ts

Lines changed: 78 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -314,88 +314,6 @@ const tests: ReadonlyArray<InvalidTestCase> = [
314314
},
315315
],
316316
},
317-
// Should fail on Array type alias.
318-
{
319-
code: `type Foo = Array<string>;`,
320-
optionsSet: [[]],
321-
output: `type Foo = ReadonlyArray<string>;`,
322-
errors: [
323-
{
324-
messageId: "type",
325-
type: "TSTypeReference",
326-
line: 1,
327-
column: 12,
328-
},
329-
],
330-
},
331-
// Should fail on Array as type member.
332-
{
333-
code: dedent`
334-
function foo() {
335-
type Foo = {
336-
readonly bar: Array<string>
337-
}
338-
}`,
339-
optionsSet: [[]],
340-
output: dedent`
341-
function foo() {
342-
type Foo = {
343-
readonly bar: ReadonlyArray<string>
344-
}
345-
}`,
346-
errors: [
347-
{
348-
messageId: "type",
349-
type: "TSTypeReference",
350-
line: 3,
351-
column: 19,
352-
},
353-
],
354-
},
355-
// Should fail on Array type alias in local type.
356-
{
357-
code: dedent`
358-
function foo() {
359-
type Foo = Array<string>;
360-
}`,
361-
optionsSet: [[]],
362-
output: dedent`
363-
function foo() {
364-
type Foo = ReadonlyArray<string>;
365-
}`,
366-
errors: [
367-
{
368-
messageId: "type",
369-
type: "TSTypeReference",
370-
line: 2,
371-
column: 14,
372-
},
373-
],
374-
},
375-
// Should fail on Array as type member in local type.
376-
{
377-
code: dedent`
378-
function foo() {
379-
type Foo = {
380-
readonly bar: Array<string>
381-
}
382-
}`,
383-
optionsSet: [[]],
384-
output: dedent`
385-
function foo() {
386-
type Foo = {
387-
readonly bar: ReadonlyArray<string>
388-
}
389-
}`,
390-
errors: [
391-
{
392-
messageId: "type",
393-
type: "TSTypeReference",
394-
line: 3,
395-
column: 19,
396-
},
397-
],
398-
},
399317
// Should fail on Array type in variable declaration.
400318
{
401319
code: `const foo: Array<string> = [];`,
@@ -632,34 +550,6 @@ const tests: ReadonlyArray<InvalidTestCase> = [
632550
},
633551
],
634552
},
635-
// Type literal in property template parameter without readonly should produce failures.
636-
{
637-
code: dedent`
638-
type foo = ReadonlyArray<{
639-
type: string,
640-
code: string,
641-
}>;`,
642-
optionsSet: [[]],
643-
output: dedent`
644-
type foo = ReadonlyArray<{
645-
readonly type: string,
646-
readonly code: string,
647-
}>;`,
648-
errors: [
649-
{
650-
messageId: "property",
651-
type: "TSPropertySignature",
652-
line: 2,
653-
column: 3,
654-
},
655-
{
656-
messageId: "property",
657-
type: "TSPropertySignature",
658-
line: 3,
659-
column: 3,
660-
},
661-
],
662-
},
663553
// Type literal without readonly on members should produce failures.
664554
// Also verify that nested members are checked.
665555
{
@@ -1088,6 +978,84 @@ const tests: ReadonlyArray<InvalidTestCase> = [
1088978
},
1089979
],
1090980
},
981+
// Should fail when using an mutable type alias in variable declaration.
982+
{
983+
code: dedent`
984+
type Foo = Array<string>;
985+
let foo: Foo;`,
986+
optionsSet: [[]],
987+
errors: [
988+
{
989+
messageId: "type",
990+
type: "TSTypeReference",
991+
line: 2,
992+
column: 20,
993+
},
994+
],
995+
},
996+
// Should fail when using an mutable type alias in function param.
997+
{
998+
code: dedent`
999+
type Foo = Array<string>;
1000+
function foo(param: Foo) {}`,
1001+
optionsSet: [[]],
1002+
errors: [
1003+
{
1004+
messageId: "type",
1005+
type: "TSTypeReference",
1006+
line: 2,
1007+
column: 20,
1008+
},
1009+
],
1010+
},
1011+
// Should fail when using an mutable type alias of type literal without readonly modifer in variable declaration.
1012+
{
1013+
code: dedent`
1014+
type Foo = ReadonlyArray<{
1015+
type: string,
1016+
code: string,
1017+
}>;
1018+
let foo: Foo;`,
1019+
optionsSet: [[]],
1020+
errors: [
1021+
{
1022+
messageId: "property",
1023+
type: "TSPropertySignature",
1024+
line: 2,
1025+
column: 3,
1026+
},
1027+
{
1028+
messageId: "property",
1029+
type: "TSPropertySignature",
1030+
line: 3,
1031+
column: 3,
1032+
},
1033+
],
1034+
},
1035+
// Should fail when using an mutable type alias of type literal without readonly modifer in function param.
1036+
{
1037+
code: dedent`
1038+
type Foo = ReadonlyArray<{
1039+
type: string,
1040+
code: string,
1041+
}>;
1042+
function foo(param: Foo) {}`,
1043+
optionsSet: [[]],
1044+
errors: [
1045+
{
1046+
messageId: "property",
1047+
type: "TSPropertySignature",
1048+
line: 2,
1049+
column: 3,
1050+
},
1051+
{
1052+
messageId: "property",
1053+
type: "TSPropertySignature",
1054+
line: 3,
1055+
column: 3,
1056+
},
1057+
],
1058+
},
10911059
];
10921060

10931061
export default tests;

tests/rules/prefer-readonly-type/ts/valid.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ const tests: ReadonlyArray<ValidTestCase> = [
3838
}`,
3939
optionsSet: [[]],
4040
},
41-
// Should not fail on ReadonlyArray type alias.
41+
// Should not fail on type alias.
4242
{
43-
code: `type Foo = ReadonlyArray<string>;`,
43+
code: `type Foo = Array<string>;`,
4444
optionsSet: [[]],
4545
},
46-
// Should not fail on ReadonlyArray type alias in local type.
46+
// Should not fail on type alias in local type.
4747
{
4848
code: dedent`
4949
function foo() {
50-
type Foo = ReadonlyArray<string>;
50+
type Foo = Array<string>;
5151
}`,
5252
optionsSet: [[]],
5353
},
@@ -214,7 +214,7 @@ const tests: ReadonlyArray<ValidTestCase> = [
214214
},
215215
// Type literal in array template parameter with readonly should not produce failures.
216216
{
217-
code: `type foo = ReadonlyArray<{ readonly type: string, readonly code: string }>;`,
217+
code: `let foo: ReadonlyArray<{ readonly type: string, readonly code: string }>;`,
218218
optionsSet: [[]],
219219
},
220220
// Type literal with readonly on members should not produce failures.
@@ -348,7 +348,7 @@ const tests: ReadonlyArray<ValidTestCase> = [
348348
},
349349
// Ignore Mutable Collections (Array, Tuple, Set, Map)
350350
{
351-
code: dedent`type Foo = Array<string>;`,
351+
code: dedent`let Foo: Array<string>;`,
352352
optionsSet: [[{ ignoreCollections: true }]],
353353
},
354354
{
@@ -360,7 +360,7 @@ const tests: ReadonlyArray<ValidTestCase> = [
360360
optionsSet: [[{ ignoreCollections: true, checkImplicit: true }]],
361361
},
362362
{
363-
code: dedent`type Foo = [string, string];`,
363+
code: dedent`let Foo: [string, string];`,
364364
optionsSet: [[{ ignoreCollections: true }]],
365365
},
366366
{
@@ -372,15 +372,15 @@ const tests: ReadonlyArray<ValidTestCase> = [
372372
optionsSet: [[{ ignoreCollections: true, checkImplicit: true }]],
373373
},
374374
{
375-
code: dedent`type Foo = Set<string, string>;`,
375+
code: dedent`let Foo: Set<string, string>;`,
376376
optionsSet: [[{ ignoreCollections: true }]],
377377
},
378378
{
379379
code: dedent`const Foo: Set<string, string> = new Set();`,
380380
optionsSet: [[{ ignoreCollections: true }]],
381381
},
382382
{
383-
code: dedent`type Foo = Map<string, string>;`,
383+
code: dedent`let Foo: Map<string, string>;`,
384384
optionsSet: [[{ ignoreCollections: true }]],
385385
},
386386
{

0 commit comments

Comments
 (0)