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