-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
no-unstable-nested-components.js
491 lines (428 loc) · 14.8 KB
/
no-unstable-nested-components.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/**
* @fileoverview Prevent creating unstable components inside components
* @author Ari Perkkiö
*/
'use strict';
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const isCreateElement = require('../util/isCreateElement');
const report = require('../util/report');
// ------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------
const COMPONENT_AS_PROPS_INFO = ' If you want to allow component creation in props, set allowAsProps option to true.';
const HOOK_REGEXP = /^use[A-Z0-9].*$/;
// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
/**
* Generate error message with given parent component name
* @param {String} parentName Name of the parent component, if known
* @returns {String} Error message with parent component name
*/
function generateErrorMessageWithParentName(parentName) {
return `Do not define components during render. React will see a new component type on every render and destroy the entire subtree’s DOM nodes and state (https://reactjs.org/docs/reconciliation.html#elements-of-different-types). Instead, move this component definition out of the parent component${parentName ? ` “${parentName}” ` : ' '}and pass data as props.`;
}
/**
* Check whether given text starts with `render`. Comparison is case-sensitive.
* @param {String} text Text to validate
* @returns {Boolean}
*/
function startsWithRender(text) {
return (text || '').startsWith('render');
}
/**
* Get closest parent matching given matcher
* @param {ASTNode} node The AST node
* @param {Context} context eslint context
* @param {Function} matcher Method used to match the parent
* @returns {ASTNode} The matching parent node, if any
*/
function getClosestMatchingParent(node, context, matcher) {
if (!node || !node.parent || node.parent.type === 'Program') {
return;
}
if (matcher(node.parent, context)) {
return node.parent;
}
return getClosestMatchingParent(node.parent, context, matcher);
}
/**
* Matcher used to check whether given node is a `createElement` call
* @param {ASTNode} node The AST node
* @param {Context} context eslint context
* @returns {Boolean} True if node is a `createElement` call, false if not
*/
function isCreateElementMatcher(node, context) {
return (
node
&& node.type === 'CallExpression'
&& isCreateElement(node, context)
);
}
/**
* Matcher used to check whether given node is a `ObjectExpression`
* @param {ASTNode} node The AST node
* @returns {Boolean} True if node is a `ObjectExpression`, false if not
*/
function isObjectExpressionMatcher(node) {
return node && node.type === 'ObjectExpression';
}
/**
* Matcher used to check whether given node is a `JSXExpressionContainer`
* @param {ASTNode} node The AST node
* @returns {Boolean} True if node is a `JSXExpressionContainer`, false if not
*/
function isJSXExpressionContainerMatcher(node) {
return node && node.type === 'JSXExpressionContainer';
}
/**
* Matcher used to check whether given node is a `JSXAttribute` of `JSXExpressionContainer`
* @param {ASTNode} node The AST node
* @returns {Boolean} True if node is a `JSXAttribute` of `JSXExpressionContainer`, false if not
*/
function isJSXAttributeOfExpressionContainerMatcher(node) {
return (
node
&& node.type === 'JSXAttribute'
&& node.value
&& node.value.type === 'JSXExpressionContainer'
);
}
/**
* Matcher used to check whether given node is an object `Property`
* @param {ASTNode} node The AST node
* @returns {Boolean} True if node is a `Property`, false if not
*/
function isPropertyOfObjectExpressionMatcher(node) {
return (
node
&& node.parent
&& node.parent.type === 'Property'
);
}
/**
* Matcher used to check whether given node is a `CallExpression`
* @param {ASTNode} node The AST node
* @returns {Boolean} True if node is a `CallExpression`, false if not
*/
function isCallExpressionMatcher(node) {
return node && node.type === 'CallExpression';
}
/**
* Check whether given node or its parent is directly inside `map` call
* ```jsx
* {items.map(item => <li />)}
* ```
* @param {ASTNode} node The AST node
* @returns {Boolean} True if node is directly inside `map` call, false if not
*/
function isMapCall(node) {
return (
node
&& node.callee
&& node.callee.property
&& node.callee.property.name === 'map'
);
}
/**
* Check whether given node is `ReturnStatement` of a React hook
* @param {ASTNode} node The AST node
* @param {Context} context eslint context
* @returns {Boolean} True if node is a `ReturnStatement` of a React hook, false if not
*/
function isReturnStatementOfHook(node, context) {
if (
!node
|| !node.parent
|| node.parent.type !== 'ReturnStatement'
) {
return false;
}
const callExpression = getClosestMatchingParent(node, context, isCallExpressionMatcher);
return (
callExpression
&& callExpression.callee
&& HOOK_REGEXP.test(callExpression.callee.name)
);
}
/**
* Check whether given node is declared inside a render prop
* ```jsx
* <Component renderFooter={() => <div />} />
* <Component>{() => <div />}</Component>
* ```
* @param {ASTNode} node The AST node
* @param {Context} context eslint context
* @returns {Boolean} True if component is declared inside a render prop, false if not
*/
function isComponentInRenderProp(node, context) {
if (
node
&& node.parent
&& node.parent.type === 'Property'
&& node.parent.key
&& startsWithRender(node.parent.key.name)
) {
return true;
}
// Check whether component is a render prop used as direct children, e.g. <Component>{() => <div />}</Component>
if (
node
&& node.parent
&& node.parent.type === 'JSXExpressionContainer'
&& node.parent.parent
&& node.parent.parent.type === 'JSXElement'
) {
return true;
}
const jsxExpressionContainer = getClosestMatchingParent(node, context, isJSXExpressionContainerMatcher);
// Check whether prop name indicates accepted patterns
if (
jsxExpressionContainer
&& jsxExpressionContainer.parent
&& jsxExpressionContainer.parent.type === 'JSXAttribute'
&& jsxExpressionContainer.parent.name
&& jsxExpressionContainer.parent.name.type === 'JSXIdentifier'
) {
const propName = jsxExpressionContainer.parent.name.name;
// Starts with render, e.g. <Component renderFooter={() => <div />} />
if (startsWithRender(propName)) {
return true;
}
// Uses children prop explicitly, e.g. <Component children={() => <div />} />
if (propName === 'children') {
return true;
}
}
return false;
}
/**
* Check whether given node is declared directly inside a render property
* ```jsx
* const rows = { render: () => <div /> }
* <Component rows={ [{ render: () => <div /> }] } />
* ```
* @param {ASTNode} node The AST node
* @returns {Boolean} True if component is declared inside a render property, false if not
*/
function isDirectValueOfRenderProperty(node) {
return (
node
&& node.parent
&& node.parent.type === 'Property'
&& node.parent.key
&& node.parent.key.type === 'Identifier'
&& startsWithRender(node.parent.key.name)
);
}
/**
* Resolve the component name of given node
* @param {ASTNode} node The AST node of the component
* @returns {String} Name of the component, if any
*/
function resolveComponentName(node) {
const parentName = node.id && node.id.name;
if (parentName) return parentName;
return (
node.type === 'ArrowFunctionExpression'
&& node.parent
&& node.parent.id
&& node.parent.id.name
);
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'Disallow creating unstable components inside components',
category: 'Possible Errors',
recommended: false,
url: docsUrl('no-unstable-nested-components'),
},
schema: [{
type: 'object',
properties: {
customValidators: {
type: 'array',
items: {
type: 'string',
},
},
allowAsProps: {
type: 'boolean',
},
},
additionalProperties: false,
}],
},
create: Components.detect((context, components, utils) => {
const allowAsProps = context.options.some((option) => option && option.allowAsProps);
/**
* Check whether given node is declared inside class component's render block
* ```jsx
* class Component extends React.Component {
* render() {
* class NestedClassComponent extends React.Component {
* ...
* ```
* @param {ASTNode} node The AST node being checked
* @returns {Boolean} True if node is inside class component's render block, false if not
*/
function isInsideRenderMethod(node) {
const parentComponent = utils.getParentComponent();
if (!parentComponent || parentComponent.type !== 'ClassDeclaration') {
return false;
}
return (
node
&& node.parent
&& node.parent.type === 'MethodDefinition'
&& node.parent.key
&& node.parent.key.name === 'render'
);
}
/**
* Check whether given node is a function component declared inside class component.
* Util's component detection fails to detect function components inside class components.
* ```jsx
* class Component extends React.Component {
* render() {
* const NestedComponent = () => <div />;
* ...
* ```
* @param {ASTNode} node The AST node being checked
* @returns {Boolean} True if given node a function component declared inside class component, false if not
*/
function isFunctionComponentInsideClassComponent(node) {
const parentComponent = utils.getParentComponent();
const parentStatelessComponent = utils.getParentStatelessComponent();
return (
parentComponent
&& parentStatelessComponent
&& parentComponent.type === 'ClassDeclaration'
&& utils.getStatelessComponent(parentStatelessComponent)
&& utils.isReturningJSX(node)
);
}
/**
* Check whether given node is declared inside `createElement` call's props
* ```js
* React.createElement(Component, {
* footer: () => React.createElement("div", null)
* })
* ```
* @param {ASTNode} node The AST node
* @returns {Boolean} True if node is declare inside `createElement` call's props, false if not
*/
function isComponentInsideCreateElementsProp(node) {
if (!components.get(node)) {
return false;
}
const createElementParent = getClosestMatchingParent(node, context, isCreateElementMatcher);
return (
createElementParent
&& createElementParent.arguments
&& createElementParent.arguments[1] === getClosestMatchingParent(node, context, isObjectExpressionMatcher)
);
}
/**
* Check whether given node is declared inside a component/object prop.
* ```jsx
* <Component footer={() => <div />} />
* { footer: () => <div /> }
* ```
* @param {ASTNode} node The AST node being checked
* @returns {Boolean} True if node is a component declared inside prop, false if not
*/
function isComponentInProp(node) {
if (isPropertyOfObjectExpressionMatcher(node)) {
return utils.isReturningJSX(node);
}
const jsxAttribute = getClosestMatchingParent(node, context, isJSXAttributeOfExpressionContainerMatcher);
if (!jsxAttribute) {
return isComponentInsideCreateElementsProp(node);
}
return utils.isReturningJSX(node);
}
/**
* Check whether given node is a stateless component returning non-JSX
* ```jsx
* {{ a: () => null }}
* ```
* @param {ASTNode} node The AST node being checked
* @returns {Boolean} True if node is a stateless component returning non-JSX, false if not
*/
function isStatelessComponentReturningNull(node) {
const component = utils.getStatelessComponent(node);
return component && !utils.isReturningJSX(component);
}
/**
* Check whether given node is a unstable nested component
* @param {ASTNode} node The AST node being checked
*/
function validate(node) {
if (!node || !node.parent) {
return;
}
const isDeclaredInsideProps = isComponentInProp(node);
if (
!components.get(node)
&& !isFunctionComponentInsideClassComponent(node)
&& !isDeclaredInsideProps) {
return;
}
if (
// Support allowAsProps option
(isDeclaredInsideProps && (allowAsProps || isComponentInRenderProp(node, context)))
// Prevent reporting components created inside Array.map calls
|| isMapCall(node)
|| isMapCall(node.parent)
// Do not mark components declared inside hooks (or falsy '() => null' clean-up methods)
|| isReturnStatementOfHook(node, context)
// Do not mark objects containing render methods
|| isDirectValueOfRenderProperty(node)
// Prevent reporting nested class components twice
|| isInsideRenderMethod(node)
// Prevent falsely reporting detected "components" which do not return JSX
|| isStatelessComponentReturningNull(node)
) {
return;
}
// Get the closest parent component
const parentComponent = getClosestMatchingParent(
node,
context,
(nodeToMatch) => components.get(nodeToMatch)
);
if (parentComponent) {
const parentName = resolveComponentName(parentComponent);
// Exclude lowercase parents, e.g. function createTestComponent()
// React-dom prevents creating lowercase components
if (parentName && parentName[0] === parentName[0].toLowerCase()) {
return;
}
let message = generateErrorMessageWithParentName(parentName);
// Add information about allowAsProps option when component is declared inside prop
if (isDeclaredInsideProps && !allowAsProps) {
message += COMPONENT_AS_PROPS_INFO;
}
report(context, message, null, {
node,
});
}
}
// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------
return {
FunctionDeclaration(node) { validate(node); },
ArrowFunctionExpression(node) { validate(node); },
FunctionExpression(node) { validate(node); },
ClassDeclaration(node) { validate(node); },
CallExpression(node) { validate(node); },
};
}),
};