jsx-max-depth.js 3.4 KB

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