no-interpolation-in-snapshots.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. var _default = (0, _utils.createRule)({
  9. name: __filename,
  10. meta: {
  11. docs: {
  12. category: 'Best Practices',
  13. description: 'Disallow string interpolation inside snapshots',
  14. recommended: 'error'
  15. },
  16. messages: {
  17. noInterpolation: 'Do not use string interpolation inside of snapshots'
  18. },
  19. schema: [],
  20. type: 'problem'
  21. },
  22. defaultOptions: [],
  23. create(context) {
  24. return {
  25. CallExpression(node) {
  26. if (!(0, _utils.isExpectCall)(node)) {
  27. return;
  28. }
  29. const {
  30. matcher
  31. } = (0, _utils.parseExpectCall)(node);
  32. if (!matcher) {
  33. return;
  34. }
  35. if (['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot'].includes(matcher.name)) {
  36. var _matcher$arguments;
  37. // Check all since the optional 'propertyMatchers' argument might be present
  38. (_matcher$arguments = matcher.arguments) === null || _matcher$arguments === void 0 ? void 0 : _matcher$arguments.forEach(argument => {
  39. if (argument.type === _experimentalUtils.AST_NODE_TYPES.TemplateLiteral && argument.expressions.length > 0) {
  40. context.report({
  41. messageId: 'noInterpolation',
  42. node: argument
  43. });
  44. }
  45. });
  46. }
  47. }
  48. };
  49. }
  50. });
  51. exports.default = _default;