prefer-to-be.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("@typescript-eslint/utils");
  7. var _utils2 = require("./utils");
  8. const isNullLiteral = node => node.type === _utils.AST_NODE_TYPES.Literal && node.value === null;
  9. /**
  10. * Checks if the given `ParsedEqualityMatcherCall` is a call to one of the equality matchers,
  11. * with a `null` literal as the sole argument.
  12. */
  13. const isNullEqualityMatcher = expectFnCall => isNullLiteral((0, _utils2.getFirstMatcherArg)(expectFnCall));
  14. const isFirstArgumentIdentifier = (expectFnCall, name) => (0, _utils2.isIdentifier)((0, _utils2.getFirstMatcherArg)(expectFnCall), name);
  15. const shouldUseToBe = expectFnCall => {
  16. const firstArg = (0, _utils2.getFirstMatcherArg)(expectFnCall);
  17. if (firstArg.type === _utils.AST_NODE_TYPES.Literal) {
  18. // regex literals are classed as literals, but they're actually objects
  19. // which means "toBe" will give different results than other matchers
  20. return !('regex' in firstArg);
  21. }
  22. return firstArg.type === _utils.AST_NODE_TYPES.TemplateLiteral;
  23. };
  24. const reportPreferToBe = (context, whatToBe, expectFnCall, modifierNode) => {
  25. context.report({
  26. messageId: `useToBe${whatToBe}`,
  27. fix(fixer) {
  28. var _expectFnCall$args;
  29. const fixes = [(0, _utils2.replaceAccessorFixer)(fixer, expectFnCall.matcher, `toBe${whatToBe}`)];
  30. if ((_expectFnCall$args = expectFnCall.args) !== null && _expectFnCall$args !== void 0 && _expectFnCall$args.length && whatToBe !== '') {
  31. fixes.push(fixer.remove(expectFnCall.args[0]));
  32. }
  33. if (modifierNode) {
  34. fixes.push(fixer.removeRange([modifierNode.range[0] - 1, modifierNode.range[1]]));
  35. }
  36. return fixes;
  37. },
  38. node: expectFnCall.matcher
  39. });
  40. };
  41. var _default = (0, _utils2.createRule)({
  42. name: __filename,
  43. meta: {
  44. docs: {
  45. category: 'Best Practices',
  46. description: 'Suggest using `toBe()` for primitive literals',
  47. recommended: false
  48. },
  49. messages: {
  50. useToBe: 'Use `toBe` when expecting primitive literals',
  51. useToBeUndefined: 'Use `toBeUndefined` instead',
  52. useToBeDefined: 'Use `toBeDefined` instead',
  53. useToBeNull: 'Use `toBeNull` instead',
  54. useToBeNaN: 'Use `toBeNaN` instead'
  55. },
  56. fixable: 'code',
  57. type: 'suggestion',
  58. schema: []
  59. },
  60. defaultOptions: [],
  61. create(context) {
  62. return {
  63. CallExpression(node) {
  64. const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
  65. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'expect') {
  66. return;
  67. }
  68. const matcherName = (0, _utils2.getAccessorValue)(jestFnCall.matcher);
  69. const notModifier = jestFnCall.modifiers.find(nod => (0, _utils2.getAccessorValue)(nod) === 'not');
  70. if (notModifier && ['toBeUndefined', 'toBeDefined'].includes(matcherName)) {
  71. reportPreferToBe(context, matcherName === 'toBeDefined' ? 'Undefined' : 'Defined', jestFnCall, notModifier);
  72. return;
  73. }
  74. if (!_utils2.EqualityMatcher.hasOwnProperty(matcherName) || jestFnCall.args.length === 0) {
  75. return;
  76. }
  77. if (isNullEqualityMatcher(jestFnCall)) {
  78. reportPreferToBe(context, 'Null', jestFnCall);
  79. return;
  80. }
  81. if (isFirstArgumentIdentifier(jestFnCall, 'undefined')) {
  82. const name = notModifier ? 'Defined' : 'Undefined';
  83. reportPreferToBe(context, name, jestFnCall, notModifier);
  84. return;
  85. }
  86. if (isFirstArgumentIdentifier(jestFnCall, 'NaN')) {
  87. reportPreferToBe(context, 'NaN', jestFnCall);
  88. return;
  89. }
  90. if (shouldUseToBe(jestFnCall) && matcherName !== _utils2.EqualityMatcher.toBe) {
  91. reportPreferToBe(context, '', jestFnCall);
  92. }
  93. }
  94. };
  95. }
  96. });
  97. exports.default = _default;