Skip to content

Commit 0d9ef7b

Browse files
committed
fix PR comment (improve detection) + update old tests
1 parent 79a7bcc commit 0d9ef7b

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

lib/util/Components.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ function componentRule(rule, context) {
651651
return undefined;
652652
}
653653
if (utils.isInAllowedPositionForComponent(node) && utils.isReturningJSXOrNull(node)) {
654-
if (utils.isArrowOrFunctionExpressionRenderWithParams(node)) return undefined;
654+
if (utils.isParentComponentNotStatelessComponent(node)) return undefined;
655655

656656
const isMethod = node.parent.type === 'Property' && node.parent.method;
657657

@@ -802,12 +802,13 @@ function componentRule(rule, context) {
802802
return components.add(componentNode, 1);
803803
},
804804

805-
isArrowOrFunctionExpressionRenderWithParams(node) {
805+
isParentComponentNotStatelessComponent(node) {
806806
return (
807807
node.parent
808808
&& node.parent.key
809809
&& node.parent.key.type === 'Identifier'
810-
&& node.parent.key.name === 'render'
810+
// custom component functions must start with a capital letter (returns false otherwise)
811+
&& node.parent.key.name.charAt(0) === node.parent.key.name.charAt(0).toLowerCase()
811812
// react render function cannot have params
812813
&& !!(node.params || []).length
813814
);

tests/lib/rules/destructuring-assignment.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ ruleTester.run('destructuring-assignment', rule, {
220220
render: val => <span>{val}</span>,
221221
},
222222
{
223-
render: function(val) {
223+
someRenderFunc: function(val) {
224224
if (val.url) {
225225
return (
226226
<a href={val.url}>
@@ -390,5 +390,27 @@ ruleTester.run('destructuring-assignment', rule, {
390390
messageId: 'noDestructAssignment',
391391
data: {type: 'state'}
392392
}]
393+
}, {
394+
code: `
395+
const columns = [
396+
{
397+
CustomComponentName: function(props) {
398+
if (props.url) {
399+
return (
400+
<a href={props.url}>
401+
{props.test}
402+
</a>
403+
);
404+
}
405+
return null;
406+
},
407+
},
408+
];
409+
`,
410+
parser: parsers.BABEL_ESLINT,
411+
errors: [{
412+
messageId: 'useDestructAssignment',
413+
data: {type: 'props'}
414+
}]
393415
}]
394416
});

tests/lib/rules/no-multi-comp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,11 @@ ruleTester.run('no-multi-comp', rule, {
316316
}, {
317317
code: [
318318
'export default {',
319-
' renderHello(props) {',
319+
' RenderHello(props) {',
320320
' let {name} = props;',
321321
' return <div>{name}</div>;',
322322
' },',
323-
' renderHello2(props) {',
323+
' RenderHello2(props) {',
324324
' let {name} = props;',
325325
' return <div>{name}</div>;',
326326
' }',

tests/lib/rules/no-unstable-nested-components.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,20 @@ ruleTester.run('no-unstable-nested-components', rule, {
490490
},
491491
});
492492
`
493+
},
494+
{
495+
code: `
496+
function ParentComponent() {
497+
const rows = [
498+
{
499+
name: 'A',
500+
notPrefixedWithRender: (props) => <Row {...props} />
501+
},
502+
];
503+
504+
return <Table rows={rows} />;
505+
}
506+
`
493507
}
494508
/* TODO These minor cases are currently falsely marked due to component detection
495509
{
@@ -1010,21 +1024,6 @@ ruleTester.run('no-unstable-nested-components', rule, {
10101024
`,
10111025
errors: [{message: ERROR_MESSAGE_COMPONENT_AS_PROPS}]
10121026
},
1013-
{
1014-
code: `
1015-
function ParentComponent() {
1016-
const rows = [
1017-
{
1018-
name: 'A',
1019-
notPrefixedWithRender: (props) => <Row {...props} />
1020-
},
1021-
];
1022-
1023-
return <Table rows={rows} />;
1024-
}
1025-
`,
1026-
errors: [{message: ERROR_MESSAGE}]
1027-
},
10281027
{
10291028
code: `
10301029
class ParentComponent extends React.Component {

0 commit comments

Comments
 (0)