no-interpolation-in-snapshots.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. var _default = (0, _utils2.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. const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
  27. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'expect') {
  28. return;
  29. }
  30. if (['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot'].includes((0, _utils2.getAccessorValue)(jestFnCall.matcher))) {
  31. // Check all since the optional 'propertyMatchers' argument might be present
  32. jestFnCall.args.forEach(argument => {
  33. if (argument.type === _utils.AST_NODE_TYPES.TemplateLiteral && argument.expressions.length > 0) {
  34. context.report({
  35. messageId: 'noInterpolation',
  36. node: argument
  37. });
  38. }
  39. });
  40. }
  41. }
  42. };
  43. }
  44. });
  45. exports.default = _default;