lowercase-name.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. var _default = (0, _utils.createRule)({
  18. name: __filename,
  19. meta: {
  20. type: 'suggestion',
  21. docs: {
  22. description: 'Enforce lowercase test names',
  23. category: 'Best Practices',
  24. recommended: false
  25. },
  26. fixable: 'code',
  27. messages: {
  28. unexpectedLowercase: '`{{ method }}`s should begin with lowercase'
  29. },
  30. schema: [{
  31. type: 'object',
  32. properties: {
  33. ignore: {
  34. type: 'array',
  35. items: {
  36. enum: [_utils.DescribeAlias.describe, _utils.TestCaseName.test, _utils.TestCaseName.it]
  37. },
  38. additionalItems: false
  39. },
  40. allowedPrefixes: {
  41. type: 'array',
  42. items: {
  43. type: 'string'
  44. },
  45. additionalItems: false
  46. },
  47. ignoreTopLevelDescribe: {
  48. type: 'boolean',
  49. default: false
  50. }
  51. },
  52. additionalProperties: false
  53. }]
  54. },
  55. defaultOptions: [{
  56. ignore: [],
  57. allowedPrefixes: [],
  58. ignoreTopLevelDescribe: false
  59. }],
  60. create(context, [{
  61. ignore = [],
  62. allowedPrefixes = [],
  63. ignoreTopLevelDescribe
  64. }]) {
  65. let numberOfDescribeBlocks = 0;
  66. return {
  67. CallExpression(node) {
  68. if ((0, _utils.isDescribeCall)(node)) {
  69. numberOfDescribeBlocks++;
  70. if (ignoreTopLevelDescribe && numberOfDescribeBlocks === 1) {
  71. return;
  72. }
  73. }
  74. const results = findNodeNameAndArgument(node);
  75. if (!results) {
  76. return;
  77. }
  78. const [name, firstArg] = results;
  79. const description = (0, _utils.getStringValue)(firstArg);
  80. if (allowedPrefixes.some(name => description.startsWith(name))) {
  81. return;
  82. }
  83. const firstCharacter = description.charAt(0);
  84. if (!firstCharacter || firstCharacter === firstCharacter.toLowerCase() || ignore.includes(name)) {
  85. return;
  86. }
  87. context.report({
  88. messageId: 'unexpectedLowercase',
  89. node: node.arguments[0],
  90. data: {
  91. method: name
  92. },
  93. fix(fixer) {
  94. const description = (0, _utils.getStringValue)(firstArg);
  95. const rangeIgnoringQuotes = [firstArg.range[0] + 1, firstArg.range[1] - 1];
  96. const newDescription = description.substring(0, 1).toLowerCase() + description.substring(1);
  97. return [fixer.replaceTextRange(rangeIgnoringQuotes, newDescription)];
  98. }
  99. });
  100. },
  101. 'CallExpression:exit'(node) {
  102. if ((0, _utils.isDescribeCall)(node)) {
  103. numberOfDescribeBlocks--;
  104. }
  105. }
  106. };
  107. }
  108. });
  109. exports.default = _default;