Skip to content

Commit 5f6350a

Browse files
SyMindljharb
authored andcommitted
[Fix] prop-types: function in class that returns a component causes false warning in typescript
Fixes #2784.
1 parent 57d0df7 commit 5f6350a

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1212

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

17+
[#2843]: https://github.com/yannickcr/eslint-plugin-react/pull/2843
1618
[#2840]: https://github.com/yannickcr/eslint-plugin-react/issues/2840
1719
[#2835]: https://github.com/yannickcr/eslint-plugin-react/pull/2835
1820
[#2763]: https://github.com/yannickcr/eslint-plugin-react/pull/2763

lib/util/Components.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,10 @@ function componentRule(rule, context) {
707707
return undefined;
708708
}
709709
if (utils.isInAllowedPositionForComponent(node) && utils.isReturningJSXOrNull(node)) {
710-
return node;
710+
if (!node.id || isFirstLetterCapitalized(node.id.name)) {
711+
return node;
712+
}
713+
return undefined;
711714
}
712715

713716
// Case like `React.memo(() => <></>)` or `React.forwardRef(...)`

lib/util/propTypes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,8 @@ module.exports = function propTypesInstructions(context, components, utils) {
891891
return;
892892
}
893893

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

tests/lib/rules/prop-types.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3143,6 +3143,37 @@ ruleTester.run('prop-types', rule, {
31433143
}
31443144
export default InputField`,
31453145
parser: parsers['@TYPESCRIPT_ESLINT']
3146+
},
3147+
{
3148+
code: `
3149+
import React from 'react'
3150+
3151+
class Factory {
3152+
getRenderFunction() {
3153+
return function renderFunction({ name }) {
3154+
return <div>Hello {name}</div>
3155+
}
3156+
}
3157+
}
3158+
`
3159+
},
3160+
{
3161+
code: `
3162+
import React from 'react'
3163+
3164+
type ComponentProps = {
3165+
name: string
3166+
}
3167+
3168+
class Factory {
3169+
getComponent() {
3170+
return function Component({ name }: ComponentProps) {
3171+
return <div>Hello {name}</div>
3172+
}
3173+
}
3174+
}
3175+
`,
3176+
parser: parsers['@TYPESCRIPT_ESLINT']
31463177
}
31473178
])
31483179
),

0 commit comments

Comments
 (0)