From 0d9ef7b5e7399008dc9c779b47fc6e05c61ce581 Mon Sep 17 00:00:00 2001 From: Veda Date: Sun, 6 Jun 2021 21:35:51 +0530 Subject: [PATCH] fix PR comment (improve detection) + update old tests --- lib/util/Components.js | 7 +++-- tests/lib/rules/destructuring-assignment.js | 24 ++++++++++++++- tests/lib/rules/no-multi-comp.js | 4 +-- .../rules/no-unstable-nested-components.js | 29 +++++++++---------- 4 files changed, 43 insertions(+), 21 deletions(-) diff --git a/lib/util/Components.js b/lib/util/Components.js index a3e2ce525a..0ba329dae5 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -651,7 +651,7 @@ function componentRule(rule, context) { return undefined; } if (utils.isInAllowedPositionForComponent(node) && utils.isReturningJSXOrNull(node)) { - if (utils.isArrowOrFunctionExpressionRenderWithParams(node)) return undefined; + if (utils.isParentComponentNotStatelessComponent(node)) return undefined; const isMethod = node.parent.type === 'Property' && node.parent.method; @@ -802,12 +802,13 @@ function componentRule(rule, context) { return components.add(componentNode, 1); }, - isArrowOrFunctionExpressionRenderWithParams(node) { + isParentComponentNotStatelessComponent(node) { return ( node.parent && node.parent.key && node.parent.key.type === 'Identifier' - && node.parent.key.name === 'render' + // custom component functions must start with a capital letter (returns false otherwise) + && node.parent.key.name.charAt(0) === node.parent.key.name.charAt(0).toLowerCase() // react render function cannot have params && !!(node.params || []).length ); diff --git a/tests/lib/rules/destructuring-assignment.js b/tests/lib/rules/destructuring-assignment.js index e6f039ff96..bf6eb46fa9 100644 --- a/tests/lib/rules/destructuring-assignment.js +++ b/tests/lib/rules/destructuring-assignment.js @@ -220,7 +220,7 @@ ruleTester.run('destructuring-assignment', rule, { render: val => {val}, }, { - render: function(val) { + someRenderFunc: function(val) { if (val.url) { return ( @@ -390,5 +390,27 @@ ruleTester.run('destructuring-assignment', rule, { messageId: 'noDestructAssignment', data: {type: 'state'} }] + }, { + code: ` + const columns = [ + { + CustomComponentName: function(props) { + if (props.url) { + return ( + + {props.test} + + ); + } + return null; + }, + }, + ]; + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + messageId: 'useDestructAssignment', + data: {type: 'props'} + }] }] }); diff --git a/tests/lib/rules/no-multi-comp.js b/tests/lib/rules/no-multi-comp.js index e2cbdc18a8..8c9bd824b4 100644 --- a/tests/lib/rules/no-multi-comp.js +++ b/tests/lib/rules/no-multi-comp.js @@ -316,11 +316,11 @@ ruleTester.run('no-multi-comp', rule, { }, { code: [ 'export default {', - ' renderHello(props) {', + ' RenderHello(props) {', ' let {name} = props;', ' return
{name}
;', ' },', - ' renderHello2(props) {', + ' RenderHello2(props) {', ' let {name} = props;', ' return
{name}
;', ' }', diff --git a/tests/lib/rules/no-unstable-nested-components.js b/tests/lib/rules/no-unstable-nested-components.js index d6df486d86..159816af8b 100644 --- a/tests/lib/rules/no-unstable-nested-components.js +++ b/tests/lib/rules/no-unstable-nested-components.js @@ -490,6 +490,20 @@ ruleTester.run('no-unstable-nested-components', rule, { }, }); ` + }, + { + code: ` + function ParentComponent() { + const rows = [ + { + name: 'A', + notPrefixedWithRender: (props) => + }, + ]; + + return ; + } + ` } /* TODO These minor cases are currently falsely marked due to component detection { @@ -1010,21 +1024,6 @@ ruleTester.run('no-unstable-nested-components', rule, { `, errors: [{message: ERROR_MESSAGE_COMPONENT_AS_PROPS}] }, - { - code: ` - function ParentComponent() { - const rows = [ - { - name: 'A', - notPrefixedWithRender: (props) => - }, - ]; - - return
; - } - `, - errors: [{message: ERROR_MESSAGE}] - }, { code: ` class ParentComponent extends React.Component {