source: imaps-frontend/node_modules/eslint-plugin-react/lib/rules/no-children-prop.js@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[d565449]1/**
2 * @fileoverview Prevent passing of children as props
3 * @author Benjamin Stepp
4 */
5
6'use strict';
7
8const docsUrl = require('../util/docsUrl');
9const isCreateElement = require('../util/isCreateElement');
10const 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.
[0c6b92a]20 * @returns {boolean} - True if node is a createElement call with a props
[d565449]21 * object literal, False if not.
22*/
23function 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
33const 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
[0c6b92a]40/** @type {import('eslint').Rule.RuleModule} */
[d565449]41module.exports = {
42 meta: {
43 docs: {
44 description: 'Disallow passing of children as props',
45 category: 'Best Practices',
46 recommended: true,
47 url: docsUrl('no-children-prop'),
48 },
49
50 messages,
51
52 schema: [{
53 type: 'object',
54 properties: {
55 allowFunctions: {
56 type: 'boolean',
57 default: false,
58 },
59 },
60 additionalProperties: false,
61 }],
62 },
63 create(context) {
64 const configuration = context.options[0] || {};
65
66 function isFunction(node) {
67 return configuration.allowFunctions && (node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression');
68 }
69
70 return {
71 JSXAttribute(node) {
72 if (node.name.name !== 'children') {
73 return;
74 }
75
76 const value = node.value;
77 if (value && value.type === 'JSXExpressionContainer' && isFunction(value.expression)) {
78 return;
79 }
80
81 report(context, messages.nestChildren, 'nestChildren', {
82 node,
83 });
84 },
85 CallExpression(node) {
86 if (!isCreateElementWithProps(node, context)) {
87 return;
88 }
89
[0c6b92a]90 const props = 'properties' in node.arguments[1] ? node.arguments[1].properties : undefined;
91 const childrenProp = props.find((prop) => (
92 'key' in prop
93 && prop.key
94 && 'name' in prop.key
95 && prop.key.name === 'children'
96 ));
[d565449]97
98 if (childrenProp) {
[0c6b92a]99 if ('value' in childrenProp && childrenProp.value && !isFunction(childrenProp.value)) {
[d565449]100 report(context, messages.passChildrenAsArgs, 'passChildrenAsArgs', {
101 node,
102 });
103 }
104 } else if (node.arguments.length === 3) {
105 const children = node.arguments[2];
106 if (isFunction(children)) {
107 report(context, messages.passFunctionAsArgs, 'passFunctionAsArgs', {
108 node,
109 });
110 }
111 }
112 },
113 JSXElement(node) {
114 const children = node.children;
115 if (children && children.length === 1 && children[0].type === 'JSXExpressionContainer') {
116 if (isFunction(children[0].expression)) {
117 report(context, messages.nestFunction, 'nestFunction', {
118 node,
119 });
120 }
121 }
122 },
123 };
124 },
125};
Note: See TracBrowser for help on using the repository browser.