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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
/**
* @fileoverview Enforce consistent usage of shorthand strings for test cases with no options
* @author Teddy Katz
*/
'use strict';
const utils = require('../utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'enforce consistent usage of shorthand strings for test cases with no options',
category: 'Tests',
recommended: false,
},
type: 'suggestion',
schema: [{ enum: ['as-needed', 'never', 'consistent', 'consistent-as-needed'] }],
fixable: 'code',
},
create (context) {
const shorthandOption = context.options[0] || 'as-needed';
const sourceCode = context.getSourceCode();
// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------
/**
* Reports test cases as necessary
* @param {object[]} cases A list of test case nodes
* @returns {void}
*/
function reportTestCases (cases) {
const caseInfoList = cases.map(testCase => {
if (testCase.type === 'Literal' || testCase.type === 'TemplateLiteral') {
return { node: testCase, shorthand: true, needsLongform: false };
}
if (testCase.type === 'ObjectExpression') {
return {
node: testCase,
shorthand: false,
needsLongform: !(testCase.properties.length === 1 && utils.getKeyName(testCase.properties[0]) === 'code'),
};
}
return null;
}).filter(Boolean);
const isConsistent = new Set(caseInfoList.map(caseInfo => caseInfo.shorthand)).size <= 1;
const hasCaseNeedingLongform = caseInfoList.some(caseInfo => caseInfo.needsLongform);
caseInfoList.filter({
'as-needed': caseInfo => !caseInfo.shorthand && !caseInfo.needsLongform,
never: caseInfo => caseInfo.shorthand,
consistent: isConsistent ? () => false : caseInfo => caseInfo.shorthand,
'consistent-as-needed': caseInfo => caseInfo.shorthand === hasCaseNeedingLongform,
}[shorthandOption]).forEach(badCaseInfo => {
context.report({
node: badCaseInfo.node,
message: 'Use {{preferred}} for this test case instead of {{actual}}.',
data: {
preferred: badCaseInfo.shorthand ? 'an object' : 'a string',
actual: badCaseInfo.shorthand ? 'a string' : 'an object',
},
fix (fixer) {
return fixer.replaceText(
badCaseInfo.node,
badCaseInfo.shorthand
? `{code: ${sourceCode.getText(badCaseInfo.node)}}`
: sourceCode.getText(badCaseInfo.node.properties[0].value)
);
},
});
});
}
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
return {
Program (ast) {
utils.getTestInfo(context, ast).map(testRun => testRun.valid).filter(Boolean).forEach(reportTestCases);
},
};
},
};
|