Skip to content

Commit

Permalink
refactor(prefer-readonly-type): give error message ids more explicit …
Browse files Browse the repository at this point in the history
…names
  • Loading branch information
RebeccaStevens committed Aug 12, 2021
1 parent f5f3930 commit c20d6df
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 84 deletions.
21 changes: 11 additions & 10 deletions src/rules/prefer-readonly-type.ts
Expand Up @@ -80,11 +80,10 @@ const defaultOptions: Options = {

// The possible error messages.
const errorMessages = {
array: "Only readonly arrays allowed.",
implicit: "Implicitly a mutable array. Only readonly arrays allowed.",
property: "A readonly modifier is required.",
tuple: "Only readonly tuples allowed.",
type: "Only readonly types allowed.",
arrayShouldBeReadonly: "Array should be readonly.",
propertyShouldBeReadonly: "This property should be readonly.",
tupleShouldBeReadonly: "Tuple should be readonly.",
typeShouldBeReadonly: "Type should be readonly.",
} as const;

// The meta data for this rule.
Expand Down Expand Up @@ -137,7 +136,9 @@ function checkArrayOrTupleType(
? [
{
node,
messageId: isTSTupleType(node) ? "tuple" : "array",
messageId: isTSTupleType(node)
? "tupleShouldBeReadonly"
: "arrayShouldBeReadonly",
fix:
node.parent !== undefined && isTSArrayType(node.parent)
? (fixer) => [
Expand Down Expand Up @@ -166,7 +167,7 @@ function checkMappedType(
: [
{
node,
messageId: "property",
messageId: "propertyShouldBeReadonly",
fix: (fixer) =>
fixer.insertTextBeforeRange(
[node.range[0] + 1, node.range[1]],
Expand Down Expand Up @@ -205,7 +206,7 @@ function checkTypeReference(
? [
{
node,
messageId: "type",
messageId: "typeShouldBeReadonly",
fix: (fixer) => fixer.replaceText(node.typeName, immutableType),
},
]
Expand Down Expand Up @@ -238,7 +239,7 @@ function checkProperty(
? [
{
node,
messageId: "property",
messageId: "propertyShouldBeReadonly",
fix:
isTSIndexSignature(node) || isTSPropertySignature(node)
? (fixer) => fixer.insertTextBefore(node, "readonly ")
Expand Down Expand Up @@ -303,7 +304,7 @@ function checkForImplicitMutableArray(
? [
{
node: declarator.node,
messageId: "implicit",
messageId: "arrayShouldBeReadonly",
fix: (fixer) =>
fixer.insertTextAfter(declarator.id, ": readonly unknown[]"),
},
Expand Down

0 comments on commit c20d6df

Please sign in to comment.