Skip to content

Commit

Permalink
feat(type-declaration-immutability): allow defining custom suggestion…
Browse files Browse the repository at this point in the history
… messages
  • Loading branch information
RebeccaStevens committed Jan 14, 2025
1 parent 59194b2 commit 037a3c7
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/rules/type-declaration-immutability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ export enum RuleEnforcementComparator {
type FixerConfigRaw = {
pattern: string;
replace: string;
message?: string;
};

type FixerConfig = {
pattern: RegExp;
replace: string;
message?: string;
};

type SuggestionsConfig = FixerConfig[];
Expand Down Expand Up @@ -188,6 +190,7 @@ const errorMessages = {
Exactly: 'This type is declare to have an immutability of exactly "{{ expected }}" (actual: "{{ actual }}").',
AtMost: 'This type is declare to have an immutability of at most "{{ expected }}" (actual: "{{ actual }}").',
More: 'This type is declare to have an immutability more than "{{ expected }}" (actual: "{{ actual }}").',
userDefined: "{{ message }}",
} as const;

/**
Expand Down Expand Up @@ -313,7 +316,6 @@ function getConfiguredSuggestions<T extends TSESTree.Node>(
node: T,
context: Readonly<RuleContext<keyof typeof errorMessages, RawOptions>>,
configs: ReadonlyArray<FixerConfig>,
messageId: keyof typeof errorMessages,
): NonNullable<Descriptor["suggest"]> | null {
const text = context.sourceCode.getText(node);
const matchingConfig = configs.filter((c) => c.pattern.test(text));
Expand All @@ -322,7 +324,10 @@ function getConfiguredSuggestions<T extends TSESTree.Node>(
}
return matchingConfig.map((config) => ({
fix: (fixer) => fixer.replaceText(node, text.replace(config.pattern, config.replace)),
messageId,
messageId: "userDefined",
data: {
message: config.message ?? `Replace with: ${text.replace(config.pattern, config.replace)}`,
},
}));
}

Expand Down Expand Up @@ -376,7 +381,7 @@ function getResults(
const suggest =
rule.suggestions === false || isTSInterfaceDeclaration(node)
? null
: getConfiguredSuggestions(node.typeAnnotation, context, rule.suggestions, messageId);
: getConfiguredSuggestions(node.typeAnnotation, context, rule.suggestions);

return {
context,
Expand Down

0 comments on commit 037a3c7

Please sign in to comment.