You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

149 rivejä
3.5 KiB

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