Skip to content

Commit

Permalink
[Fix] prop-types, propTypes: bail out unknown generic types insid…
Browse files Browse the repository at this point in the history
…e func params
  • Loading branch information
vedadeepta authored and ljharb committed Sep 18, 2021
1 parent bd270fc commit 227e967
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## Unreleased

### Fixed
* [`prop-types`], `propTypes`: bail out unknown generic types inside func params ([#3076] @vedadeepta)

[#3076]: https://github.com/yannickcr/eslint-plugin-react/pull/3076

## [7.25.2] - 2021.09.16

### Fixed
Expand Down
10 changes: 0 additions & 10 deletions lib/util/propTypes.js
Expand Up @@ -970,16 +970,6 @@ module.exports = function propTypesInstructions(context, components, utils) {
}
});
} else {
// check if its a valid generic type when `X<{...}>`
if (
param.typeAnnotation
&& param.typeAnnotation.typeAnnotation
&& param.typeAnnotation.typeAnnotation.type === 'TSTypeReference'
&& param.typeAnnotation.typeAnnotation.typeParameters != null
&& !isValidReactGenericTypeAnnotation(param.typeAnnotation.typeAnnotation)
) {
return;
}
markPropTypesAsDeclared(node, resolveTypeAnnotation(param));
}
} else {
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -3303,6 +3303,19 @@ ruleTester.run('prop-types', rule, {
`,
parser: parsers['@TYPESCRIPT_ESLINT']
},
{
code: `
type Props<ValueType> = {
value: ValueType;
onClick: (value: ValueType) => void;
};
const Button = <T,>({ onClick, value }: Props<T>) => {
return <button onClick={() => onClick(value)}>BUTTON</button>;
};
`,
parser: parsers['@TYPESCRIPT_ESLINT']
}
]),
{
Expand Down Expand Up @@ -6865,6 +6878,23 @@ ruleTester.run('prop-types', rule, {
}
],
parser: parsers['@TYPESCRIPT_ESLINT']
},
{
code: `
type PersonProps<T> = {
x: T;
}
const Person = (props: PersonProps<string>): React.ReactElement => (
<div>{props.username}</div>
);
`,
errors: [
{
messageId: 'missingPropType',
data: {name: 'username'}
}
],
parser: parsers['@TYPESCRIPT_ESLINT']
}
])
)
Expand Down

0 comments on commit 227e967

Please sign in to comment.