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

[New] no-unstable-nested-components: add propNamePattern to support custom render prop naming conventions #3826

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* add type generation ([#3830][] @voxpelli)
* [`no-unescaped-entities`]: add suggestions ([#3831][] @StyleShit)
* [`forbid-component-props`]: add `allowedForPatterns`/`disallowedForPatterns` options ([#3805][] @Efimenko)
* [`no-unstable-nested-components`]: add `propNamePattern` to support custom render prop naming conventions ([#3826][] @danreeves)

[#3831]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3831
[#3830]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3830
[#3826]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3826
[#3805]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3805

## [7.36.1] - 2024.09.12
Expand Down
11 changes: 11 additions & 0 deletions docs/rules/no-unstable-nested-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function Component() {
{
"allowAsProps": true | false,
"customValidators": [] /* optional array of validators used for propTypes validation */
ljharb marked this conversation as resolved.
Show resolved Hide resolved
"propNamePattern": string
}
]
...
Expand All @@ -148,6 +149,16 @@ function Component() {
}
```

You can allow other render prop naming conventions by setting the `propNamePattern` option. By default this option is `"render*"`.

For example, if `propNamePattern` is set to `"*Renderer"` the following pattern is **not** considered warnings:

```jsx
<Table
rowRenderer={(rowData) => <Row data={rowData} />}
/>
```

## When Not To Use It

If you are not interested in preventing bugs related to re-creation of the nested components or do not care about optimization of virtual DOM.
28 changes: 18 additions & 10 deletions lib/rules/no-unstable-nested-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

'use strict';

const minimatch = require('minimatch');
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const astUtil = require('../util/ast');
Expand Down Expand Up @@ -32,12 +33,13 @@ function generateErrorMessageWithParentName(parentName) {
}

/**
* Check whether given text starts with `render`. Comparison is case-sensitive.
* Check whether given text matches the pattern passed in.
* @param {string} text Text to validate
* @param {string} pattern Pattern to match against
* @returns {boolean}
*/
function startsWithRender(text) {
return typeof text === 'string' && text.startsWith('render');
function propMatchesRenderPropPattern(text, pattern) {
return typeof text === 'string' && minimatch(text, pattern);
}

/**
Expand Down Expand Up @@ -165,15 +167,16 @@ function isReturnStatementOfHook(node, context) {
* ```
* @param {ASTNode} node The AST node
* @param {Context} context eslint context
* @param {string} propNamePattern a pattern to match render props against
* @returns {boolean} True if component is declared inside a render prop, false if not
*/
function isComponentInRenderProp(node, context) {
function isComponentInRenderProp(node, context, propNamePattern) {
if (
node
&& node.parent
&& node.parent.type === 'Property'
&& node.parent.key
&& startsWithRender(node.parent.key.name)
&& propMatchesRenderPropPattern(node.parent.key.name, propNamePattern)
) {
return true;
}
Expand Down Expand Up @@ -202,7 +205,7 @@ function isComponentInRenderProp(node, context) {
const propName = jsxExpressionContainer.parent.name.name;

// Starts with render, e.g. <Component renderFooter={() => <div />} />
if (startsWithRender(propName)) {
if (propMatchesRenderPropPattern(propName, propNamePattern)) {
return true;
}

Expand All @@ -222,16 +225,17 @@ function isComponentInRenderProp(node, context) {
* <Component rows={ [{ render: () => <div /> }] } />
* ```
* @param {ASTNode} node The AST node
* @param {string} propNamePattern The pattern to match render props against
* @returns {boolean} True if component is declared inside a render property, false if not
*/
function isDirectValueOfRenderProperty(node) {
function isDirectValueOfRenderProperty(node, propNamePattern) {
return (
node
&& node.parent
&& node.parent.type === 'Property'
&& node.parent.key
&& node.parent.key.type === 'Identifier'
&& startsWithRender(node.parent.key.name)
&& propMatchesRenderPropPattern(node.parent.key.name, propNamePattern)
);
}

Expand Down Expand Up @@ -277,13 +281,17 @@ module.exports = {
allowAsProps: {
type: 'boolean',
},
propNamePattern: {
type: 'string',
},
},
additionalProperties: false,
}],
},

create: Components.detect((context, components, utils) => {
const allowAsProps = context.options.some((option) => option && option.allowAsProps);
const propNamePattern = (context.options[0] || {}).propNamePattern || 'render*';

/**
* Check whether given node is declared inside class component's render block
Expand Down Expand Up @@ -418,7 +426,7 @@ module.exports = {

if (
// Support allowAsProps option
(isDeclaredInsideProps && (allowAsProps || isComponentInRenderProp(node, context)))
(isDeclaredInsideProps && (allowAsProps || isComponentInRenderProp(node, context, propNamePattern)))

// Prevent reporting components created inside Array.map calls
|| isMapCall(node)
Expand All @@ -428,7 +436,7 @@ module.exports = {
|| isReturnStatementOfHook(node, context)

// Do not mark objects containing render methods
|| isDirectValueOfRenderProperty(node)
|| isDirectValueOfRenderProperty(node, propNamePattern)

// Prevent reporting nested class components twice
|| isInsideRenderMethod(node)
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/no-unstable-nested-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,18 @@ ruleTester.run('no-unstable-nested-components', rule, {
allowAsProps: true,
}],
},
{
code: `
function ParentComponent() {
return <Table
rowRenderer={(rowData) => <Row data={data} />}
/>
}
`,
options: [{
propNamePattern: '*Renderer',
}],
},
/* TODO These minor cases are currently falsely marked due to component detection
{
code: `
Expand Down