jsx-max-depth.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * @fileoverview Validate JSX maximum depth
  3. * @author Chris<wfsr@foxmail.com>
  4. */
  5. 'use strict';
  6. const has = require('hasown');
  7. const includes = require('array-includes');
  8. const variableUtil = require('../util/variable');
  9. const jsxUtil = require('../util/jsx');
  10. const docsUrl = require('../util/docsUrl');
  11. const reportC = require('../util/report');
  12. // ------------------------------------------------------------------------------
  13. // Rule Definition
  14. // ------------------------------------------------------------------------------
  15. const messages = {
  16. wrongDepth: 'Expected the depth of nested jsx elements to be <= {{needed}}, but found {{found}}.',
  17. };
  18. /** @type {import('eslint').Rule.RuleModule} */
  19. module.exports = {
  20. meta: {
  21. docs: {
  22. description: 'Enforce JSX maximum depth',
  23. category: 'Stylistic Issues',
  24. recommended: false,
  25. url: docsUrl('jsx-max-depth'),
  26. },
  27. messages,
  28. schema: [
  29. {
  30. type: 'object',
  31. properties: {
  32. max: {
  33. type: 'integer',
  34. minimum: 0,
  35. },
  36. },
  37. additionalProperties: false,
  38. },
  39. ],
  40. },
  41. create(context) {
  42. const DEFAULT_DEPTH = 2;
  43. const option = context.options[0] || {};
  44. const maxDepth = has(option, 'max') ? option.max : DEFAULT_DEPTH;
  45. function isExpression(node) {
  46. return node.type === 'JSXExpressionContainer';
  47. }
  48. function hasJSX(node) {
  49. return jsxUtil.isJSX(node) || (isExpression(node) && jsxUtil.isJSX(node.expression));
  50. }
  51. function isLeaf(node) {
  52. const children = node.children;
  53. return !children || children.length === 0 || !children.some(hasJSX);
  54. }
  55. function getDepth(node) {
  56. let count = 0;
  57. while (jsxUtil.isJSX(node.parent) || isExpression(node.parent)) {
  58. node = node.parent;
  59. if (jsxUtil.isJSX(node)) {
  60. count += 1;
  61. }
  62. }
  63. return count;
  64. }
  65. function report(node, depth) {
  66. reportC(context, messages.wrongDepth, 'wrongDepth', {
  67. node,
  68. data: {
  69. found: depth,
  70. needed: maxDepth,
  71. },
  72. });
  73. }
  74. function findJSXElementOrFragment(startNode, name, previousReferences) {
  75. function find(refs, prevRefs) {
  76. for (let i = refs.length - 1; i >= 0; i--) {
  77. if (typeof refs[i].writeExpr !== 'undefined') {
  78. const writeExpr = refs[i].writeExpr;
  79. return (jsxUtil.isJSX(writeExpr)
  80. && writeExpr)
  81. || ((writeExpr && writeExpr.type === 'Identifier')
  82. && findJSXElementOrFragment(startNode, writeExpr.name, prevRefs));
  83. }
  84. }
  85. return null;
  86. }
  87. const variable = variableUtil.getVariableFromContext(context, startNode, name);
  88. if (variable && variable.references) {
  89. const containDuplicates = previousReferences.some((ref) => includes(variable.references, ref));
  90. // Prevent getting stuck in circular references
  91. if (containDuplicates) {
  92. return false;
  93. }
  94. return find(variable.references, previousReferences.concat(variable.references));
  95. }
  96. return false;
  97. }
  98. function checkDescendant(baseDepth, children) {
  99. baseDepth += 1;
  100. (children || []).filter((node) => hasJSX(node)).forEach((node) => {
  101. if (baseDepth > maxDepth) {
  102. report(node, baseDepth);
  103. } else if (!isLeaf(node)) {
  104. checkDescendant(baseDepth, node.children);
  105. }
  106. });
  107. }
  108. function handleJSX(node) {
  109. if (!isLeaf(node)) {
  110. return;
  111. }
  112. const depth = getDepth(node);
  113. if (depth > maxDepth) {
  114. report(node, depth);
  115. }
  116. }
  117. return {
  118. JSXElement: handleJSX,
  119. JSXFragment: handleJSX,
  120. JSXExpressionContainer(node) {
  121. if (node.expression.type !== 'Identifier') {
  122. return;
  123. }
  124. const element = findJSXElementOrFragment(node, node.expression.name, []);
  125. if (element) {
  126. const baseDepth = getDepth(node);
  127. checkDescendant(baseDepth, element.children);
  128. }
  129. },
  130. };
  131. },
  132. };