This repository was archived by the owner on Jun 6, 2019. It is now read-only.
forked from eslint-community/eslint-plugin-eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefer-output-null.js
66 lines (57 loc) · 2.13 KB
/
prefer-output-null.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* @fileoverview disallows invalid RuleTester test cases with the output the same as the code.
* @author 薛定谔的猫<[email protected]>
*/
'use strict';
const utils = require('../utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'disallows invalid RuleTester test cases with the output the same as the code.',
category: 'Tests',
recommended: false,
url: 'https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/tree/master/docs/rules/prefer-output-null.md',
},
fixable: 'code',
schema: [],
},
create (context) {
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
const message = 'Use `output: null` to assert that a test case is not autofixed.';
const sourceCode = context.getSourceCode();
return {
Program (ast) {
utils.getTestInfo(context, ast).forEach(testRun => {
testRun.invalid.forEach(test => {
/**
* Get a test case's giving keyname node.
* @param {string} the keyname to find.
* @returns {Node} found node; if not found, return null;
*/
function getTestInfo (key) {
if (test.type === 'ObjectExpression') {
const res = test.properties.filter(item => item.key.name === key);
return res.length ? res[res.length - 1] : null;
}
return key === 'code' ? test : null;
}
const code = getTestInfo('code');
const output = getTestInfo('output');
if (output && sourceCode.getText(output.value) === sourceCode.getText(code.value)) {
context.report({
node: output,
message,
fix: fixer => fixer.replaceText(output.value, 'null'),
});
}
});
});
},
};
},
};