no-manual-cleanup.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RULE_NAME = void 0;
  4. const utils_1 = require("@typescript-eslint/utils");
  5. const create_testing_library_rule_1 = require("../create-testing-library-rule");
  6. const node_utils_1 = require("../node-utils");
  7. exports.RULE_NAME = 'no-manual-cleanup';
  8. const CLEANUP_LIBRARY_REGEXP = /(@testing-library\/(preact|react|svelte|vue))|@marko\/testing-library/;
  9. exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
  10. name: exports.RULE_NAME,
  11. meta: {
  12. type: 'problem',
  13. docs: {
  14. description: 'Disallow the use of `cleanup`',
  15. recommendedConfig: {
  16. dom: false,
  17. angular: false,
  18. react: false,
  19. vue: false,
  20. marko: false,
  21. },
  22. },
  23. messages: {
  24. noManualCleanup: "`cleanup` is performed automatically by your test runner, you don't need manual cleanups.",
  25. },
  26. schema: [],
  27. },
  28. defaultOptions: [],
  29. create(context, _, helpers) {
  30. function reportImportReferences(references) {
  31. for (const reference of references) {
  32. const utilsUsage = reference.identifier.parent;
  33. if (utilsUsage &&
  34. (0, node_utils_1.isMemberExpression)(utilsUsage) &&
  35. utils_1.ASTUtils.isIdentifier(utilsUsage.property) &&
  36. utilsUsage.property.name === 'cleanup') {
  37. context.report({
  38. node: utilsUsage.property,
  39. messageId: 'noManualCleanup',
  40. });
  41. }
  42. }
  43. }
  44. function reportCandidateModule(moduleNode) {
  45. if ((0, node_utils_1.isImportDeclaration)(moduleNode)) {
  46. if ((0, node_utils_1.isImportDefaultSpecifier)(moduleNode.specifiers[0])) {
  47. const { references } = context.getDeclaredVariables(moduleNode)[0];
  48. reportImportReferences(references);
  49. }
  50. const cleanupSpecifier = moduleNode.specifiers.find((specifier) => (0, node_utils_1.isImportSpecifier)(specifier) &&
  51. specifier.imported.name === 'cleanup');
  52. if (cleanupSpecifier) {
  53. context.report({
  54. node: cleanupSpecifier,
  55. messageId: 'noManualCleanup',
  56. });
  57. }
  58. }
  59. else {
  60. const declaratorNode = moduleNode.parent;
  61. if ((0, node_utils_1.isObjectPattern)(declaratorNode.id)) {
  62. const cleanupProperty = declaratorNode.id.properties.find((property) => (0, node_utils_1.isProperty)(property) &&
  63. utils_1.ASTUtils.isIdentifier(property.key) &&
  64. property.key.name === 'cleanup');
  65. if (cleanupProperty) {
  66. context.report({
  67. node: cleanupProperty,
  68. messageId: 'noManualCleanup',
  69. });
  70. }
  71. }
  72. else {
  73. const references = (0, node_utils_1.getVariableReferences)(context, declaratorNode);
  74. reportImportReferences(references);
  75. }
  76. }
  77. }
  78. return {
  79. 'Program:exit'() {
  80. const testingLibraryImportName = helpers.getTestingLibraryImportName();
  81. const testingLibraryImportNode = helpers.getTestingLibraryImportNode();
  82. const customModuleImportNode = helpers.getCustomModuleImportNode();
  83. if (testingLibraryImportName &&
  84. testingLibraryImportNode &&
  85. testingLibraryImportName.match(CLEANUP_LIBRARY_REGEXP)) {
  86. reportCandidateModule(testingLibraryImportNode);
  87. }
  88. if (customModuleImportNode) {
  89. reportCandidateModule(customModuleImportNode);
  90. }
  91. },
  92. };
  93. },
  94. });