no-children-prop.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @fileoverview Prevent passing of children as props
  3. * @author Benjamin Stepp
  4. */
  5. 'use strict';
  6. const docsUrl = require('../util/docsUrl');
  7. const isCreateElement = require('../util/isCreateElement');
  8. const report = require('../util/report');
  9. // ------------------------------------------------------------------------------
  10. // Helpers
  11. // ------------------------------------------------------------------------------
  12. /**
  13. * Checks if the node is a createElement call with a props literal.
  14. * @param {ASTNode} node - The AST node being checked.
  15. * @param {Context} context - The AST node being checked.
  16. * @returns {Boolean} - True if node is a createElement call with a props
  17. * object literal, False if not.
  18. */
  19. function isCreateElementWithProps(node, context) {
  20. return isCreateElement(node, context)
  21. && node.arguments.length > 1
  22. && node.arguments[1].type === 'ObjectExpression';
  23. }
  24. // ------------------------------------------------------------------------------
  25. // Rule Definition
  26. // ------------------------------------------------------------------------------
  27. const messages = {
  28. nestChildren: 'Do not pass children as props. Instead, nest children between the opening and closing tags.',
  29. passChildrenAsArgs: 'Do not pass children as props. Instead, pass them as additional arguments to React.createElement.',
  30. nestFunction: 'Do not nest a function between the opening and closing tags. Instead, pass it as a prop.',
  31. passFunctionAsArgs: 'Do not pass a function as an additional argument to React.createElement. Instead, pass it as a prop.',
  32. };
  33. module.exports = {
  34. meta: {
  35. docs: {
  36. description: 'Disallow passing of children as props',
  37. category: 'Best Practices',
  38. recommended: true,
  39. url: docsUrl('no-children-prop'),
  40. },
  41. messages,
  42. schema: [{
  43. type: 'object',
  44. properties: {
  45. allowFunctions: {
  46. type: 'boolean',
  47. default: false,
  48. },
  49. },
  50. additionalProperties: false,
  51. }],
  52. },
  53. create(context) {
  54. const configuration = context.options[0] || {};
  55. function isFunction(node) {
  56. return configuration.allowFunctions && (node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression');
  57. }
  58. return {
  59. JSXAttribute(node) {
  60. if (node.name.name !== 'children') {
  61. return;
  62. }
  63. const value = node.value;
  64. if (value && value.type === 'JSXExpressionContainer' && isFunction(value.expression)) {
  65. return;
  66. }
  67. report(context, messages.nestChildren, 'nestChildren', {
  68. node,
  69. });
  70. },
  71. CallExpression(node) {
  72. if (!isCreateElementWithProps(node, context)) {
  73. return;
  74. }
  75. const props = node.arguments[1].properties;
  76. const childrenProp = props.find((prop) => prop.key && prop.key.name === 'children');
  77. if (childrenProp) {
  78. if (childrenProp.value && !isFunction(childrenProp.value)) {
  79. report(context, messages.passChildrenAsArgs, 'passChildrenAsArgs', {
  80. node,
  81. });
  82. }
  83. } else if (node.arguments.length === 3) {
  84. const children = node.arguments[2];
  85. if (isFunction(children)) {
  86. report(context, messages.passFunctionAsArgs, 'passFunctionAsArgs', {
  87. node,
  88. });
  89. }
  90. }
  91. },
  92. JSXElement(node) {
  93. const children = node.children;
  94. if (children && children.length === 1 && children[0].type === 'JSXExpressionContainer') {
  95. if (isFunction(children[0].expression)) {
  96. report(context, messages.nestFunction, 'nestFunction', {
  97. node,
  98. });
  99. }
  100. }
  101. },
  102. };
  103. },
  104. };