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 30, 2021
1 parent 000a219 commit 0ae146a
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
Original file line number Diff line number Diff line change
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 @@ -644,34 +562,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 @@ -1078,6 +968,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
Original file line number Diff line number Diff line change
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 @@ -206,7 +206,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 @@ -340,7 +340,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 @@ -352,7 +352,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 @@ -364,15 +364,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 0ae146a

Please sign in to comment.