prefer-to-be-null.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _experimentalUtils = require("@typescript-eslint/experimental-utils");
  7. var _utils = require("./utils");
  8. const isNullLiteral = node => node.type === _experimentalUtils.AST_NODE_TYPES.Literal && node.value === null;
  9. /**
  10. * Checks if the given `ParsedExpectMatcher` is a call to one of the equality matchers,
  11. * with a `null` literal as the sole argument.
  12. *
  13. * @param {ParsedExpectMatcher} matcher
  14. *
  15. * @return {matcher is ParsedEqualityMatcherCall<MaybeTypeCast<NullLiteral>>}
  16. */
  17. const isNullEqualityMatcher = matcher => (0, _utils.isParsedEqualityMatcherCall)(matcher) && isNullLiteral((0, _utils.followTypeAssertionChain)(matcher.arguments[0]));
  18. var _default = (0, _utils.createRule)({
  19. name: __filename,
  20. meta: {
  21. docs: {
  22. category: 'Best Practices',
  23. description: 'Suggest using `toBeNull()`',
  24. recommended: false
  25. },
  26. messages: {
  27. useToBeNull: 'Use toBeNull() instead'
  28. },
  29. fixable: 'code',
  30. type: 'suggestion',
  31. schema: []
  32. },
  33. defaultOptions: [],
  34. create(context) {
  35. return {
  36. CallExpression(node) {
  37. if (!(0, _utils.isExpectCall)(node)) {
  38. return;
  39. }
  40. const {
  41. matcher
  42. } = (0, _utils.parseExpectCall)(node);
  43. if (matcher && isNullEqualityMatcher(matcher)) {
  44. context.report({
  45. fix: fixer => [fixer.replaceText(matcher.node.property, 'toBeNull'), fixer.remove(matcher.arguments[0])],
  46. messageId: 'useToBeNull',
  47. node: matcher.node.property
  48. });
  49. }
  50. }
  51. };
  52. }
  53. });
  54. exports.default = _default;