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 薛定谔的猫<hh_2013@foxmail.com>
*/
'use strict';
const utils = require('../utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'disallow invalid RuleTester test cases with the output the same as the code.',
category: 'Tests',
recommended: false,
},
type: 'suggestion',
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'),
});
}
});
});
},
};
},
};
|