Skip to content

Commit 10c1b06

Browse files
authored
Merge pull request #127 from bmish/refactor-notice-use-col-type
2 parents d645efb + 835cd3a commit 10c1b06

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

lib/rule-notices.ts

+31-24
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,15 @@ import {
1717
RuleDocTitleFormat,
1818
RULE_DOC_TITLE_FORMAT_DEFAULT,
1919
} from './rule-doc-title-format.js';
20-
21-
enum MESSAGE_TYPE {
22-
CONFIGS = 'configs',
23-
DEPRECATED = 'deprecated',
24-
FIXABLE = 'fixable',
25-
HAS_SUGGESTIONS = 'hasSuggestions',
26-
REQUIRES_TYPE_CHECKING = 'requiresTypeChecking',
27-
}
20+
import { COLUMN_TYPE } from './rule-list-columns.js';
2821

2922
/**
3023
* An object containing the text for each notice type (as a string or function to generate the string).
3124
*/
3225
const RULE_NOTICES: {
33-
[key in MESSAGE_TYPE]:
26+
[key in COLUMN_TYPE]:
3427
| string
28+
| undefined
3529
| ((data: {
3630
configsEnabled: string[];
3731
configEmojis: ConfigEmojis;
@@ -40,7 +34,7 @@ const RULE_NOTICES: {
4034
}) => string);
4135
} = {
4236
// Configs notice varies based on whether the rule is enabled in one or more configs.
43-
[MESSAGE_TYPE.CONFIGS]: ({
37+
[COLUMN_TYPE.CONFIGS]: ({
4438
configsEnabled,
4539
configEmojis,
4640
urlConfigs,
@@ -84,7 +78,7 @@ const RULE_NOTICES: {
8478
},
8579

8680
// Deprecated notice has optional "replaced by" rules list.
87-
[MESSAGE_TYPE.DEPRECATED]: ({
81+
[COLUMN_TYPE.DEPRECATED]: ({
8882
replacedBy,
8983
}: {
9084
replacedBy?: readonly string[] | undefined;
@@ -96,9 +90,13 @@ const RULE_NOTICES: {
9690
}`,
9791

9892
// Simple strings.
99-
[MESSAGE_TYPE.FIXABLE]: `${EMOJI_FIXABLE} This rule is automatically fixable by the [\`--fix\` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).`,
100-
[MESSAGE_TYPE.HAS_SUGGESTIONS]: `${EMOJI_HAS_SUGGESTIONS} This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).`,
101-
[MESSAGE_TYPE.REQUIRES_TYPE_CHECKING]: `${EMOJI_REQUIRES_TYPE_CHECKING} This rule requires type information.`,
93+
[COLUMN_TYPE.FIXABLE]: `${EMOJI_FIXABLE} This rule is automatically fixable by the [\`--fix\` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).`,
94+
[COLUMN_TYPE.HAS_SUGGESTIONS]: `${EMOJI_HAS_SUGGESTIONS} This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).`,
95+
[COLUMN_TYPE.REQUIRES_TYPE_CHECKING]: `${EMOJI_REQUIRES_TYPE_CHECKING} This rule requires type information.`,
96+
97+
// No notice for these.
98+
[COLUMN_TYPE.DESCRIPTION]: undefined,
99+
[COLUMN_TYPE.NAME]: undefined,
102100
};
103101

104102
/**
@@ -115,13 +113,15 @@ function ruleNamesToList(ruleNames: readonly string[]) {
115113
*/
116114
function getNoticesForRule(rule: RuleModule, configsEnabled: string[]) {
117115
const notices: {
118-
[key in MESSAGE_TYPE]: boolean;
116+
[key in COLUMN_TYPE]: boolean;
119117
} = {
120-
[MESSAGE_TYPE.CONFIGS]: configsEnabled.length > 0,
121-
[MESSAGE_TYPE.DEPRECATED]: rule.meta.deprecated || false,
122-
[MESSAGE_TYPE.FIXABLE]: Boolean(rule.meta.fixable),
123-
[MESSAGE_TYPE.HAS_SUGGESTIONS]: rule.meta.hasSuggestions || false,
124-
[MESSAGE_TYPE.REQUIRES_TYPE_CHECKING]:
118+
[COLUMN_TYPE.CONFIGS]: configsEnabled.length > 0,
119+
[COLUMN_TYPE.DEPRECATED]: rule.meta.deprecated || false,
120+
[COLUMN_TYPE.DESCRIPTION]: false, // No notice for this column.
121+
[COLUMN_TYPE.FIXABLE]: Boolean(rule.meta.fixable),
122+
[COLUMN_TYPE.HAS_SUGGESTIONS]: rule.meta.hasSuggestions || false,
123+
[COLUMN_TYPE.NAME]: false, // No notice for this column.
124+
[COLUMN_TYPE.REQUIRES_TYPE_CHECKING]:
125125
rule.meta.docs?.requiresTypeChecking || false,
126126
};
127127

@@ -161,10 +161,10 @@ function getRuleNoticeLines(
161161
pluginPrefix
162162
).filter((config) => !ignoreConfig?.includes(config));
163163
const notices = getNoticesForRule(rule, configsEnabled);
164-
let messageType: keyof typeof notices;
164+
let noticeType: keyof typeof notices;
165165

166-
for (messageType in notices) {
167-
const expected = notices[messageType];
166+
for (noticeType in notices) {
167+
const expected = notices[noticeType];
168168

169169
if (!expected) {
170170
// This notice should not be included.
@@ -173,7 +173,14 @@ function getRuleNoticeLines(
173173

174174
lines.push(''); // Blank line first.
175175

176-
const ruleNoticeStrOrFn = RULE_NOTICES[messageType];
176+
const ruleNoticeStrOrFn = RULE_NOTICES[noticeType];
177+
178+
/* istanbul ignore next -- this won't happen since we would have already bailed out earlier. */
179+
if (!ruleNoticeStrOrFn) {
180+
// No notice for this column.
181+
continue;
182+
}
183+
177184
lines.push(
178185
typeof ruleNoticeStrOrFn === 'function'
179186
? ruleNoticeStrOrFn({

0 commit comments

Comments
 (0)