| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = void 0;
|
|---|
| 7 |
|
|---|
| 8 | var _utils = require("./utils");
|
|---|
| 9 |
|
|---|
| 10 | const hasStringAsFirstArgument = node => node.arguments[0] && (0, _utils.isStringNode)(node.arguments[0]);
|
|---|
| 11 |
|
|---|
| 12 | const findNodeNameAndArgument = node => {
|
|---|
| 13 | if (!((0, _utils.isTestCaseCall)(node) || (0, _utils.isDescribeCall)(node))) {
|
|---|
| 14 | return null;
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | if (!hasStringAsFirstArgument(node)) {
|
|---|
| 18 | return null;
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | return [(0, _utils.getNodeName)(node).split('.')[0], node.arguments[0]];
|
|---|
| 22 | };
|
|---|
| 23 |
|
|---|
| 24 | const populateIgnores = ignore => {
|
|---|
| 25 | const ignores = [];
|
|---|
| 26 |
|
|---|
| 27 | if (ignore.includes(_utils.DescribeAlias.describe)) {
|
|---|
| 28 | ignores.push(...Object.keys(_utils.DescribeAlias));
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | if (ignore.includes(_utils.TestCaseName.test)) {
|
|---|
| 32 | ignores.push(...Object.keys(_utils.TestCaseName).filter(k => k.endsWith(_utils.TestCaseName.test)));
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | if (ignore.includes(_utils.TestCaseName.it)) {
|
|---|
| 36 | ignores.push(...Object.keys(_utils.TestCaseName).filter(k => k.endsWith(_utils.TestCaseName.it)));
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | return ignores;
|
|---|
| 40 | };
|
|---|
| 41 |
|
|---|
| 42 | var _default = (0, _utils.createRule)({
|
|---|
| 43 | name: __filename,
|
|---|
| 44 | meta: {
|
|---|
| 45 | type: 'suggestion',
|
|---|
| 46 | docs: {
|
|---|
| 47 | description: 'Enforce lowercase test names',
|
|---|
| 48 | category: 'Best Practices',
|
|---|
| 49 | recommended: false
|
|---|
| 50 | },
|
|---|
| 51 | fixable: 'code',
|
|---|
| 52 | messages: {
|
|---|
| 53 | unexpectedLowercase: '`{{ method }}`s should begin with lowercase'
|
|---|
| 54 | },
|
|---|
| 55 | schema: [{
|
|---|
| 56 | type: 'object',
|
|---|
| 57 | properties: {
|
|---|
| 58 | ignore: {
|
|---|
| 59 | type: 'array',
|
|---|
| 60 | items: {
|
|---|
| 61 | enum: [_utils.DescribeAlias.describe, _utils.TestCaseName.test, _utils.TestCaseName.it]
|
|---|
| 62 | },
|
|---|
| 63 | additionalItems: false
|
|---|
| 64 | },
|
|---|
| 65 | allowedPrefixes: {
|
|---|
| 66 | type: 'array',
|
|---|
| 67 | items: {
|
|---|
| 68 | type: 'string'
|
|---|
| 69 | },
|
|---|
| 70 | additionalItems: false
|
|---|
| 71 | },
|
|---|
| 72 | ignoreTopLevelDescribe: {
|
|---|
| 73 | type: 'boolean',
|
|---|
| 74 | default: false
|
|---|
| 75 | }
|
|---|
| 76 | },
|
|---|
| 77 | additionalProperties: false
|
|---|
| 78 | }]
|
|---|
| 79 | },
|
|---|
| 80 | defaultOptions: [{
|
|---|
| 81 | ignore: [],
|
|---|
| 82 | allowedPrefixes: [],
|
|---|
| 83 | ignoreTopLevelDescribe: false
|
|---|
| 84 | }],
|
|---|
| 85 |
|
|---|
| 86 | create(context, [{
|
|---|
| 87 | ignore = [],
|
|---|
| 88 | allowedPrefixes = [],
|
|---|
| 89 | ignoreTopLevelDescribe
|
|---|
| 90 | }]) {
|
|---|
| 91 | const ignores = populateIgnores(ignore);
|
|---|
| 92 | let numberOfDescribeBlocks = 0;
|
|---|
| 93 | return {
|
|---|
| 94 | CallExpression(node) {
|
|---|
| 95 | if ((0, _utils.isDescribeCall)(node)) {
|
|---|
| 96 | numberOfDescribeBlocks++;
|
|---|
| 97 |
|
|---|
| 98 | if (ignoreTopLevelDescribe && numberOfDescribeBlocks === 1) {
|
|---|
| 99 | return;
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | const results = findNodeNameAndArgument(node);
|
|---|
| 104 |
|
|---|
| 105 | if (!results) {
|
|---|
| 106 | return;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | const [name, firstArg] = results;
|
|---|
| 110 | const description = (0, _utils.getStringValue)(firstArg);
|
|---|
| 111 |
|
|---|
| 112 | if (allowedPrefixes.some(name => description.startsWith(name))) {
|
|---|
| 113 | return;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | const firstCharacter = description.charAt(0);
|
|---|
| 117 |
|
|---|
| 118 | if (!firstCharacter || firstCharacter === firstCharacter.toLowerCase() || ignores.includes(name)) {
|
|---|
| 119 | return;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | context.report({
|
|---|
| 123 | messageId: 'unexpectedLowercase',
|
|---|
| 124 | node: node.arguments[0],
|
|---|
| 125 | data: {
|
|---|
| 126 | method: name
|
|---|
| 127 | },
|
|---|
| 128 |
|
|---|
| 129 | fix(fixer) {
|
|---|
| 130 | const description = (0, _utils.getStringValue)(firstArg);
|
|---|
| 131 | const rangeIgnoringQuotes = [firstArg.range[0] + 1, firstArg.range[1] - 1];
|
|---|
| 132 | const newDescription = description.substring(0, 1).toLowerCase() + description.substring(1);
|
|---|
| 133 | return [fixer.replaceTextRange(rangeIgnoringQuotes, newDescription)];
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | });
|
|---|
| 137 | },
|
|---|
| 138 |
|
|---|
| 139 | 'CallExpression:exit'(node) {
|
|---|
| 140 | if ((0, _utils.isDescribeCall)(node)) {
|
|---|
| 141 | numberOfDescribeBlocks--;
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | };
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | });
|
|---|
| 149 |
|
|---|
| 150 | exports.default = _default; |
|---|