Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] no-unstable-components: improve handling of objects containing render functions #3111

Merged
merged 1 commit into from Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -16,7 +16,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`no-unused-prop-types`], `usedPropTypes`: avoid crash with typescript-eslint parser (@ljharb)
* [`display-name`]: unwrap TS `as` expressions ([#3110][] @ljharb)
* [`destructuring-assignment`]: detect refs nested in functions ([#3102] @ljharb)
* [`no-unstable-components`]: improve handling of objects containing render function properties ([#3111] @fizwidget)

[#3111]: https://github.com/yannickcr/eslint-plugin-react/pull/3111
[#3110]: https://github.com/yannickcr/eslint-plugin-react/pull/3110
[#3102]: https://github.com/yannickcr/eslint-plugin-react/issue/3102
[#3092]: https://github.com/yannickcr/eslint-plugin-react/pull/3092
Expand Down
20 changes: 19 additions & 1 deletion lib/rules/no-unstable-nested-components.js
Expand Up @@ -105,6 +105,19 @@ function isJSXAttributeOfExpressionContainerMatcher(node) {
);
}

/**
* 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
Expand Down Expand Up @@ -358,14 +371,19 @@ module.exports = {
}

/**
* Check whether given node is declared inside a component prop.
* 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) {
Expand Down
133 changes: 133 additions & 0 deletions tests/lib/rules/no-unstable-nested-components.js
Expand Up @@ -383,6 +383,78 @@ ruleTester.run('no-unstable-nested-components', rule, {
`,
options: [{ allowAsProps: true }],
},
{
code: `
function ParentComponent() {
return (
<SomeComponent>
{
thing.match({
renderLoading: () => <div />,
renderSuccess: () => <div />,
renderFailure: () => <div />,
})
}
</SomeComponent>
)
}
`,
},
{
code: `
function ParentComponent() {
const thingElement = thing.match({
renderLoading: () => <div />,
renderSuccess: () => <div />,
renderFailure: () => <div />,
});
return (
<SomeComponent>
{thingElement}
</SomeComponent>
)
}
Comment on lines +405 to +416
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function ParentComponent() {
const thingElement = thing.match({
renderLoading: () => <div />,
renderSuccess: () => <div />,
renderFailure: () => <div />,
});
return (
<SomeComponent>
{thingElement}
</SomeComponent>
)
}
function ParentComponent() {
const thingElement = thing.match({
renderLoading: () => <div />,
renderSuccess: () => <div />,
renderFailure: () => <div />,
});
return (
<SomeComponent>
{thingElement}
</SomeComponent>
)
}

same change throughout

`,
},
{
code: `
function ParentComponent() {
return (
<SomeComponent>
{
thing.match({
loading: () => <div />,
success: () => <div />,
failure: () => <div />,
})
}
</SomeComponent>
)
}
`,
options: [{
allowAsProps: true,
}],
},
{
code: `
function ParentComponent() {
const thingElement = thing.match({
loading: () => <div />,
success: () => <div />,
failure: () => <div />,
});
return (
<SomeComponent>
{thingElement}
</SomeComponent>
)
}
`,
options: [{
allowAsProps: true,
}],
},
{
code: `
function ParentComponent() {
Expand Down Expand Up @@ -500,6 +572,9 @@ ruleTester.run('no-unstable-nested-components', rule, {
return <Table rows={rows} />;
}
`,
options: [{
allowAsProps: true,
}],
Comment on lines +575 to +577
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
options: [{
allowAsProps: true,
}],
options: [{ allowAsProps: true }],

same change throughout

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this case was previously allowed without allowAsProps. There might be something I'm missing here..?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, this should not have been an allowed pattern. Requiring the { allowedAsProps: true } here is correct.

It was marked as error by initial implementation. It seems there were some changes made to general component detection which affected this. 860ebea#diff-2c6ecc4f3063d33adbe4b551631a53d57725100435423ed732f4e8ea752d821aL1013

},
/* TODO These minor cases are currently falsely marked due to component detection
{
Expand Down Expand Up @@ -1042,5 +1117,63 @@ ruleTester.run('no-unstable-nested-components', rule, {
// Only a single error should be shown. This can get easily marked twice.
errors: [{ message: ERROR_MESSAGE }],
},
{
code: `
function ParentComponent() {
return (
<SomeComponent>
{
thing.match({
loading: () => <div />,
success: () => <div />,
failure: () => <div />,
})
}
</SomeComponent>
)
}
`,
errors: [
{ message: ERROR_MESSAGE_COMPONENT_AS_PROPS },
{ message: ERROR_MESSAGE_COMPONENT_AS_PROPS },
{ message: ERROR_MESSAGE_COMPONENT_AS_PROPS },
],
},
{
code: `
function ParentComponent() {
const thingElement = thing.match({
loading: () => <div />,
success: () => <div />,
failure: () => <div />,
});
return (
<SomeComponent>
{thingElement}
</SomeComponent>
)
}
`,
errors: [
{ message: ERROR_MESSAGE_COMPONENT_AS_PROPS },
{ message: ERROR_MESSAGE_COMPONENT_AS_PROPS },
{ message: ERROR_MESSAGE_COMPONENT_AS_PROPS },
],
},
{
code: `
function ParentComponent() {
const rows = [
{
name: 'A',
notPrefixedWithRender: (props) => <Row {...props} />
},
];

return <Table rows={rows} />;
}
`,
errors: [{ message: ERROR_MESSAGE_COMPONENT_AS_PROPS }],
},
]),
});