Skip to content

Commit 2988572

Browse files
committed
docs: add rule documentation consistency tests
Adds tests to ensure that our rule docs have notices to indicate whether they are fixable and/or in the recommended config. These notices match the format used by ESLint itself. Example: https://eslint.org/docs/rules/no-extra-semi Also adds tests for various other aspects of rule docs and that each rule has a documentation file and test file.
1 parent 15ffada commit 2988572

18 files changed

+173
-39
lines changed

docs/rules/fixer-return.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Enforces always return from a fixer function (fixer-return)
22

3+
✔️ The `"extends": "plugin:eslint-plugin/recommended"` property in a configuration file enables this rule.
4+
35
In a fixable rule, missing return from a fixer function will not apply fixes.
46

57
## Rule Details

docs/rules/meta-property-ordering.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# enforce ordering of meta properties in rule source (meta-property-ordering)
22

3-
(fixable) The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) automatically fixes problems reported by this rule.
3+
⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) can automatically fix some of the problems reported by this rule.
44

55
This rule enforces that meta properties of a rule are placed in a consistent order.
66

docs/rules/no-deprecated-context-methods.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Disallows usage of deprecated methods on rule context objects (no-deprecated-context-methods)
22

3-
(fixable) The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) automatically fixes problems reported by this rule.
3+
⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) can automatically fix some of the problems reported by this rule.
44

55
This rule disallows the use of deprecated methods on rule `context` objects.
66

docs/rules/no-deprecated-report-api.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# disallow use of the deprecated context.report() API (no-deprecated-report-api)
22

3-
(fixable) The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) automatically fixes problems reported by this rule.
3+
✔️ The `"extends": "plugin:eslint-plugin/recommended"` property in a configuration file enables this rule.
4+
5+
⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) can automatically fix some of the problems reported by this rule.
46

57
ESLint has two APIs that rules can use to report problems. The [deprecated API](http://eslint.org/docs/developer-guide/working-with-rules-deprecated) accepts multiple arguments: `context.report(node, [loc], message)`. The ["new API"](http://eslint.org/docs/developer-guide/working-with-rules#contextreport) accepts a single argument: an object containing information about the reported problem. It is recommended that all rules use the new API.
68

79
## Rule Details
810

911
This rule aims to disallow use of the deprecated `context.report(node, [loc], message)` API.
1012

11-
The following patterns are considered warnings:
13+
Examples of **incorrect** code for this rule:
1214

1315
```js
1416
module.exports = {
@@ -19,7 +21,7 @@ module.exports = {
1921

2022
```
2123

22-
The following patterns are not warnings:
24+
Examples of **correct** code for this rule:
2325

2426
```js
2527
module.exports = {

docs/rules/no-identical-tests.md

+25-23
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
# Disallow identical tests (no-identical-tests)
2-
3-
(fixable) The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) automatically fixes problems reported by this rule.
4-
5-
When a rule has a lot of tests, it's sometimes difficult to tell if any tests are duplicates. This rule would warn if any test cases have the same properties.
6-
7-
## Rule Details
8-
9-
Examples of **incorrect** code for this rule:
10-
11-
```js
1+
# Disallow identical tests (no-identical-tests)
2+
3+
✔️ The `"extends": "plugin:eslint-plugin/recommended"` property in a configuration file enables this rule.
4+
5+
⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) can automatically fix some of the problems reported by this rule.
6+
7+
When a rule has a lot of tests, it's sometimes difficult to tell if any tests are duplicates. This rule would warn if any test cases have the same properties.
8+
9+
## Rule Details
10+
11+
Examples of **incorrect** code for this rule:
12+
13+
```js
1214
/* eslint eslint-plugin/no-identical-tests: error */
1315

1416
new RuleTester().run('foo', bar, {
@@ -17,12 +19,12 @@ new RuleTester().run('foo', bar, {
1719
{ code: 'foo' },
1820
],
1921
invalid: [],
20-
});
21-
```
22-
23-
Examples of **correct** code for this rule:
24-
25-
```js
22+
});
23+
```
24+
25+
Examples of **correct** code for this rule:
26+
27+
```js
2628
/* eslint eslint-plugin/no-identical-tests: error */
2729

2830
new RuleTester().run('foo', bar, {
@@ -31,9 +33,9 @@ new RuleTester().run('foo', bar, {
3133
{ code: 'bar' },
3234
],
3335
invalid: [],
34-
});
35-
```
36-
37-
## When Not To Use It
38-
39-
If you want to allow identical tests, do not enable this rule.
36+
});
37+
```
38+
39+
## When Not To Use It
40+
41+
If you want to allow identical tests, do not enable this rule.

docs/rules/no-missing-placeholders.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Disallow missing placeholders in rule report messages (no-missing-placeholders)
22

3+
✔️ The `"extends": "plugin:eslint-plugin/recommended"` property in a configuration file enables this rule.
4+
35
Report messages in rules can have placeholders surrounded by curly brackets.
46

57
```js

docs/rules/no-unused-placeholders.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Disallow unused placeholders in rule report messages (no-unused-placeholders)
22

