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