Skip to content

Commit 138bb90

Browse files
authored
docs: ensure rule docs mention all rule options (#226)
1 parent 57d4b5e commit 138bb90

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

docs/rules/require-meta-docs-url.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,6 @@ A rule can store the URL to its documentation page in `meta.docs.url`. This enab
88

99
This rule aims to require ESLint rules to have a `meta.docs.url` property.
1010

11-
This rule has an option.
12-
13-
```json
14-
{
15-
"eslint-plugin/require-meta-docs-url": ["error", {
16-
"pattern": "https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/{{name}}.md"
17-
}]
18-
}
19-
```
20-
21-
- `pattern` (`string`) ... A pattern to enforce rule's document URL. It replaces `{{name}}` placeholder by each rule name. The rule name is the basename of each rule file. Default is `undefined` which allows any URL.
22-
23-
If you set the `pattern` option, this rule adds `meta.docs.url` property automatically when you execute `eslint --fix` command.
24-
2511
Examples of **incorrect** code for this rule:
2612

2713
```js
@@ -97,6 +83,22 @@ module.exports = {
9783

9884
```
9985

86+
## Options
87+
88+
This rule has an option.
89+
90+
```json
91+
{
92+
"eslint-plugin/require-meta-docs-url": ["error", {
93+
"pattern": "https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/{{name}}.md"
94+
}]
95+
}
96+
```
97+
98+
- `pattern` (`string`) ... A pattern to enforce rule's document URL. It replaces `{{name}}` placeholder by each rule name. The rule name is the basename of each rule file. Default is `undefined` which allows any URL.
99+
100+
If you set the `pattern` option, this rule adds `meta.docs.url` property automatically when you execute `eslint --fix` command.
101+
100102
## Version specific URL
101103

102104
If you want to enforce version-specific URLs, it's feasible easily with `.eslintrc.js` and `npm version <type>` script.

tests/lib/rule-setup.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@ function capitalizeFirstLetter (string) {
2424
return string.charAt(0).toUpperCase() + string.slice(1);
2525
}
2626

27+
/**
28+
* Get list of named options from a JSON schema (used for rule schemas).
29+
* @param {Object|Array} jsonSchema - the JSON schema to check
30+
* @returns {String[]} list of named options
31+
*/
32+
function getAllNamedOptions (jsonSchema) {
33+
if (!jsonSchema) {
34+
return [];
35+
}
36+
37+
if (Array.isArray(jsonSchema)) {
38+
return jsonSchema.flatMap(item => getAllNamedOptions(item));
39+
}
40+
41+
if (jsonSchema.items) {
42+
return getAllNamedOptions(jsonSchema.items);
43+
}
44+
45+
if (jsonSchema.properties) {
46+
return Object.keys(jsonSchema.properties);
47+
}
48+
49+
return [];
50+
}
51+
2752
describe('rule setup is correct', () => {
2853
it('should have a list of exported rules and rules directory that match', () => {
2954
const filePath = path.join(__dirname, '..', 'lib', 'rules');
@@ -131,6 +156,24 @@ describe('rule setup is correct', () => {
131156
for (const unexpectedNotice of unexpectedNotices) {
132157
assert.ok(!fileContents.includes(MESSAGES[unexpectedNotice]), 'does not include notice: ' + MESSAGES[unexpectedNotice]);
133158
}
159+
160+
// Check if the rule has configuration options.
161+
if (
162+
(Array.isArray(rule.meta.schema) && rule.meta.schema.length > 0) ||
163+
(typeof rule.meta.schema === 'object' && Object.keys(rule.meta.schema).length > 0)
164+
) {
165+
// Should have a configuration section header:
166+
assert.ok(fileContents.includes('## Options'), 'Should have an "## Options" section');
167+
168+
// Ensure all configuration options are mentioned.
169+
for (const namedOption of getAllNamedOptions(rule.meta.schema)) {
170+
assert.ok(fileContents.includes(namedOption), 'Should mention the `' + namedOption + '` option');
171+
}
172+
} else {
173+
// Should NOT have any options/config section headers:
174+
assert.notOk(fileContents.includes('# Options'), 'Should not have an "Options" section');
175+
assert.notOk(fileContents.includes('# Config'), 'Should not have a "Config" section');
176+
}
134177
});
135178
});
136179
}

0 commit comments

Comments
 (0)