3+
✔️ The `"extends": "plugin:eslint-plugin/recommended"` property in a configuration file enables this rule.
4+
35
This rule aims to disallow unused placeholders in rule report messages.
46

57
## Rule Details

docs/rules/no-useless-token-range.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Disallow unnecessary calls to sourceCode.getFirstToken and sourceCode.getLastToken (no-useless-token-range)
22

3+
✔️ The `"extends": "plugin:eslint-plugin/recommended"` property in a configuration file enables this rule.
4+
5+
⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) can automatically fix some of the problems reported by this rule.
6+
37
AST nodes always start and end with tokens. As a result, the start index of the first token in a node is the same as the start index of the node itself, and the end index of the last token in a node is the same as the end index of the node itself. Using code like `sourceCode.getFirstToken(node).range[0]` unnecessarily hurts the performance of your rule, and makes your code less readable.
48

59
## Rule Details

docs/rules/prefer-object-rule.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Disallow rule exports where the export is a function. (prefer-object-rule)
22

3-
(fixable) The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) automatically fixes problems reported by this rule.
3+
⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) can automatically fix some of the problems reported by this rule.
44

55
## Rule Details
66

docs/rules/prefer-output-null.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Disallows invalid RuleTester test cases with the output the same as the code. (prefer-output-null)
22

3-
(fixable) The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) automatically fixes problems reported by this rule.
3+
⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) can automatically fix some of the problems reported by this rule.
44

55
Instead of repeating the test case `code`, using `output: null` is more concise and makes it easier to distinguish whether a test case provides an autofix.
66

docs/rules/report-message-format.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ For example, in order to mandate that all report messages begin with a capital l
2525

2626
Note that since this rule uses static analysis and does not actually run your code, it will attempt to match report messages *before* placeholders are inserted.
2727

28-
The following patterns are considered warnings:
28+
Examples of **incorrect** code for this rule:
2929

3030
```js
3131
/* eslint eslint-plugin/report-message-format: ["error", "^[A-Z].*\\.$"] */
@@ -42,7 +42,7 @@ module.exports = {
4242
};
4343
```
4444

45-
The following patterns are not warnings:
45+
Examples of **correct** code for this rule:
4646

4747
```js
4848
module.exports = {

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# require rules to implement a meta.docs.url property (require-meta-docs-url)
22

3+
⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) can automatically fix some of the problems reported by this rule.
4+
35
A rule can store the URL to its documentation page in `meta.docs.url`. This enables integration tools / IDEs / editors to conveniently provide the link to developers so that they can better understand the rule.
46

57
## Rule Details
@@ -20,7 +22,7 @@ This rule has an option.
2022

2123
If you set the `pattern` option, this rule adds `meta.docs.url` property automatically when you execute `eslint --fix` command.
2224

23-
The following patterns are considered warnings:
25+
Examples of **incorrect** code for this rule:
2426

2527
```js
2628

