forked from jsx-eslint/eslint-plugin-react
/
no-children-prop.js
120 lines (106 loc) · 3.6 KB
/
no-children-prop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* @fileoverview Prevent passing of children as props
* @author Benjamin Stepp
*/
'use strict';
const docsUrl = require('../util/docsUrl');
const isCreateElement = require('../util/isCreateElement');
// ------------------------------------------------------------------------------
// 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
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'Prevent passing of children as props.',
category: 'Best Practices',
recommended: true,
url: docsUrl('no-children-prop')
},
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.'
},
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;
}
context.report({
node,
messageId: 'nestChildren'
});
},
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)) {
context.report({
node,
messageId: 'passChildrenAsArgs'
});
}
} else if (node.arguments.length === 3) {
const children = node.arguments[2];
if (isFunction(children)) {
context.report({
node,
messageId: 'passFunctionAsArgs'
});
}
}
},
JSXElement(node) {
const children = node.children;
if (children && children.length === 1 && children[0].type === 'JSXExpressionContainer') {
if (isFunction(children[0].expression)) {
context.report({
node,
messageId: 'nestFunction'
});
}
}
}
};
}
};