prefer-explicit-assert.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RULE_NAME = void 0;
  4. var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
  5. var utils_1 = require("../utils");
  6. var node_utils_1 = require("../node-utils");
  7. exports.RULE_NAME = 'prefer-explicit-assert';
  8. var ALL_GET_BY_QUERIES = utils_1.ALL_QUERIES_METHODS.map(function (queryMethod) { return "get" + queryMethod; });
  9. var isValidQuery = function (node, customQueryNames) {
  10. return ALL_GET_BY_QUERIES.includes(node.name) ||
  11. customQueryNames.includes(node.name);
  12. };
  13. var isAtTopLevel = function (node) {
  14. return node.parent.parent.type === 'ExpressionStatement';
  15. };
  16. exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
  17. name: exports.RULE_NAME,
  18. meta: {
  19. type: 'suggestion',
  20. docs: {
  21. description: 'Suggest using explicit assertions rather than just `getBy*` queries',
  22. category: 'Best Practices',
  23. recommended: false,
  24. },
  25. messages: {
  26. preferExplicitAssert: 'Wrap stand-alone `getBy*` query with `expect` function for better explicit assertion',
  27. preferExplicitAssertAssertion: '`getBy*` queries must be asserted with `{{assertion}}`',
  28. },
  29. fixable: null,
  30. schema: [
  31. {
  32. type: 'object',
  33. additionalProperties: false,
  34. properties: {
  35. assertion: {
  36. type: 'string',
  37. enum: utils_1.PRESENCE_MATCHERS,
  38. },
  39. customQueryNames: {
  40. type: 'array',
  41. },
  42. },
  43. },
  44. ],
  45. },
  46. defaultOptions: [
  47. {
  48. customQueryNames: [],
  49. },
  50. ],
  51. create: function (context, _a) {
  52. var options = _a[0];
  53. var customQueryNames = options.customQueryNames, assertion = options.assertion;
  54. var getQueryCalls = [];
  55. return {
  56. 'CallExpression Identifier': function (node) {
  57. if (isValidQuery(node, customQueryNames)) {
  58. getQueryCalls.push(node);
  59. }
  60. },
  61. 'Program:exit': function () {
  62. getQueryCalls.forEach(function (queryCall) {
  63. var node = node_utils_1.isMemberExpression(queryCall.parent)
  64. ? queryCall.parent
  65. : queryCall;
  66. if (isAtTopLevel(node)) {
  67. context.report({
  68. node: queryCall,
  69. messageId: 'preferExplicitAssert',
  70. });
  71. }
  72. else if (assertion) {
  73. var expectCallNode = node_utils_1.findClosestCallNode(node, 'expect');
  74. if (!expectCallNode)
  75. return;
  76. var expectStatement = expectCallNode.parent;
  77. var property = expectStatement.property;
  78. var matcher = property.name;
  79. var isNegatedMatcher = false;
  80. if (matcher === 'not' &&
  81. node_utils_1.isMemberExpression(expectStatement.parent) &&
  82. node_utils_1.isIdentifier(expectStatement.parent.property)) {
  83. isNegatedMatcher = true;
  84. matcher = expectStatement.parent.property.name;
  85. }
  86. var shouldEnforceAssertion = (!isNegatedMatcher && utils_1.PRESENCE_MATCHERS.includes(matcher)) ||
  87. (isNegatedMatcher && utils_1.ABSENCE_MATCHERS.includes(matcher));
  88. if (shouldEnforceAssertion && matcher !== assertion) {
  89. context.report({
  90. node: property,
  91. messageId: 'preferExplicitAssertAssertion',
  92. data: {
  93. assertion: assertion,
  94. },
  95. });
  96. }
  97. }
  98. });
  99. },
  100. };
  101. },
  102. });