no-node-access.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 utils_2 = require("../utils");
  7. exports.RULE_NAME = 'no-node-access';
  8. exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
  9. name: exports.RULE_NAME,
  10. meta: {
  11. type: 'problem',
  12. docs: {
  13. description: 'Disallow direct Node access',
  14. recommendedConfig: {
  15. dom: false,
  16. angular: 'error',
  17. react: 'error',
  18. vue: 'error',
  19. marko: 'error',
  20. },
  21. },
  22. messages: {
  23. noNodeAccess: 'Avoid direct Node access. Prefer using the methods from Testing Library.',
  24. },
  25. schema: [
  26. {
  27. type: 'object',
  28. properties: {
  29. allowContainerFirstChild: {
  30. type: 'boolean',
  31. },
  32. },
  33. },
  34. ],
  35. },
  36. defaultOptions: [
  37. {
  38. allowContainerFirstChild: false,
  39. },
  40. ],
  41. create(context, [{ allowContainerFirstChild = false }], helpers) {
  42. function showErrorForNodeAccess(node) {
  43. if (!helpers.isTestingLibraryImported(true)) {
  44. return;
  45. }
  46. if (utils_1.ASTUtils.isIdentifier(node.property) &&
  47. utils_2.ALL_RETURNING_NODES.includes(node.property.name)) {
  48. if (allowContainerFirstChild && node.property.name === 'firstChild') {
  49. return;
  50. }
  51. if (utils_1.ASTUtils.isIdentifier(node.object) &&
  52. node.object.name === 'props') {
  53. return;
  54. }
  55. context.report({
  56. node,
  57. loc: node.property.loc.start,
  58. messageId: 'noNodeAccess',
  59. });
  60. }
  61. }
  62. return {
  63. 'ExpressionStatement MemberExpression': showErrorForNodeAccess,
  64. 'VariableDeclarator MemberExpression': showErrorForNodeAccess,
  65. };
  66. },
  67. });