Skip to content

Commit 9f98ac4

Browse files
authored
New: rule prefer-output-null (#20)
* New: rule prefer-output-null * update: check output property is the same as the code property. * Docs: prefer-output-null * update: check output property is the same as the code property. * Fix: linting errors. * Fix: some small suggestions.
1 parent 4cec353 commit 9f98ac4

File tree

5 files changed

+151
-21
lines changed

5 files changed

+151
-21
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Name | ✔️ | 🛠 | Description
5353
[no-identical-tests](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-identical-tests.md) | | 🛠 | Disallows identical tests
5454
[no-missing-placeholders](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-missing-placeholders.md) | ✔️ | | Disallows missing placeholders in rule report messages
5555
[no-useless-token-range](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-useless-token-range.md) | ✔️ | 🛠 | Disallows unnecessary calls to sourceCode.getFirstToken and sourceCode.getLastToken
56+
[prefer-output-null](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/prefer-output-null.md) | | | Disallows invalid RuleTester test cases with the output the same as the code.
5657
[prefer-placeholders](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/prefer-placeholders.md) | | | Disallows template literals as report messages
5758
[report-message-format](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/report-message-format.md) | | | Enforces a consistent format for report messages
5859
[require-meta-fixable](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-fixable.md) | ✔️ | | Requires a `meta.fixable` property for fixable rules

docs/rules/prefer-output-null.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Disallows invalid RuleTester test cases with the output the same as the code. (prefer-output-null)
2+
3+
## Rule Details
4+
5+
The rule reports an error if it encounters a test case where the output is the same as the code.
6+
7+
Examples of **incorrect** code for this rule:
8+
9+
```js
10+
/* eslint eslint-plugin/prefer-output-null: error */
11+
12+
new RuleTester().run('foo', bar, {
13+
valid: [],
14+
invalid: [
15+
{ code: 'foo', output: 'foo', errors: [{ message: 'bar' }] },
16+
]
17+
});
18+
```
19+
20+
Examples of **correct** code for this rule:
21+
22+
```js
23+
/* eslint eslint-plugin/prefer-output-null: error */
24+
25+
new RuleTester().run('foo', bar, {
26+
valid: [],
27+
invalid: [
28+
{ code: 'foo', output: null, errors: [{ message: 'bar' }] },
29+
]
30+
});
31+
```

lib/rules/prefer-output-null.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @fileoverview disallows invalid RuleTester test cases with the output the same as the code.
3+
* @author 薛定谔的猫<[email protected]>
4+
*/
5+
6+
'use strict';
7+
8+
const utils = require('../utils');
9+
10+
// ------------------------------------------------------------------------------
11+
// Rule Definition
12+
// ------------------------------------------------------------------------------
13+
14+
module.exports = {
15+
meta: {
16+
docs: {
17+
description: 'disallows invalid RuleTester test cases with the output the same as the code.',
18+
category: 'Tests',
19+
recommended: false,
20+
},
21+
fixable: null,
22+
schema: [],
23+
},
24+
25+
create (context) {
26+
// ----------------------------------------------------------------------
27+
// Public
28+
// ----------------------------------------------------------------------
29+
30+
const message = 'Use `output: null` to assert that a test case is not autofixed.';
31+
const sourceCode = context.getSourceCode();
32+
33+
return {
34+
Program (ast) {
35+
utils.getTestInfo(context, ast).forEach(testRun => {
36+
(testRun.invalid || []).forEach(test => {
37+
/**
38+
* Get a test case's giving keyname node.
39+
* @param {string} the keyname to find.
40+
* @returns {Node} found node; if not found, return null;
41+
*/
42+
function getTestInfo (key) {
43+
const res = test.properties.filter(item => item.key.name === key);
44+
return res.length ? res[res.length - 1] : null;
45+
}
46+
47+
const code = getTestInfo('code');
48+
const output = getTestInfo('output');
49+
50+
if (output && sourceCode.getText(output.value) === sourceCode.getText(code.value)) {
51+
context.report({
52+
node: test,
53+
message,
54+
});
55+
}
56+
});
57+
});
58+
},
59+
};
60+
},
61+
};

tests/lib/rules/no-deprecated-report-api.js

+3-21
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,7 @@ ruleTester.run('no-deprecated-report-api', rule, {
124124
}
125125
};
126126
`,
127-
output: `
128-
module.exports = {
129-
create(context) {
130-
context.report(theNode, theMessage, theData, theFix);
131-
}
132-
};
133-
`,
127+
output: null,
134128
errors: [ERROR],
135129
},
136130
{
@@ -175,13 +169,7 @@ ruleTester.run('no-deprecated-report-api', rule, {
175169
}
176170
};
177171
`,
178-
output: `
179-
module.exports = {
180-
create(notContext) {
181-
notContext.report(theNode, theMessage, theData, theFix);
182-
}
183-
};
184-
`,
172+
output: null,
185173
errors: [ERROR],
186174
},
187175
{
@@ -218,13 +206,7 @@ ruleTester.run('no-deprecated-report-api', rule, {
218206
}
219207
};
220208
`,
221-
output: `
222-
module.exports = {
223-
create(context) {
224-
context.report(theNode, theLocation, theMessage, theData, theFix, somethingElse, somethingElse, somethingElse);
225-
}
226-
};
227-
`,
209+
output: null,
228210
errors: [ERROR],
229211
},
230212
],

tests/lib/rules/prefer-output-null.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @fileoverview disallows invalid RuleTester test cases with the output the same as the code.
3+
* @author 薛定谔的猫<[email protected]>
4+
*/
5+
6+
'use strict';
7+
8+
// ------------------------------------------------------------------------------
9+
// Requirements
10+
// ------------------------------------------------------------------------------
11+
12+
const rule = require('../../../lib/rules/prefer-output-null');
13+
const RuleTester = require('eslint').RuleTester;
14+
15+
const ERROR = { message: 'Use `output: null` to assert that a test case is not autofixed.' };
16+
17+
// ------------------------------------------------------------------------------
18+
// Tests
19+
// ------------------------------------------------------------------------------
20+
21+
const ruleTester = new RuleTester();
22+
ruleTester.run('prefer-output-null', rule, {
23+
valid: [
24+
`
25+
new RuleTester().run('foo', bar, {
26+
valid: [],
27+
invalid: [
28+
{code: 'foo', output: 'bar', errors: ['bar']},
29+
]
30+
});
31+
`,
32+
`
33+
new RuleTester().run('foo', bar, {
34+
valid: [],
35+
invalid: [
36+
{code: 'foo', output: null, errors: ['bar']},
37+
]
38+
});
39+
`,
40+
],
41+
42+
invalid: [
43+
{
44+
code: `
45+
new RuleTester().run('foo', bar, {
46+
valid: [],
47+
invalid: [
48+
{code: 'foo', output: 'foo', errors: ['bar']},
49+
]
50+
});
51+
`,
52+
errors: [ERROR],
53+
},
54+
],
55+
});

0 commit comments

Comments
 (0)