Skip to content

Commit bfe6f03

Browse files
test(prefer-readonly-type): update rule test for new behavior
re #153
1 parent 4882d42 commit bfe6f03

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> = [];`,
@@ -644,34 +562,6 @@ const tests: ReadonlyArray<InvalidTestCase> = [
644562
},
645563
],
646564
},
647-
// Type literal in property template parameter without readonly should produce failures.
648-
{
649-
code: dedent`
650-
type foo = ReadonlyArray<{
651-
type: string,
652-
code: string,
653-
}>;`,
654-
optionsSet: [[]],
655-
output: dedent`
656-
type foo = ReadonlyArray<{
657-
readonly type: string,
658-
readonly code: string,
659-
}>;`,
660-
errors: [
661-
{
662-
messageId: "property",
663-
type: "TSPropertySignature",
664-
line: 2,
665-
column: 3,
666-
},
667-
{
668-
messageId: "property",
669-
type: "TSPropertySignature",
670-
line: 3,
671-
column: 3,
672-
},
673-
],
674-
},
675565
// Type literal without readonly on members should produce failures.
676566
// Also verify that nested members are checked.
677567
{
@@ -1050,6 +940,84 @@ const tests: ReadonlyArray<InvalidTestCase> = [
1050940
},
1051941
],
1052942
},
943+
// Should fail when using an mutable type alias in variable declaration.
944+
{
945+
code: dedent`
946+
type Foo = Array<string>;
947+
let foo: Foo;`,
948+
optionsSet: [[]],
949+
errors: [
950+
{
951+
messageId: "type",
952+
type: "TSTypeReference",
953+
line: 2,
954+
column: 20,
955+
},
956+
],
957+
},
958+
// Should fail when using an mutable type alias in function param.
959+
{
960+
code: dedent`
961+
type Foo = Array<string>;
962+
function foo(param: Foo) {}`,
963+
optionsSet: [[]],
964+
errors: [
965+
{
966+
messageId: "type",
967+
type: "TSTypeReference",
968+
line: 2,
969+
column: 20,
970+
},
971+
],
972+
},
973+
// Should fail when using an mutable type alias of type literal without readonly modifer in variable declaration.
974+
{
975+
code: dedent`
976+
type Foo = ReadonlyArray<{
977+
type: string,
978+
code: string,
979+
}>;
980+
let foo: Foo;`,
981+
optionsSet: [[]],
982+
errors: [
983+
{
984+
messageId: "property",
985+
type: "TSPropertySignature",
986+
line: 2,
987+
column: 3,
988+
},
989+
{
990+
messageId: "property",
991+
type: "TSPropertySignature",
992+
line: 3,
993+
column: 3,
994+
},
995+
],
996+
},
997+
// Should fail when using an mutable type alias of type literal without readonly modifer in function param.
998+
{
999+
code: dedent`
1000+
type Foo = ReadonlyArray<{
1001+
type: string,
1002+
code: string,
1003+
}>;
1004+
function foo(param: Foo) {}`,
1005+
optionsSet: [[]],
1006+
errors: [
1007+
{
1008+
messageId: "property",
1009+
type: "TSPropertySignature",
1010+
line: 2,
1011+
column: 3,
1012+
},
1013+
{
1014+
messageId: "property",
1015+
type: "TSPropertySignature",
1016+
line: 3,
1017+
column: 3,
1018+
},
1019+
],
1020+
},
10531021
];
10541022

10551023
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
},
@@ -206,7 +206,7 @@ const tests: ReadonlyArray<ValidTestCase> = [
206206
},
207207
// Type literal in array template parameter with readonly should not produce failures.
208208
{
209-
code: `type foo = ReadonlyArray<{ readonly type: string, readonly code: string }>;`,
209+
code: `let foo: ReadonlyArray<{ readonly type: string, readonly code: string }>;`,
210210
optionsSet: [[]],
211211
},
212212
// Type literal with readonly on members should not produce failures.
@@ -340,7 +340,7 @@ const tests: ReadonlyArray<ValidTestCase> = [
340340
},
341341
// Ignore Mutable Collections (Array, Tuple, Set, Map)
342342
{
343-
code: dedent`type Foo = Array<string>;`,
343+
code: dedent`let Foo: Array<string>;`,
344344
optionsSet: [[{ ignoreCollections: true }]],
345345
},
346346
{
@@ -352,7 +352,7 @@ const tests: ReadonlyArray<ValidTestCase> = [
352352
optionsSet: [[{ ignoreCollections: true, checkImplicit: true }]],
353353
},
354354
{
355-
code: dedent`type Foo = [string, string];`,
355+
code: dedent`let Foo: [string, string];`,
356356
optionsSet: [[{ ignoreCollections: true }]],
357357
},
358358
{
@@ -364,15 +364,15 @@ const tests: ReadonlyArray<ValidTestCase> = [
364364
optionsSet: [[{ ignoreCollections: true, checkImplicit: true }]],
365365
},
366366
{
367-
code: dedent`type Foo = Set<string, string>;`,
367+
code: dedent`let Foo: Set<string, string>;`,
368368
optionsSet: [[{ ignoreCollections: true }]],
369369
},
370370
{
371371
code: dedent`const Foo: Set<string, string> = new Set();`,
372372
optionsSet: [[{ ignoreCollections: true }]],
373373
},
374374
{
375-
code: dedent`type Foo = Map<string, string>;`,
375+
code: dedent`let Foo: Map<string, string>;`,
376376
optionsSet: [[{ ignoreCollections: true }]],
377377
},
378378
{

0 commit comments

Comments
 (0)