Skip to content

Commit 79a7bcc

Browse files
committed
[Fix]: destructuring-assignment, Components: improve component detection
1 parent faadccb commit 79a7bcc

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

lib/util/Components.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,8 @@ function componentRule(rule, context) {
651651
return undefined;
652652
}
653653
if (utils.isInAllowedPositionForComponent(node) && utils.isReturningJSXOrNull(node)) {
654+
if (utils.isArrowOrFunctionExpressionRenderWithParams(node)) return undefined;
655+
654656
const isMethod = node.parent.type === 'Property' && node.parent.method;
655657

656658
if (isMethod && !isFirstLetterCapitalized(node.parent.key.name)) {
@@ -798,6 +800,17 @@ function componentRule(rule, context) {
798800

799801
// Return the component
800802
return components.add(componentNode, 1);
803+
},
804+
805+
isArrowOrFunctionExpressionRenderWithParams(node) {
806+
return (
807+
node.parent
808+
&& node.parent.key
809+
&& node.parent.key.type === 'Identifier'
810+
&& node.parent.key.name === 'render'
811+
// react render function cannot have params
812+
&& !!(node.params || []).length
813+
);
801814
}
802815
};
803816

@@ -846,6 +859,7 @@ function componentRule(rule, context) {
846859
components.add(node, 0);
847860
return;
848861
}
862+
849863
const component = utils.getParentComponent();
850864
if (
851865
!component
@@ -863,6 +877,7 @@ function componentRule(rule, context) {
863877
components.add(node, 0);
864878
return;
865879
}
880+
866881
node = utils.getParentComponent();
867882
if (!node) {
868883
return;
@@ -875,7 +890,9 @@ function componentRule(rule, context) {
875890
components.add(node, 0);
876891
return;
877892
}
893+
878894
const component = utils.getParentComponent();
895+
879896
if (
880897
!component
881898
|| (component.parent && component.parent.type === 'JSXExpressionContainer')

tests/lib/rules/destructuring-assignment.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,43 @@ ruleTester.run('destructuring-assignment', rule, {
196196
},
197197
};
198198
`
199+
}, {
200+
code: `
201+
const columns = [
202+
{
203+
render: (val) => {
204+
if (val.url) {
205+
return (
206+
<a href={val.url}>
207+
{val.test}
208+
</a>
209+
);
210+
}
211+
return null;
212+
},
213+
},
214+
];
215+
`
216+
}, {
217+
code: `
218+
const columns = [
219+
{
220+
render: val => <span>{val}</span>,
221+
},
222+
{
223+
render: function(val) {
224+
if (val.url) {
225+
return (
226+
<a href={val.url}>
227+
{val.test}
228+
</a>
229+
);
230+
}
231+
return null;
232+
},
233+
},
234+
];
235+
`
199236
}],
200237

201238
invalid: [{

0 commit comments

Comments
 (0)