jsx-max-depth.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * @fileoverview Validate JSX maximum depth
  3. * @author Chris<wfsr@foxmail.com>
  4. */
  5. 'use strict';
  6. const has = require('has');
  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. // ------------------------------------------------------------------------------
  12. // Rule Definition
  13. // ------------------------------------------------------------------------------
  14. module.exports = {
  15. meta: {
  16. docs: {
  17. description: 'Validate JSX maximum depth',
  18. category: 'Stylistic Issues',
  19. recommended: false,
  20. url: docsUrl('jsx-max-depth')
  21. },
  22. messages: {
  23. wrongDepth: 'Expected the depth of nested jsx elements to be <= {{needed}}, but found {{found}}.'
  24. },
  25. schema: [
  26. {
  27. type: 'object',
  28. properties: {
  29. max: {
  30. type: 'integer',
  31. minimum: 0
  32. }
  33. },
  34. additionalProperties: false
  35. }
  36. ]
  37. },
  38. create(context) {
  39. const DEFAULT_DEPTH = 2;
  40. const option = context.options[0] || {};
  41. const maxDepth = has(option, 'max') ? option.max : DEFAULT_DEPTH;
  42. function isExpression(node) {
  43. return node.type === 'JSXExpressionContainer';
  44. }
  45. function hasJSX(node) {
  46. return jsxUtil.isJSX(node) || (isExpression(node) && jsxUtil.isJSX(node.expression));
  47. }
  48. function isLeaf(node) {
  49. const children = node.children;
  50. return !children || children.length === 0 || !children.some(hasJSX);
  51. }
  52. function getDepth(node) {
  53. let count = 0;
  54. while (jsxUtil.isJSX(node.parent) || isExpression(node.parent)) {
  55. node = node.parent;
  56. if (jsxUtil.isJSX(node)) {
  57. count++;
  58. }
  59. }
  60. return count;
  61. }
  62. function report(node, depth) {
  63. context.report({
  64. node,
  65. messageId: 'wrongDepth',
  66. data: {
  67. found: depth,
  68. needed: maxDepth
  69. }
  70. });
  71. }
  72. function findJSXElementOrFragment(variables, name, previousReferences) {
  73. function find(refs, prevRefs) {
  74. let i = refs.length;
  75. while (--i >= 0) {
  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++;
  99. (children || []).forEach((node) => {
  100. if (!hasJSX(node)) {
  101. return;
  102. }
  103. if (baseDepth > maxDepth) {
  104. report(node, baseDepth);
  105. } else if (!isLeaf(node)) {
  106. checkDescendant(baseDepth, node.children);
  107. }
  108. });
  109. }
  110. function handleJSX(node) {
  111. if (!isLeaf(node)) {
  112. return;
  113. }
  114. const depth = getDepth(node);
  115. if (depth > maxDepth) {
  116. report(node, depth);
  117. }
  118. }
  119. return {
  120. JSXElement: handleJSX,
  121. JSXFragment: handleJSX,
  122. JSXExpressionContainer(node) {
  123. if (node.expression.type !== 'Identifier') {
  124. return;
  125. }
  126. const variables = variableUtil.variablesInScope(context);
  127. const element = findJSXElementOrFragment(variables, node.expression.name, []);
  128. if (element) {
  129. const baseDepth = getDepth(node);
  130. checkDescendant(baseDepth, element.children);
  131. }
  132. }
  133. };
  134. }
  135. };