Skip to content

[Fix] prop-types: function in class that returns a component causes false warning in typescript #2843

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

Merged
merged 1 commit into from
Oct 25, 2020
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 @@ -12,7 +12,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

### Fixed
* [`display-name`]/component detection: avoid a crash on anonymous components ([#2840][] @ljharb)
* [`prop-types`]: function in class that returns a component causes false warning in typescript ([#2843][] @SyMind)

[#2843]: https://github.com/yannickcr/eslint-plugin-react/pull/2843
[#2840]: https://github.com/yannickcr/eslint-plugin-react/issues/2840
[#2835]: https://github.com/yannickcr/eslint-plugin-react/pull/2835
[#2763]: https://github.com/yannickcr/eslint-plugin-react/pull/2763
Expand Down
5 changes: 4 additions & 1 deletion lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,10 @@ function componentRule(rule, context) {
return undefined;
}
if (utils.isInAllowedPositionForComponent(node) && utils.isReturningJSXOrNull(node)) {
return node;
if (!node.id || isFirstLetterCapitalized(node.id.name)) {
return node;
}
return undefined;
}

// Case like `React.memo(() => <></>)` or `React.forwardRef(...)`
Expand Down
3 changes: 2 additions & 1 deletion lib/util/propTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,8 @@ module.exports = function propTypesInstructions(context, components, utils) {
return;
}

if (isInsideClassBody(node)) {
// https://github.com/yannickcr/eslint-plugin-react/issues/2784
if (isInsideClassBody(node) && !astUtil.isFunction(node)) {
return;
}

Expand Down
31 changes: 31 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3143,6 +3143,37 @@ ruleTester.run('prop-types', rule, {
}
export default InputField`,
parser: parsers['@TYPESCRIPT_ESLINT']
},
{
code: `
import React from 'react'

class Factory {
getRenderFunction() {
return function renderFunction({ name }) {
return <div>Hello {name}</div>
}
}
}
`
},
{
code: `
import React from 'react'

type ComponentProps = {
name: string
}

class Factory {
getComponent() {
return function Component({ name }: ComponentProps) {
return <div>Hello {name}</div>
}
}
}
`,
parser: parsers['@TYPESCRIPT_ESLINT']
}
])
),
Expand Down