Skip to content

Commit

Permalink
test(prefer-readonly-type): update rule test for new behavior
Browse files Browse the repository at this point in the history
re #153
  • Loading branch information
RebeccaStevens committed Jul 31, 2021
1 parent 3eedeba commit 2c747d2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 119 deletions.
188 changes: 78 additions & 110 deletions tests/rules/prefer-readonly-type/ts/invalid.ts
Expand Up @@ -314,88 +314,6 @@ const tests: ReadonlyArray<InvalidTestCase> = [
},
],
},
// Should fail on Array type alias.
{
code: `type Foo = Array<string>;`,
optionsSet: [[]],
output: `type Foo = ReadonlyArray<string>;`,
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<string>
}
}`,
optionsSet: [[]],
output: dedent`
function foo() {
type Foo = {
readonly bar: ReadonlyArray<string>
}
}`,
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<string>;
}`,
optionsSet: [[]],
output: dedent`
function foo() {
type Foo = ReadonlyArray<string>;
}`,
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<string>
}
}`,
optionsSet: [[]],
output: dedent`
function foo() {
type Foo = {
readonly bar: ReadonlyArray<string>
}
}`,
errors: [
{
messageId: "type",
type: "TSTypeReference",
line: 3,
column: 19,
},
],
},
// Should fail on Array type in variable declaration.
{
code: `const foo: Array<string> = [];`,
Expand Down Expand Up @@ -632,34 +550,6 @@ const tests: ReadonlyArray<InvalidTestCase> = [
},
],
},
// 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.
{
Expand Down Expand Up @@ -1088,6 +978,84 @@ const tests: ReadonlyArray<InvalidTestCase> = [
},
],
},
// Should fail when using an mutable type alias in variable declaration.
{
code: dedent`
type Foo = Array<string>;
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<string>;
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;
18 changes: 9 additions & 9 deletions tests/rules/prefer-readonly-type/ts/valid.ts
Expand Up @@ -38,16 +38,16 @@ const tests: ReadonlyArray<ValidTestCase> = [
}`,
optionsSet: [[]],
},
// Should not fail on ReadonlyArray type alias.
// Should not fail on type alias.
{
code: `type Foo = ReadonlyArray<string>;`,
code: `type Foo = Array<string>;`,
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<string>;
type Foo = Array<string>;
}`,
optionsSet: [[]],
},
Expand Down Expand Up @@ -214,7 +214,7 @@ const tests: ReadonlyArray<ValidTestCase> = [
},
// 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.
Expand Down Expand Up @@ -348,7 +348,7 @@ const tests: ReadonlyArray<ValidTestCase> = [
},
// Ignore Mutable Collections (Array, Tuple, Set, Map)
{
code: dedent`type Foo = Array<string>;`,
code: dedent`let Foo: Array<string>;`,
optionsSet: [[{ ignoreCollections: true }]],
},
{
Expand All @@ -360,7 +360,7 @@ const tests: ReadonlyArray<ValidTestCase> = [
optionsSet: [[{ ignoreCollections: true, checkImplicit: true }]],
},
{
code: dedent`type Foo = [string, string];`,
code: dedent`let Foo: [string, string];`,
optionsSet: [[{ ignoreCollections: true }]],
},
{
Expand All @@ -372,15 +372,15 @@ const tests: ReadonlyArray<ValidTestCase> = [
optionsSet: [[{ ignoreCollections: true, checkImplicit: true }]],
},
{
code: dedent`type Foo = Set<string, string>;`,
code: dedent`let Foo: Set<string, string>;`,
optionsSet: [[{ ignoreCollections: true }]],
},
{
code: dedent`const Foo: Set<string, string> = new Set();`,
optionsSet: [[{ ignoreCollections: true }]],
},
{
code: dedent`type Foo = Map<string, string>;`,
code: dedent`let Foo: Map<string, string>;`,
optionsSet: [[{ ignoreCollections: true }]],
},
{
Expand Down

0 comments on commit 2c747d2

Please sign in to comment.