no-dom-import.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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-dom-import';
  8. var DOM_TESTING_LIBRARY_MODULES = [
  9. 'dom-testing-library',
  10. '@testing-library/dom',
  11. ];
  12. exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
  13. name: exports.RULE_NAME,
  14. meta: {
  15. type: 'problem',
  16. docs: {
  17. description: 'Disallow importing from DOM Testing Library',
  18. category: 'Best Practices',
  19. recommended: false,
  20. },
  21. messages: {
  22. noDomImport: 'import from DOM Testing Library is restricted, import from corresponding Testing Library framework instead',
  23. noDomImportFramework: 'import from DOM Testing Library is restricted, import from {{module}} instead',
  24. },
  25. fixable: 'code',
  26. schema: [
  27. {
  28. type: 'string',
  29. },
  30. ],
  31. },
  32. defaultOptions: [''],
  33. create: function (context, _a) {
  34. var _b;
  35. var framework = _a[0];
  36. function report(node, moduleName) {
  37. if (framework) {
  38. var isRequire_1 = node_utils_1.isIdentifier(node) && node.name === 'require';
  39. var correctModuleName_1 = moduleName.replace('dom', framework);
  40. context.report({
  41. node: node,
  42. messageId: 'noDomImportFramework',
  43. data: {
  44. module: correctModuleName_1,
  45. },
  46. fix: function (fixer) {
  47. if (isRequire_1) {
  48. var callExpression = node.parent;
  49. var name_1 = callExpression.arguments[0];
  50. return fixer.replaceText(name_1, name_1.raw.replace(moduleName, correctModuleName_1));
  51. }
  52. else {
  53. var importDeclaration = node;
  54. var name_2 = importDeclaration.source;
  55. return fixer.replaceText(name_2, name_2.raw.replace(moduleName, correctModuleName_1));
  56. }
  57. },
  58. });
  59. }
  60. else {
  61. context.report({
  62. node: node,
  63. messageId: 'noDomImport',
  64. });
  65. }
  66. }
  67. return _b = {
  68. ImportDeclaration: function (node) {
  69. var value = node.source.value;
  70. var domModuleName = DOM_TESTING_LIBRARY_MODULES.find(function (module) { return module === value; });
  71. if (domModuleName) {
  72. report(node, domModuleName);
  73. }
  74. }
  75. },
  76. _b["CallExpression > Identifier[name=\"require\"]"] = function (node) {
  77. var callExpression = node.parent;
  78. var args = callExpression.arguments;
  79. var literalNodeDomModuleName = args.find(function (args) {
  80. return node_utils_1.isLiteral(args) &&
  81. typeof args.value === 'string' &&
  82. DOM_TESTING_LIBRARY_MODULES.includes(args.value);
  83. });
  84. if (literalNodeDomModuleName) {
  85. report(node, literalNodeDomModuleName.value);
  86. }
  87. },
  88. _b;
  89. },
  90. });