Skip to content

Commit

Permalink
propTypes: add VFC to react generic list
Browse files Browse the repository at this point in the history
Fixes #3230
  • Loading branch information
ljharb committed Mar 1, 2022
1 parent d8602ac commit d9531c3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,7 +8,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
### Fixed
* [`no-unused-state`]: avoid a crash on type-only gDSFP declarations ([#3225][] @ljharb)
* [`jsx-curly-brace-presence`]: the string "never" defaults to `propElementValues` as `ignore` ([#3228][] @ljharb)
* [`propTypes`]: add `VFC` to react generic list ([#3230][] @ljharb)

[#3230]: https://github.com/yannickcr/eslint-plugin-react/issues/3230
[#3228]: https://github.com/yannickcr/eslint-plugin-react/issues/3228
[#3225]: https://github.com/yannickcr/eslint-plugin-react/issues/3225

Expand Down
2 changes: 1 addition & 1 deletion lib/util/propTypes.js
Expand Up @@ -100,7 +100,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
const defaults = { customValidators: [] };
const configuration = Object.assign({}, defaults, context.options[0] || {});
const customValidators = configuration.customValidators;
const allowedGenericTypes = new Set(['forwardRef', 'ForwardRefRenderFunction', 'VoidFunctionComponent', 'PropsWithChildren', 'SFC', 'StatelessComponent', 'FunctionComponent', 'FC']);
const allowedGenericTypes = new Set(['forwardRef', 'ForwardRefRenderFunction', 'VFC', 'VoidFunctionComponent', 'PropsWithChildren', 'SFC', 'StatelessComponent', 'FunctionComponent', 'FC']);
const genericTypeParamIndexWherePropsArePresent = {
ForwardRefRenderFunction: 1,
forwardRef: 1,
Expand Down
61 changes: 61 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -7746,6 +7746,27 @@ ruleTester.run('prop-types', rule, {
],
features: ['ts', 'no-babel'],
},
{
code: `
import React, { VFC } from 'react'
interface Props {
age: number
}
const Hello: VFC<Props> = function Hello(props) {
const { test } = props;
return <div>Hello {name}</div>;
}
`,
errors: [
{
messageId: 'missingPropType',
data: { name: 'test' },
},
],
features: ['ts', 'no-babel'],
},
{
code: `
import React from 'react'
Expand All @@ -7771,6 +7792,27 @@ ruleTester.run('prop-types', rule, {
code: `
import React from 'react'
interface Props {
age: number
}
const Hello: React.VFC<Props> = function Hello(props) {
const { test } = props;
return <div>Hello {name}</div>;
}
`,
errors: [
{
messageId: 'missingPropType',
data: { name: 'test' },
},
],
features: ['ts', 'no-babel'],
},
{
code: `
import React from 'react'
type IfooProps = { e: string };
const Foo: React.ForwardRefRenderFunction<HTMLDivElement, IfooProps> = function Foo (props, ref) {
const { name } = props;
Expand Down Expand Up @@ -7971,6 +8013,25 @@ ruleTester.run('prop-types', rule, {
],
features: ['ts', 'no-babel'],
},
{
code: `
import React from 'react';
export interface PersonProps {
username: string;
}
const Person: React.VFC<PersonProps> = (props): React.ReactElement => (
<div>{props.nonExistent}</div>
);
`,
errors: [
{
messageId: 'missingPropType',
data: { name: 'nonExistent' },
},
],
features: ['ts', 'no-babel'],
},
{
code: `
const Foo = ({ foo }) => {
Expand Down

0 comments on commit d9531c3

Please sign in to comment.