Skip to content

Commit f34e403

Browse files
authored
Merge pull request #180 from bmish/configs-warn
2 parents b26c5b0 + 3f429ed commit f34e403

File tree

5 files changed

+124
-22
lines changed

5 files changed

+124
-22
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ Generated content in a rule doc (everything above the marker comment) (intention
7070

7171
💼 This rule is enabled in the following configs: ✅ `recommended`, 🎨 `stylistic`.
7272

73+
🎨 This rule will _warn_ in the `stylistic` config.
74+
7375
🎨 This rule is _disabled_ in the `stylistic` config.
7476

7577
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).

lib/rule-notices.ts

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ import {
1818
RuleDocTitleFormat,
1919
RULE_DOC_TITLE_FORMAT_DEFAULT,
2020
} from './rule-doc-title-format.js';
21-
import { NOTICE_TYPE, SEVERITY_ERROR, SEVERITY_OFF } from './types.js';
21+
import {
22+
NOTICE_TYPE,
23+
SEVERITY_ERROR,
24+
SEVERITY_OFF,
25+
SEVERITY_WARN,
26+
} from './types.js';
2227

2328
export const NOTICE_TYPE_DEFAULT_PRESENCE_AND_ORDERING: {
2429
[key in NOTICE_TYPE]: boolean;
@@ -43,16 +48,19 @@ const RULE_NOTICES: {
4348
| undefined
4449
| ((data: {
4550
configsEnabled: string[];
51+
configsWarn: string[];
4652
configsDisabled: string[];
4753
configEmojis: ConfigEmojis;
4854
urlConfigs?: string;
4955
replacedBy: readonly string[] | undefined;
5056
type?: RULE_TYPE;
5157
}) => string);
5258
} = {
53-
// Configs notice varies based on whether the rule is enabled in one or more configs.
59+
// Configs notice varies based on whether the rule is configured in one or more configs.
60+
// eslint-disable-next-line complexity
5461
[NOTICE_TYPE.CONFIGS]: ({
5562
configsEnabled,
63+
configsWarn,
5664
configsDisabled,
5765
configEmojis,
5866
urlConfigs,
@@ -66,22 +74,31 @@ const RULE_NOTICES: {
6674
/* istanbul ignore next -- this shouldn't happen */
6775
if (
6876
(!configsEnabled || configsEnabled.length === 0) &&
77+
(!configsWarn || configsWarn.length === 0) &&
6978
(!configsDisabled || configsDisabled.length === 0)
7079
) {
7180
throw new Error(
72-
'Should not be trying to display config notice for rule not enabled/disabled in any configs.'
81+
'Should not be trying to display config notice for rule not configured in any configs.'
7382
);
7483
}
7584

7685
// If one applicable config with an emoji, use the emoji for that config, otherwise use the general config emoji.
7786
let emoji = '';
78-
if (configsEnabled.length + configsDisabled.length > 1) {
87+
if (
88+
configsEnabled.length + configsWarn.length + configsDisabled.length >
89+
1
90+
) {
7991
emoji = EMOJI_CONFIG;
8092
} else if (configsEnabled.length > 0) {
8193
emoji =
8294
configEmojis.find(
8395
(configEmoji) => configEmoji.config === configsEnabled[0]
8496
)?.emoji ?? EMOJI_CONFIG;
97+
} else if (configsWarn.length > 0) {
98+
emoji =
99+
configEmojis.find(
100+
(configEmoji) => configEmoji.config === configsWarn[0]
101+
)?.emoji ?? EMOJI_CONFIG;
85102
} else if (configsDisabled.length > 0) {
86103
emoji =
87104
configEmojis.find(
@@ -99,6 +116,16 @@ const RULE_NOTICES: {
99116
})
100117
.join(', ');
101118

119+
// List of configs that warn for the rule.
120+
const configsWarnCSV = configsWarn
121+
.map((configWarn) => {
122+
const emoji = configEmojis.find(
123+
(configEmoji) => configEmoji.config === configWarn
124+
)?.emoji;
125+
return `${emoji ? `${emoji} ` : ''}\`${configWarn}\``;
126+
})
127+
.join(', ');
128+
102129
// List of configs that disable the rule.
103130
const configsDisabledCSV = configsDisabled
104131
.map((configDisabled) => {
@@ -115,19 +142,27 @@ const RULE_NOTICES: {
115142
? `This rule is enabled in the following ${configsLinkOrWord}: ${configsEnabledCSV}.`
116143
: configsEnabled.length === 1
117144
? `This rule is enabled in the \`${configsEnabled?.[0]}\` ${configLinkOrWord}.`
118-
: '';
145+
: undefined;
146+
147+
// Complete sentence for configs that warn for the rule.
148+
const SENTENCE_WARN =
149+
configsWarn.length > 1
150+
? `This rule will _warn_ in the following ${configsLinkOrWord}: ${configsWarnCSV}.`
151+
: configsWarn.length === 1
152+
? `This rule will _warn_ in the \`${configsWarn?.[0]}\` ${configLinkOrWord}.`
153+
: undefined;
119154

120155
// Complete sentence for configs that disable the rule.
121156
const SENTENCE_DISABLED =
122157
configsDisabled.length > 1
123158
? `This rule is _disabled_ in the following ${configsLinkOrWord}: ${configsDisabledCSV}.`
124159
: configsDisabled.length === 1
125160
? `This rule is _disabled_ in the \`${configsDisabled?.[0]}\` ${configLinkOrWord}.`
126-
: '';
161+
: undefined;
127162

128-
return `${emoji} ${SENTENCE_ENABLED}${
129-
SENTENCE_ENABLED && SENTENCE_DISABLED ? ' ' : '' // Space if two sentences.
130-
}${SENTENCE_DISABLED}`;
163+
return `${emoji} ${[SENTENCE_ENABLED, SENTENCE_WARN, SENTENCE_DISABLED]
164+
.filter((sentence) => sentence !== undefined)
165+
.join(' ')}`;
131166
},
132167

133168
// Deprecated notice has optional "replaced by" rules list.
@@ -170,6 +205,7 @@ function ruleNamesToList(ruleNames: readonly string[]) {
170205
function getNoticesForRule(
171206
rule: RuleModule,
172207
configsEnabled: string[],
208+
configsWarn: string[],
173209
configsDisabled: string[],
174210
ruleDocNotices: NOTICE_TYPE[]
175211
) {
@@ -178,7 +214,9 @@ function getNoticesForRule(
178214
} = {
179215
// Alphabetical order.
180216
[NOTICE_TYPE.CONFIGS]:
181-
configsEnabled.length > 0 || configsDisabled.length > 0,
217+
configsEnabled.length > 0 ||
218+
configsWarn.length > 0 ||
219+
configsDisabled.length > 0,
182220
[NOTICE_TYPE.DEPRECATED]: rule.meta.deprecated || false,
183221

184222
// FIXABLE_AND_HAS_SUGGESTIONS potentially replaces FIXABLE and HAS_SUGGESTIONS.
@@ -239,6 +277,13 @@ function getRuleNoticeLines(
239277
SEVERITY_ERROR
240278
).filter((configName) => !ignoreConfig?.includes(configName));
241279

280+
const configsWarn = getConfigsForRule(
281+
ruleName,
282+
configsToRules,
283+
pluginPrefix,
284+
SEVERITY_WARN
285+
).filter((configName) => !ignoreConfig?.includes(configName));
286+
242287
const configsDisabled = getConfigsForRule(
243288
ruleName,
244289
configsToRules,
@@ -249,6 +294,7 @@ function getRuleNoticeLines(
249294
const notices = getNoticesForRule(
250295
rule,
251296
configsEnabled,
297+
configsWarn,
252298
configsDisabled,
253299
ruleDocNotices
254300
);
@@ -276,6 +322,7 @@ function getRuleNoticeLines(
276322
typeof ruleNoticeStrOrFn === 'function'
277323
? ruleNoticeStrOrFn({
278324
configsEnabled,
325+
configsWarn,
279326
configsDisabled,
280327
configEmojis,
281328
urlConfigs,

lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type Plugin = TSESLint.Linter.Plugin;
1515
// Custom types.
1616

1717
export const SEVERITY_ERROR = new Set<RuleSeverity>([2, 'error']);
18+
export const SEVERITY_WARN = new Set<RuleSeverity>([1, 'warn']);
1819
export const SEVERITY_OFF = new Set<RuleSeverity>([0, 'off']);
1920

2021
export type ConfigsToRules = Record<string, Rules>;

test/lib/__snapshots__/generator-test.ts.snap

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ exports[`generator #generate rule with type, type column not enabled hides the t
571571
"
572572
`;
573573

574-
exports[`generator #generate rules that are disabled generates the documentation 1`] = `
574+
exports[`generator #generate rules that are disabled or set to warn generates the documentation 1`] = `
575575
"## Rules
576576
<!-- begin auto-generated rules list -->
577577
@@ -580,15 +580,18 @@ exports[`generator #generate rules that are disabled generates the documentation
580580
| Name | Description | ✅ |
581581
| :----------------------------- | :--------------------- | :-- |
582582
| [no-bar](docs/rules/no-bar.md) | Description of no-bar. | |
583-
| [no-baz](docs/rules/no-baz.md) | Description of no-bar. | ✅ |
584-
| [no-biz](docs/rules/no-biz.md) | Description of no-bar. | |
583+
| [no-baz](docs/rules/no-baz.md) | Description of no-baz. | ✅ |
584+
| [no-bez](docs/rules/no-bez.md) | Description of no-bez. | |
585+
| [no-biz](docs/rules/no-biz.md) | Description of no-biz. | |
586+
| [no-boz](docs/rules/no-boz.md) | Description of no-boz. | |
587+
| [no-buz](docs/rules/no-buz.md) | Description of no-buz. | |
585588
| [no-foo](docs/rules/no-foo.md) | Description of no-foo. | |
586589
587590
<!-- end auto-generated rules list -->
588591
"
589592
`;
590593

591-
exports[`generator #generate rules that are disabled generates the documentation 2`] = `
594+
exports[`generator #generate rules that are disabled or set to warn generates the documentation 2`] = `
592595
"# Description of no-foo (\`test/no-foo\`)
593596
594597
✅ This rule is _disabled_ in the \`recommended\` config.
@@ -597,7 +600,7 @@ exports[`generator #generate rules that are disabled generates the documentation
597600
"
598601
`;
599602

600-
exports[`generator #generate rules that are disabled generates the documentation 3`] = `
603+
exports[`generator #generate rules that are disabled or set to warn generates the documentation 3`] = `
601604
"# Description of no-bar (\`test/no-bar\`)
602605
603606
💼 This rule is _disabled_ in the following configs: \`other\`, ✅ \`recommended\`.
@@ -606,24 +609,51 @@ exports[`generator #generate rules that are disabled generates the documentation
606609
"
607610
`;
608611

609-
exports[`generator #generate rules that are disabled generates the documentation 4`] = `
610-
"# Description of no-bar (\`test/no-baz\`)
612+
exports[`generator #generate rules that are disabled or set to warn generates the documentation 4`] = `
613+
"# Description of no-baz (\`test/no-baz\`)
611614
612615
💼 This rule is enabled in the \`recommended\` config. This rule is _disabled_ in the \`other\` config.
613616
614617
<!-- end auto-generated rule header -->
615618
"
616619
`;
617620

618-
exports[`generator #generate rules that are disabled generates the documentation 5`] = `
619-
"# Description of no-bar (\`test/no-biz\`)
621+
exports[`generator #generate rules that are disabled or set to warn generates the documentation 5`] = `
622+
"# Description of no-biz (\`test/no-biz\`)
620623
621624
💼 This rule is _disabled_ in the \`other\` config.
622625
623626
<!-- end auto-generated rule header -->
624627
"
625628
`;
626629

630+
exports[`generator #generate rules that are disabled or set to warn generates the documentation 6`] = `
631+
"# Description of no-boz (\`test/no-boz\`)
632+
633+
✅ This rule will _warn_ in the \`recommended\` config.
634+
635+
<!-- end auto-generated rule header -->
636+
"
637+
`;
638+
639+
exports[`generator #generate rules that are disabled or set to warn generates the documentation 7`] = `
640+
"# Description of no-buz (\`test/no-buz\`)
641+
642+
💼 This rule will _warn_ in the following configs: \`other\`, ✅ \`recommended\`.
643+
644+
<!-- end auto-generated rule header -->
645+
"
646+
`;
647+
648+
exports[`generator #generate rules that are disabled or set to warn generates the documentation 8`] = `
649+
"# Description of no-bez (\`test/no-bez\`)
650+
651+
💼 This rule will _warn_ in the \`other\` config.
652+
653+
<!-- end auto-generated rule header -->
654+
"
655+
`;
656+
627657
exports[`generator #generate shows column and notice for requiresTypeChecking updates the documentation 1`] = `
628658
"<!-- begin auto-generated rules list -->
629659

test/lib/generator-test.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,7 +1976,7 @@ describe('generator', function () {
19761976
});
19771977
});
19781978

1979-
describe('rules that are disabled', function () {
1979+
describe('rules that are disabled or set to warn', function () {
19801980
beforeEach(function () {
19811981
mockFs({
19821982
'package.json': JSON.stringify({
@@ -1997,11 +1997,23 @@ describe('generator', function () {
19971997
create(context) {},
19981998
},
19991999
'no-baz': {
2000-
meta: { docs: { description: 'Description of no-bar.' }, },
2000+
meta: { docs: { description: 'Description of no-baz.' }, },
20012001
create(context) {},
20022002
},
20032003
'no-biz': {
2004-
meta: { docs: { description: 'Description of no-bar.' }, },
2004+
meta: { docs: { description: 'Description of no-biz.' }, },
2005+
create(context) {},
2006+
},
2007+
'no-boz': {
2008+
meta: { docs: { description: 'Description of no-boz.' }, },
2009+
create(context) {},
2010+
},
2011+
'no-buz': {
2012+
meta: { docs: { description: 'Description of no-buz.' }, },
2013+
create(context) {},
2014+
},
2015+
'no-bez': {
2016+
meta: { docs: { description: 'Description of no-bez.' }, },
20052017
create(context) {},
20062018
},
20072019
},
@@ -2011,13 +2023,17 @@ describe('generator', function () {
20112023
'test/no-foo': 'off',
20122024
'test/no-bar': 0,
20132025
'test/no-baz': 'error',
2026+
'test/no-boz': 'warn',
2027+
'test/no-buz': 1,
20142028
}
20152029
},
20162030
other: {
20172031
rules: {
20182032
'test/no-bar': 0,
20192033
'test/no-baz': 'off',
20202034
'test/no-biz': 'off',
2035+
'test/no-buz': 'warn',
2036+
'test/no-bez': 'warn',
20212037
}
20222038
},
20232039
}
@@ -2029,6 +2045,9 @@ describe('generator', function () {
20292045
'docs/rules/no-bar.md': '',
20302046
'docs/rules/no-baz.md': '',
20312047
'docs/rules/no-biz.md': '',
2048+
'docs/rules/no-boz.md': '',
2049+
'docs/rules/no-buz.md': '',
2050+
'docs/rules/no-bez.md': '',
20322051

20332052
// Needed for some of the test infrastructure to work.
20342053
node_modules: mockFs.load(
@@ -2049,6 +2068,9 @@ describe('generator', function () {
20492068
expect(readFileSync('docs/rules/no-bar.md', 'utf8')).toMatchSnapshot();
20502069
expect(readFileSync('docs/rules/no-baz.md', 'utf8')).toMatchSnapshot();
20512070
expect(readFileSync('docs/rules/no-biz.md', 'utf8')).toMatchSnapshot();
2071+
expect(readFileSync('docs/rules/no-boz.md', 'utf8')).toMatchSnapshot();
2072+
expect(readFileSync('docs/rules/no-buz.md', 'utf8')).toMatchSnapshot();
2073+
expect(readFileSync('docs/rules/no-bez.md', 'utf8')).toMatchSnapshot();
20522074
});
20532075
});
20542076

0 commit comments

Comments
 (0)