123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- /**
- * @fileoverview Prevent passing of children as props
- * @author Benjamin Stepp
- */
- 'use strict';
- const docsUrl = require('../util/docsUrl');
- const isCreateElement = require('../util/isCreateElement');
- const report = require('../util/report');
- // ------------------------------------------------------------------------------
- // Helpers
- // ------------------------------------------------------------------------------
- /**
- * Checks if the node is a createElement call with a props literal.
- * @param {ASTNode} node - The AST node being checked.
- * @param {Context} context - The AST node being checked.
- * @returns {Boolean} - True if node is a createElement call with a props
- * object literal, False if not.
- */
- function isCreateElementWithProps(node, context) {
- return isCreateElement(node, context)
- && node.arguments.length > 1
- && node.arguments[1].type === 'ObjectExpression';
- }
- // ------------------------------------------------------------------------------
- // Rule Definition
- // ------------------------------------------------------------------------------
- const messages = {
- nestChildren: 'Do not pass children as props. Instead, nest children between the opening and closing tags.',
- passChildrenAsArgs: 'Do not pass children as props. Instead, pass them as additional arguments to React.createElement.',
- nestFunction: 'Do not nest a function between the opening and closing tags. Instead, pass it as a prop.',
- passFunctionAsArgs: 'Do not pass a function as an additional argument to React.createElement. Instead, pass it as a prop.',
- };
- module.exports = {
- meta: {
- docs: {
- description: 'Disallow passing of children as props',
- category: 'Best Practices',
- recommended: true,
- url: docsUrl('no-children-prop'),
- },
- messages,
- schema: [{
- type: 'object',
- properties: {
- allowFunctions: {
- type: 'boolean',
- default: false,
- },
- },
- additionalProperties: false,
- }],
- },
- create(context) {
- const configuration = context.options[0] || {};
- function isFunction(node) {
- return configuration.allowFunctions && (node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression');
- }
- return {
- JSXAttribute(node) {
- if (node.name.name !== 'children') {
- return;
- }
- const value = node.value;
- if (value && value.type === 'JSXExpressionContainer' && isFunction(value.expression)) {
- return;
- }
- report(context, messages.nestChildren, 'nestChildren', {
- node,
- });
- },
- CallExpression(node) {
- if (!isCreateElementWithProps(node, context)) {
- return;
- }
- const props = node.arguments[1].properties;
- const childrenProp = props.find((prop) => prop.key && prop.key.name === 'children');
- if (childrenProp) {
- if (childrenProp.value && !isFunction(childrenProp.value)) {
- report(context, messages.passChildrenAsArgs, 'passChildrenAsArgs', {
- node,
- });
- }
- } else if (node.arguments.length === 3) {
- const children = node.arguments[2];
- if (isFunction(children)) {
- report(context, messages.passFunctionAsArgs, 'passFunctionAsArgs', {
- node,
- });
- }
- }
- },
- JSXElement(node) {
- const children = node.children;
- if (children && children.length === 1 && children[0].type === 'JSXExpressionContainer') {
- if (isFunction(children[0].expression)) {
- report(context, messages.nestFunction, 'nestFunction', {
- node,
- });
- }
- }
- },
- };
- },
- };
|