UNPKG

eslint-plugin-jest

Version:
101 lines (75 loc) 3.31 kB
'use strict'; function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } const _require = require('./util'), getDocsUrl = _require.getDocsUrl; const isItTestOrDescribeFunction = node => { return node.type === 'CallExpression' && node.callee && (node.callee.name === 'it' || node.callee.name === 'test' || node.callee.name === 'describe'); }; const isItDescription = node => { return node.arguments && node.arguments[0] && (node.arguments[0].type === 'Literal' || node.arguments[0].type === 'TemplateLiteral'); }; const testDescription = node => { const _node$arguments = _slicedToArray(node.arguments, 1), firstArgument = _node$arguments[0]; const type = firstArgument.type; if (type === 'Literal') { return firstArgument.value; } // `isItDescription` guarantees this is `type === 'TemplateLiteral'` return firstArgument.quasis[0].value.raw; }; const descriptionBeginsWithLowerCase = node => { if (isItTestOrDescribeFunction(node) && isItDescription(node)) { const description = testDescription(node); if (!description[0]) { return false; } if (description[0] !== description[0].toLowerCase()) { return node.callee.name; } } return false; }; module.exports = { meta: { docs: { url: getDocsUrl(__filename) }, messages: { unexpectedLowercase: '`{{ method }}`s should begin with lowercase' }, fixable: 'code' }, create(context) { const ignore = context.options[0] && context.options[0].ignore || []; const ignoredFunctionNames = ignore.reduce((accumulator, value) => { accumulator[value] = true; return accumulator; }, Object.create(null)); const isIgnoredFunctionName = node => ignoredFunctionNames[node.callee.name]; return { CallExpression(node) { const erroneousMethod = descriptionBeginsWithLowerCase(node); if (erroneousMethod && !isIgnoredFunctionName(node)) { context.report({ messageId: 'unexpectedLowercase', data: { method: erroneousMethod }, node, fix(fixer) { const _node$arguments2 = _slicedToArray(node.arguments, 1), firstArg = _node$arguments2[0]; const description = testDescription(node); const rangeIgnoringQuotes = [firstArg.range[0] + 1, firstArg.range[1] - 1]; const newDescription = description.substring(0, 1).toLowerCase() + description.substring(1); return [fixer.replaceTextRange(rangeIgnoringQuotes, newDescription)]; } }); } } }; } };