Skip to content

Commit d62ce3c

Browse files
author
Rebecca Stevens
committed
fix(prefer-readonly-types): Give tuples their own error message.
1 parent 4290ee1 commit d62ce3c

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/rules/prefer-readonly-types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
isTSArrayType,
2424
isTSIndexSignature,
2525
isTSParameterProperty,
26+
isTSTupleType,
2627
isTSTypeOperator
2728
} from "../util/typeguard";
2829

@@ -70,6 +71,7 @@ const defaultOptions: Options = {
7071
// The possible error messages.
7172
const errorMessages = {
7273
array: "Only readonly arrays allowed.",
74+
tuple: "Only readonly tuples allowed.",
7375
implicit: "Implicitly a mutable array. Only readonly arrays allowed.",
7476
property: "A readonly modifier is required."
7577
} as const;
@@ -105,7 +107,7 @@ function checkArrayOrTupleType(
105107
? [
106108
{
107109
node,
108-
messageId: "array",
110+
messageId: isTSTupleType(node) ? "tuple" : "array",
109111
fix:
110112
node.parent && isTSArrayType(node.parent)
111113
? fixer => [

src/util/typeguard.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ export function isTSPropertySignature(
166166
return node.type === AST_NODE_TYPES.TSPropertySignature;
167167
}
168168

169+
export function isTSTupleType(
170+
node: TSESTree.Node
171+
): node is TSESTree.TSTupleType {
172+
return node.type === AST_NODE_TYPES.TSTupleType;
173+
}
174+
169175
export function isTSTypeAliasDeclaration(
170176
node: TSESTree.Node
171177
): node is TSESTree.TSTypeAliasDeclaration {

tests/rules/prefer-readonly-types.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ const invalid: ReadonlyArray<InvalidTestCase> = [
517517
}`,
518518
errors: [
519519
{
520-
messageId: "array",
520+
messageId: "tuple",
521521
type: "TSTupleType",
522522
line: 1,
523523
column: 21
@@ -534,13 +534,13 @@ const invalid: ReadonlyArray<InvalidTestCase> = [
534534
}`,
535535
errors: [
536536
{
537-
messageId: "array",
537+
messageId: "tuple",
538538
type: "TSTupleType",
539539
line: 1,
540540
column: 21
541541
},
542542
{
543-
messageId: "array",
543+
messageId: "tuple",
544544
type: "TSTupleType",
545545
line: 1,
546546
column: 38
@@ -557,7 +557,7 @@ const invalid: ReadonlyArray<InvalidTestCase> = [
557557
}`,
558558
errors: [
559559
{
560-
messageId: "array",
560+
messageId: "tuple",
561561
type: "TSTupleType",
562562
line: 1,
563563
column: 47
@@ -574,7 +574,7 @@ const invalid: ReadonlyArray<InvalidTestCase> = [
574574
}`,
575575
errors: [
576576
{
577-
messageId: "array",
577+
messageId: "tuple",
578578
type: "TSTupleType",
579579
line: 1,
580580
column: 21

0 commit comments

Comments
 (0)