no-manual-cleanup.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RULE_NAME = void 0;
  4. var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
  5. var utils_1 = require("../utils");
  6. var node_utils_1 = require("../node-utils");
  7. exports.RULE_NAME = 'no-manual-cleanup';
  8. var CLEANUP_LIBRARY_REGEX = /(@testing-library\/(preact|react|svelte|vue))|@marko\/testing-library/;
  9. exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
  10. name: exports.RULE_NAME,
  11. meta: {
  12. type: 'problem',
  13. docs: {
  14. description: 'Disallow the use of `cleanup`',
  15. category: 'Best Practices',
  16. recommended: false,
  17. },
  18. messages: {
  19. noManualCleanup: "`cleanup` is performed automatically by your test runner, you don't need manual cleanups.",
  20. },
  21. fixable: null,
  22. schema: [],
  23. },
  24. defaultOptions: [],
  25. create: function (context) {
  26. var _a;
  27. var defaultImportFromTestingLibrary;
  28. var defaultRequireFromTestingLibrary;
  29. function reportImportReferences(references) {
  30. if (references && references.length > 0) {
  31. references.forEach(function (reference) {
  32. var utilsUsage = reference.identifier.parent;
  33. if (node_utils_1.isMemberExpression(utilsUsage) &&
  34. node_utils_1.isIdentifier(utilsUsage.property) &&
  35. utilsUsage.property.name === 'cleanup') {
  36. context.report({
  37. node: utilsUsage.property,
  38. messageId: 'noManualCleanup',
  39. });
  40. }
  41. });
  42. }
  43. }
  44. return _a = {
  45. ImportDeclaration: function (node) {
  46. var value = node.source.value;
  47. var testingLibraryWithCleanup = value.match(CLEANUP_LIBRARY_REGEX);
  48. if (!testingLibraryWithCleanup) {
  49. return;
  50. }
  51. if (node_utils_1.isImportDefaultSpecifier(node.specifiers[0])) {
  52. defaultImportFromTestingLibrary = node;
  53. }
  54. var cleanupSpecifier = node.specifiers.find(function (specifier) {
  55. return node_utils_1.isImportSpecifier(specifier) &&
  56. specifier.imported &&
  57. specifier.imported.name === 'cleanup';
  58. });
  59. if (cleanupSpecifier) {
  60. context.report({
  61. node: cleanupSpecifier,
  62. messageId: 'noManualCleanup',
  63. });
  64. }
  65. }
  66. },
  67. _a["VariableDeclarator > CallExpression > Identifier[name=\"require\"]"] = function (node) {
  68. var args = node.parent.arguments;
  69. var literalNodeCleanupModuleName = args.find(function (args) {
  70. return node_utils_1.isLiteral(args) &&
  71. typeof args.value === 'string' &&
  72. args.value.match(CLEANUP_LIBRARY_REGEX);
  73. });
  74. if (!literalNodeCleanupModuleName) {
  75. return;
  76. }
  77. var declaratorNode = node.parent
  78. .parent;
  79. if (node_utils_1.isObjectPattern(declaratorNode.id)) {
  80. var cleanupProperty = declaratorNode.id.properties.find(function (property) {
  81. return node_utils_1.isProperty(property) &&
  82. node_utils_1.isIdentifier(property.key) &&
  83. property.key.name === 'cleanup';
  84. });
  85. if (cleanupProperty) {
  86. context.report({
  87. node: cleanupProperty,
  88. messageId: 'noManualCleanup',
  89. });
  90. }
  91. }
  92. else {
  93. defaultRequireFromTestingLibrary = declaratorNode.id;
  94. }
  95. },
  96. _a['Program:exit'] = function () {
  97. if (defaultImportFromTestingLibrary) {
  98. var references = context.getDeclaredVariables(defaultImportFromTestingLibrary)[0].references;
  99. reportImportReferences(references);
  100. }
  101. if (defaultRequireFromTestingLibrary) {
  102. var references = context
  103. .getDeclaredVariables(defaultRequireFromTestingLibrary.parent)[0]
  104. .references.slice(1);
  105. reportImportReferences(references);
  106. }
  107. },
  108. _a;
  109. },
  110. });