Skip to content

Commit 1675710

Browse files
authored
Merge pull request #67 from bmish/rule-severity-array
2 parents 6bbbbf1 + d1b5ebc commit 1675710

File tree

4 files changed

+166
-2
lines changed

4 files changed

+166
-2
lines changed

lib/configs.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export function hasCustomConfigs(plugin: Plugin) {
77
);
88
}
99

10+
const SEVERITY_ENABLED = new Set([2, 'error']);
11+
1012
/**
1113
* Get config names that a given rule belongs to.
1214
*/
@@ -20,7 +22,13 @@ export function getConfigsForRule(
2022
for (const configName in configsToRules) {
2123
const rules = configsToRules[configName];
2224
const value = rules[`${pluginPrefix}/${ruleName}`];
23-
const isEnabled = [2, 'error'].includes(value);
25+
const isEnabled =
26+
((typeof value === 'string' || typeof value === 'number') &&
27+
SEVERITY_ENABLED.has(value)) ||
28+
(typeof value === 'object' &&
29+
Array.isArray(value) &&
30+
value.length > 0 &&
31+
SEVERITY_ENABLED.has(value[0]));
2432

2533
if (isEnabled) {
2634
configNames.push(configName);

lib/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ export type RuleModule = TSESLint.RuleModule<string, unknown[]> & {
44
meta: Required<Pick<TSESLint.RuleMetaData<string>, 'docs'>>;
55
};
66

7-
export type Rules = Record<string, string | number>;
7+
type RuleSeverity = 'off' | 'error' | 'warn' | 0 | 1 | 2;
8+
9+
export type Rules = Record<string, RuleSeverity | [RuleSeverity, unknown]>;
810

911
export type Config = {
1012
extends?: string[];

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

+48
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,27 @@ exports[`generator #generate only a \`recommended\` config updates the documenta
316316
"
317317
`;
318318

319+
exports[`generator #generate rule config with options generates the documentation 1`] = `
320+
"## Rules
321+
<!-- begin rules list -->
322+
323+
| Rule | Description | ✅ | 🔧 | 💡 |
324+
| ------------------------------ | ---------------------- | --- | --- | --- |
325+
| [no-foo](docs/rules/no-foo.md) | Description of no-foo. | ✅ | | |
326+
327+
<!-- end rules list -->
328+
"
329+
`;
330+
331+
exports[`generator #generate rule config with options generates the documentation 2`] = `
332+
"# Description of no-foo (\`test/no-foo\`)
333+
334+
✅ This rule is enabled in the \`recommended\` config.
335+
336+
<!-- end rule header -->
337+
"
338+
`;
339+
319340
exports[`generator #generate rule doc without header marker but pre-existing header updates the documentation 1`] = `
320341
"# Description (\`test/no-foo\`)
321342
@@ -327,6 +348,33 @@ Pre-existing notice about the rule being recommended.
327348
Details."
328349
`;
329350

351+
exports[`generator #generate rules that are disabled generates the documentation 1`] = `
352+
"## Rules
353+
<!-- begin rules list -->
354+
355+
| Rule | Description | ✅ | 🔧 | 💡 |
356+
| ------------------------------ | ---------------------- | --- | --- | --- |
357+
| [no-bar](docs/rules/no-bar.md) | Description of no-bar. | | | |
358+
| [no-foo](docs/rules/no-foo.md) | Description of no-foo. | | | |
359+
360+
<!-- end rules list -->
361+
"
362+
`;
363+
364+
exports[`generator #generate rules that are disabled generates the documentation 2`] = `
365+
"# Description of no-foo (\`test/no-foo\`)
366+
367+
<!-- end rule header -->
368+
"
369+
`;
370+
371+
exports[`generator #generate rules that are disabled generates the documentation 3`] = `
372+
"# Description of no-bar (\`test/no-bar\`)
373+
374+
<!-- end rule header -->
375+
"
376+
`;
377+
330378
exports[`generator #generate successful updates the documentation 1`] = `
331379
"# eslint-plugin-test
332380
Description.

test/lib/generator-test.ts

+106
Original file line numberDiff line numberDiff line change
@@ -1481,5 +1481,111 @@ describe('generator', function () {
14811481
expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot();
14821482
});
14831483
});
1484+
1485+
describe('rule config with options', function () {
1486+
beforeEach(function () {
1487+
mockFs({
1488+
'package.json': JSON.stringify({
1489+
name: 'eslint-plugin-test',
1490+
main: 'index.js',
1491+
type: 'module',
1492+
}),
1493+
1494+
'index.js': `
1495+
export default {
1496+
rules: {
1497+
'no-foo': {
1498+
meta: { docs: { description: 'Description of no-foo.' }, },
1499+
create(context) {},
1500+
schema: [{ /* some options */ }]
1501+
},
1502+
},
1503+
configs: {
1504+
recommended: {
1505+
rules: {
1506+
'test/no-foo': ['error', { /* some options */ }],
1507+
}
1508+
},
1509+
}
1510+
};`,
1511+
1512+
'README.md': '## Rules\n',
1513+
1514+
'docs/rules/no-foo.md': '',
1515+
1516+
// Needed for some of the test infrastructure to work.
1517+
node_modules: mockFs.load(
1518+
resolve(__dirname, '..', '..', 'node_modules')
1519+
),
1520+
});
1521+
});
1522+
1523+
afterEach(function () {
1524+
mockFs.restore();
1525+
jest.resetModules();
1526+
});
1527+
1528+
it('generates the documentation', async function () {
1529+
await generate('.');
1530+
expect(readFileSync('README.md', 'utf8')).toMatchSnapshot();
1531+
expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot();
1532+
});
1533+
});
1534+
1535+
describe('rules that are disabled', function () {
1536+
beforeEach(function () {
1537+
mockFs({
1538+
'package.json': JSON.stringify({
1539+
name: 'eslint-plugin-test',
1540+
main: 'index.js',
1541+
type: 'module',
1542+
}),
1543+
1544+
'index.js': `
1545+
export default {
1546+
rules: {
1547+
'no-foo': {
1548+
meta: { docs: { description: 'Description of no-foo.' }, },
1549+
create(context) {},
1550+
},
1551+
'no-bar': {
1552+
meta: { docs: { description: 'Description of no-bar.' }, },
1553+
create(context) {},
1554+
},
1555+
},
1556+
configs: {
1557+
recommended: {
1558+
rules: {
1559+
'test/no-foo': 'off',
1560+
'test/no-bar': 0,
1561+
}
1562+
},
1563+
}
1564+
};`,
1565+
1566+
'README.md': '## Rules\n',
1567+
1568+
'docs/rules/no-foo.md': '',
1569+
'docs/rules/no-bar.md': '',
1570+
1571+
// Needed for some of the test infrastructure to work.
1572+
node_modules: mockFs.load(
1573+
resolve(__dirname, '..', '..', 'node_modules')
1574+
),
1575+
});
1576+
});
1577+
1578+
afterEach(function () {
1579+
mockFs.restore();
1580+
jest.resetModules();
1581+
});
1582+
1583+
it('generates the documentation', async function () {
1584+
await generate('.');
1585+
expect(readFileSync('README.md', 'utf8')).toMatchSnapshot();
1586+
expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot();
1587+
expect(readFileSync('docs/rules/no-bar.md', 'utf8')).toMatchSnapshot();
1588+
});
1589+
});
14841590
});
14851591
});

0 commit comments

Comments
 (0)