prefer-lowercase-title.js 3.7 KB

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