From 71ca205950d11b0613c543dac7d536d469da537a Mon Sep 17 00:00:00 2001 From: Rebecca Stevens Date: Mon, 26 Jul 2021 08:08:13 +1200 Subject: [PATCH] test(prefer-readonly-type): update rule test for new behavior re #153 --- .../rules/prefer-readonly-type/ts/invalid.ts | 188 ++++++++---------- tests/rules/prefer-readonly-type/ts/valid.ts | 18 +- 2 files changed, 87 insertions(+), 119 deletions(-) diff --git a/tests/rules/prefer-readonly-type/ts/invalid.ts b/tests/rules/prefer-readonly-type/ts/invalid.ts index dedae2fd0..e770fa6bd 100644 --- a/tests/rules/prefer-readonly-type/ts/invalid.ts +++ b/tests/rules/prefer-readonly-type/ts/invalid.ts @@ -314,88 +314,6 @@ const tests: ReadonlyArray = [ }, ], }, - // Should fail on Array type alias. - { - code: `type Foo = Array;`, - optionsSet: [[]], - output: `type Foo = ReadonlyArray;`, - errors: [ - { - messageId: "type", - type: "TSTypeReference", - line: 1, - column: 12, - }, - ], - }, - // Should fail on Array as type member. - { - code: dedent` - function foo() { - type Foo = { - readonly bar: Array - } - }`, - optionsSet: [[]], - output: dedent` - function foo() { - type Foo = { - readonly bar: ReadonlyArray - } - }`, - errors: [ - { - messageId: "type", - type: "TSTypeReference", - line: 3, - column: 19, - }, - ], - }, - // Should fail on Array type alias in local type. - { - code: dedent` - function foo() { - type Foo = Array; - }`, - optionsSet: [[]], - output: dedent` - function foo() { - type Foo = ReadonlyArray; - }`, - errors: [ - { - messageId: "type", - type: "TSTypeReference", - line: 2, - column: 14, - }, - ], - }, - // Should fail on Array as type member in local type. - { - code: dedent` - function foo() { - type Foo = { - readonly bar: Array - } - }`, - optionsSet: [[]], - output: dedent` - function foo() { - type Foo = { - readonly bar: ReadonlyArray - } - }`, - errors: [ - { - messageId: "type", - type: "TSTypeReference", - line: 3, - column: 19, - }, - ], - }, // Should fail on Array type in variable declaration. { code: `const foo: Array = [];`, @@ -632,34 +550,6 @@ const tests: ReadonlyArray = [ }, ], }, - // Type literal in property template parameter without readonly should produce failures. - { - code: dedent` - type foo = ReadonlyArray<{ - type: string, - code: string, - }>;`, - optionsSet: [[]], - output: dedent` - type foo = ReadonlyArray<{ - readonly type: string, - readonly code: string, - }>;`, - errors: [ - { - messageId: "property", - type: "TSPropertySignature", - line: 2, - column: 3, - }, - { - messageId: "property", - type: "TSPropertySignature", - line: 3, - column: 3, - }, - ], - }, // Type literal without readonly on members should produce failures. // Also verify that nested members are checked. { @@ -1088,6 +978,84 @@ const tests: ReadonlyArray = [ }, ], }, + // Should fail when using an mutable type alias in variable declaration. + { + code: dedent` + type Foo = Array; + let foo: Foo;`, + optionsSet: [[]], + errors: [ + { + messageId: "type", + type: "TSTypeReference", + line: 2, + column: 20, + }, + ], + }, + // Should fail when using an mutable type alias in function param. + { + code: dedent` + type Foo = Array; + function foo(param: Foo) {}`, + optionsSet: [[]], + errors: [ + { + messageId: "type", + type: "TSTypeReference", + line: 2, + column: 20, + }, + ], + }, + // Should fail when using an mutable type alias of type literal without readonly modifer in variable declaration. + { + code: dedent` + type Foo = ReadonlyArray<{ + type: string, + code: string, + }>; + let foo: Foo;`, + optionsSet: [[]], + errors: [ + { + messageId: "property", + type: "TSPropertySignature", + line: 2, + column: 3, + }, + { + messageId: "property", + type: "TSPropertySignature", + line: 3, + column: 3, + }, + ], + }, + // Should fail when using an mutable type alias of type literal without readonly modifer in function param. + { + code: dedent` + type Foo = ReadonlyArray<{ + type: string, + code: string, + }>; + function foo(param: Foo) {}`, + optionsSet: [[]], + errors: [ + { + messageId: "property", + type: "TSPropertySignature", + line: 2, + column: 3, + }, + { + messageId: "property", + type: "TSPropertySignature", + line: 3, + column: 3, + }, + ], + }, ]; export default tests; diff --git a/tests/rules/prefer-readonly-type/ts/valid.ts b/tests/rules/prefer-readonly-type/ts/valid.ts index a6613133a..f6c8723fe 100644 --- a/tests/rules/prefer-readonly-type/ts/valid.ts +++ b/tests/rules/prefer-readonly-type/ts/valid.ts @@ -38,16 +38,16 @@ const tests: ReadonlyArray = [ }`, optionsSet: [[]], }, - // Should not fail on ReadonlyArray type alias. + // Should not fail on type alias. { - code: `type Foo = ReadonlyArray;`, + code: `type Foo = Array;`, optionsSet: [[]], }, - // Should not fail on ReadonlyArray type alias in local type. + // Should not fail on type alias in local type. { code: dedent` function foo() { - type Foo = ReadonlyArray; + type Foo = Array; }`, optionsSet: [[]], }, @@ -214,7 +214,7 @@ const tests: ReadonlyArray = [ }, // Type literal in array template parameter with readonly should not produce failures. { - code: `type foo = ReadonlyArray<{ readonly type: string, readonly code: string }>;`, + code: `let foo: ReadonlyArray<{ readonly type: string, readonly code: string }>;`, optionsSet: [[]], }, // Type literal with readonly on members should not produce failures. @@ -348,7 +348,7 @@ const tests: ReadonlyArray = [ }, // Ignore Mutable Collections (Array, Tuple, Set, Map) { - code: dedent`type Foo = Array;`, + code: dedent`let Foo: Array;`, optionsSet: [[{ ignoreCollections: true }]], }, { @@ -360,7 +360,7 @@ const tests: ReadonlyArray = [ optionsSet: [[{ ignoreCollections: true, checkImplicit: true }]], }, { - code: dedent`type Foo = [string, string];`, + code: dedent`let Foo: [string, string];`, optionsSet: [[{ ignoreCollections: true }]], }, { @@ -372,7 +372,7 @@ const tests: ReadonlyArray = [ optionsSet: [[{ ignoreCollections: true, checkImplicit: true }]], }, { - code: dedent`type Foo = Set;`, + code: dedent`let Foo: Set;`, optionsSet: [[{ ignoreCollections: true }]], }, { @@ -380,7 +380,7 @@ const tests: ReadonlyArray = [ optionsSet: [[{ ignoreCollections: true }]], }, { - code: dedent`type Foo = Map;`, + code: dedent`let Foo: Map;`, optionsSet: [[{ ignoreCollections: true }]], }, {