@@ -63,7 +65,7 @@ module.exports = {
6365

6466
```
6567

66-
The following patterns are not warnings:
68+
Examples of **correct** code for this rule:
6769

6870
```js
6971

docs/rules/require-meta-fixable.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# require rules to implement a meta.fixable property (require-meta-fixable)
22

3+
✔️ The `"extends": "plugin:eslint-plugin/recommended"` property in a configuration file enables this rule.
4+
35
A fixable ESLint rule must have a valid `meta.fixable` property. A rule reports a problem with a `fix()` function but does not export a `meta.fixable` property is likely to cause an unexpected error.
46

57
## Rule Details
68

79
This rule aims to require ESLint rules to have a `meta.fixable` property if necessary.
810

9-
The following patterns are considered warnings:
11+
Examples of **incorrect** code for this rule:
1012

1113
```js
1214
/* eslint eslint-plugin/require-meta-fixable: "error" */
@@ -56,7 +58,7 @@ module.exports = { create (context) {
5658
} };
5759
```
5860

59-
The following patterns are not warnings:
61+
Examples of **correct** code for this rule:
6062

6163
```js
6264
/* eslint eslint-plugin/require-meta-fixable: "error" */

docs/rules/require-meta-has-suggestions.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Likewise, rules that do not report suggestions should not enable the `meta.hasSu
88

99
This rule aims to require ESLint rules to have a `meta.hasSuggestions` property if necessary.
1010

11-
The following patterns are considered warnings:
11+
Examples of **incorrect** code for this rule:
1212

1313
```js
1414
/* eslint eslint-plugin/require-meta-has-suggestions: "error" */
@@ -44,7 +44,7 @@ module.exports = {
4444
};
4545
```
4646

47-
The following patterns are not warnings:
47+
Examples of **correct** code for this rule:
4848

4949
```js
5050
/* eslint eslint-plugin/require-meta-has-suggestions: "error" */

docs/rules/require-meta-schema.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# require rules to implement a meta.schema property (require-meta-schema)
22

3+
⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) can automatically fix some of the problems reported by this rule.
4+
35
Defining a schema for each rule allows eslint to validate that configuration options are passed correctly. Even when there are no options for a rule, a schema should still be defined (as an empty array) so that eslint can validate that no data is passed to the rule.
46

57
## Rule Details

docs/rules/test-case-property-ordering.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# enforce ordering of keys in test cases (test-case-property-ordering)
22

3-
(fixable) The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) automatically fixes problems reported by this rule.
3+
⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) can automatically fix some of the problems reported by this rule.
44

55
This rule enforces that the properties of RuleTester test cases are arranged in a consistent order.
66

docs/rules/test-case-shorthand-strings.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Enforce consistent usage of shorthand strings for test cases with no options (test-case-shorthand-strings)
22

3+
⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) can automatically fix some of the problems reported by this rule.
4+
35
When writing valid test cases for rules with `RuleTester`, one can optionally include a string as a test case instead of an object, if the the test case does not use any options.
46

57
```js

tests/lib/rule-setup.js

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
'use strict';
2+
3+
const { readdirSync, readFileSync } = require('fs');
4+
const path = require('path');
5+
const assert = require('chai').assert;
6+
const plugin = require('../..');
7+
8+
const RULE_NAMES = Object.keys(plugin.rules);
9+
const RULE_NAMES_RECOMMENDED = new Set(Object.keys(plugin.configs.recommended.rules));
10+
11+
const MESSAGES = {
12+
fixable:
13+
'⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) can automatically fix some of the problems reported by this rule.',
14+
configRecommended:
15+
'✔️ The `"extends": "plugin:eslint-plugin/recommended"` property in a configuration file enables this rule.',
16+
};
17+
18+
describe('rule setup is correct', () => {
19+
it('should have a list of exported rules and rules directory that match', () => {
20+
const filePath = path.join(__dirname, '..', 'lib', 'rules');
21+
const files = readdirSync(filePath);
22+
23+
assert.deepStrictEqual(
24+
RULE_NAMES,
25+
files
26+
.filter(file => !file.startsWith('.'))
27+
.map(file => file.replace('.js', ''))
28+
);
29+
});
30+
31+
it('should have tests for all rules', () => {
32+
const filePath = path.join(__dirname, 'rules');
33+
const files = readdirSync(filePath);
34+
35+
assert.deepStrictEqual(
36+
RULE_NAMES,
37+
files
38+
.filter(file => !file.startsWith('.'))
39+
.map(file => file.replace('.js', ''))
40+
);
41+
});
42+
43+
it('should have documentation for all rules', () => {
44+
const filePath = path.join(__dirname, '..', '..', 'docs', 'rules');
45+
const files = readdirSync(filePath);
46+
47+
assert.deepStrictEqual(
48+
RULE_NAMES,
49+
files
50+
.filter(file => !file.startsWith('.'))
51+
.map(file => file.replace('.md', ''))
52+
);
53+
});
54+
55+
describe('rule documentation files', () => {
56+
for (const ruleName of RULE_NAMES) {
57+
const rule = plugin.rules[ruleName];
58+
const filePath = path.join(
59+
__dirname,
60+
'..',
61+
'..',
62+
'docs',
63+
'rules',
64+
`${ruleName}.md`
65+
);
66+
const fileContents = readFileSync(filePath, 'utf8');
67+
const lines = fileContents.split('\n');
68+
69+
describe(ruleName, () => {
70+
it('should have the right contents (title, notices, etc)', () => {
71+
// Title
72+
assert.ok(lines[0].endsWith(`(${ruleName})`), 'first line ends with rule name');
73+
assert.strictEqual(lines[1], '', 'second line is blank');
74+
75+
// Rule Details
76+
assert.ok(fileContents.includes('## Rule Details'), 'includes "## Rule Details" header');
77+
78+
// Examples
79+
assert.ok(fileContents.includes('Examples of **incorrect** code for this rule'), 'includes incorrect examples');
80+
assert.ok(fileContents.includes('Examples of **correct** code for this rule'), 'includes correct examples');
81+
82+
// Decide which notices should be shown at the top of the doc.
83+
const expectedNotices = [];
84+
const unexpectedNotices = [];
85+
if (RULE_NAMES_RECOMMENDED.has('eslint-plugin/' + ruleName)) {
86+
expectedNotices.push('configRecommended');
87+
} else {
88+
unexpectedNotices.push('configRecommended');
89+
}
90+
if (rule.meta.fixable) {
91+
expectedNotices.push('fixable');
92+
} else {
93+
unexpectedNotices.push('fixable');
94+
}
95+
96+
// Ensure that expected notices are present in the correct order.
97+
let currentLineNumber = 1;
98+
for (const expectedNotice of expectedNotices) {
99+
assert.strictEqual(lines[currentLineNumber], '');
100+
assert.strictEqual(lines[currentLineNumber + 1], MESSAGES[expectedNotice]);
101+
currentLineNumber += 2;
102+
}
103+
104+
// Ensure that unexpected notices are not present.
105+
for (const unexpectedNotice of unexpectedNotices) {
106+
assert.ok(!fileContents.includes(MESSAGES[unexpectedNotice]), 'does not include notice: ' + MESSAGES[unexpectedNotice]);
107+
}
108+
});
109+
});
110+
}
111+
});
112+
});

0 commit comments

Comments
 (0)