Skip to content

Commit d74a7d8

Browse files
vedadeeptaljharb
authored andcommitted
[Fix] prop-types, propTypes: handle React.* TypeScript types
1 parent 3d23496 commit d74a7d8

File tree

3 files changed

+135
-13
lines changed

3 files changed

+135
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
2121
* [`destructuring-assignment`], component detection: handle default exports edge case ([#3038][] @vedadeepta)
2222
* [`no-typos`]: fix crash on private methods ([#3043][] @ljharb)
2323
* [`jsx-no-bind`]: handle local function declarations ([#3048][] @p7g)
24+
* [`prop-types`], `propTypes`: handle React.* TypeScript types ([#3049][] @vedadeepta)
2425

2526
### Changed
2627
* [Docs] [`jsx-no-bind`]: updates discussion of refs ([#2998][] @dimitropoulos)
@@ -29,6 +30,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
2930
* [Docs] [`require-default-props`]: fix small typo ([#2994][] @evsasse)
3031
* [Tests] add weekly scheduled smoke tests ([#2963][] @AriPerkkio)
3132

33+
[#3049]: https://github.com/yannickcr/eslint-plugin-react/pull/3049
3234
[#3048]: https://github.com/yannickcr/eslint-plugin-react/pull/3048
3335
[#3043]: https://github.com/yannickcr/eslint-plugin-react/issues/3043
3436
[#3039]: https://github.com/yannickcr/eslint-plugin-react/pull/3039

lib/util/propTypes.js

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ function isFunctionType(node) {
2323
if (!node) return false;
2424
const nodeType = node.type;
2525
return nodeType === 'FunctionDeclaration'
26-
|| nodeType === 'FunctionExpression'
27-
|| nodeType === 'ArrowFunctionExpression';
26+
|| nodeType === 'FunctionExpression'
27+
|| nodeType === 'ArrowFunctionExpression';
2828
}
2929

3030
/**
@@ -100,6 +100,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
100100
const defaults = {customValidators: []};
101101
const configuration = Object.assign({}, defaults, context.options[0] || {});
102102
const customValidators = configuration.customValidators;
103+
const allowedGenericTypes = ['React.SFC', 'React.StatelessComponent', 'React.FunctionComponent', 'React.FC'];
103104

104105
/**
105106
* Returns the full scope.
@@ -547,6 +548,11 @@ module.exports = function propTypesInstructions(context, components, utils) {
547548
let typeName;
548549
if (astUtil.isTSTypeReference(node)) {
549550
typeName = node.typeName.name;
551+
if (!typeName && node.typeParameters && node.typeParameters.length !== 0) {
552+
const nextNode = node.typeParameters.params[0];
553+
this.visitTSNode(nextNode);
554+
return;
555+
}
550556
} else if (astUtil.isTSInterfaceHeritage(node)) {
551557
if (!node.expression && node.id) {
552558
typeName = node.id.name;
@@ -887,7 +893,15 @@ module.exports = function propTypesInstructions(context, components, utils) {
887893
* FunctionDeclaration, or FunctionExpression
888894
*/
889895
function markAnnotatedFunctionArgumentsAsDeclared(node) {
890-
if (!node.params || !node.params.length || !annotations.isAnnotatedFunctionPropsDeclaration(node, context)) {
896+
if (!node.params || !node.params.length) {
897+
return;
898+
}
899+
900+
const siblingIdentifier = node.parent && node.parent.id;
901+
const siblingHasTypeAnnotation = siblingIdentifier && siblingIdentifier.typeAnnotation;
902+
const isNodeAnnotated = annotations.isAnnotatedFunctionPropsDeclaration(node, context);
903+
904+
if (!isNodeAnnotated && !siblingHasTypeAnnotation) {
891905
return;
892906
}
893907

@@ -901,17 +915,38 @@ module.exports = function propTypesInstructions(context, components, utils) {
901915
return;
902916
}
903917

904-
const param = node.params[0];
905-
if (param.typeAnnotation && param.typeAnnotation.typeAnnotation && param.typeAnnotation.typeAnnotation.type === 'UnionTypeAnnotation') {
906-
param.typeAnnotation.typeAnnotation.types.forEach((annotation) => {
907-
if (annotation.type === 'GenericTypeAnnotation') {
908-
markPropTypesAsDeclared(node, resolveTypeAnnotation(annotation));
909-
} else {
910-
markPropTypesAsDeclared(node, annotation);
911-
}
912-
});
918+
if (isNodeAnnotated) {
919+
const param = node.params[0];
920+
if (param.typeAnnotation && param.typeAnnotation.typeAnnotation && param.typeAnnotation.typeAnnotation.type === 'UnionTypeAnnotation') {
921+
param.typeAnnotation.typeAnnotation.types.forEach((annotation) => {
922+
if (annotation.type === 'GenericTypeAnnotation') {
923+
markPropTypesAsDeclared(node, resolveTypeAnnotation(annotation));
924+
} else {
925+
markPropTypesAsDeclared(node, annotation);
926+
}
927+
});
928+
} else {
929+
markPropTypesAsDeclared(node, resolveTypeAnnotation(param));
930+
}
913931
} else {
914-
markPropTypesAsDeclared(node, resolveTypeAnnotation(param));
932+
// implements what's discussed here: https://github.com/yannickcr/eslint-plugin-react/issues/2777#issuecomment-683944481
933+
const annotation = siblingIdentifier.typeAnnotation.typeAnnotation;
934+
935+
if (
936+
annotation
937+
&& annotation.type !== 'TSTypeReference'
938+
&& annotation.typeParameters == null
939+
) {
940+
return;
941+
}
942+
943+
const typeName = context.getSourceCode().getText(annotation.typeName).replace(/ /g, '');
944+
945+
if (allowedGenericTypes.indexOf(typeName) === -1) {
946+
return;
947+
}
948+
949+
markPropTypesAsDeclared(node, resolveTypeAnnotation(siblingIdentifier));
915950
}
916951
}
917952

tests/lib/rules/prop-types.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,6 +3147,55 @@ ruleTester.run('prop-types', rule, {
31473147
}
31483148
`,
31493149
parser: parsers['@TYPESCRIPT_ESLINT']
3150+
},
3151+
{
3152+
code: `
3153+
interface PersonProps {
3154+
username: string;
3155+
}
3156+
const Person: React.FunctionComponent<PersonProps> = (props): React.ReactElement => (
3157+
<div>{props.username}</div>
3158+
);
3159+
`,
3160+
parser: parsers['@TYPESCRIPT_ESLINT']
3161+
},
3162+
{
3163+
code: `
3164+
const Person: React.FunctionComponent<PersonProps> = (props): React.ReactElement => (
3165+
<div>{props.username}</div>
3166+
);
3167+
`,
3168+
parser: parsers['@TYPESCRIPT_ESLINT']
3169+
},
3170+
{
3171+
code: `
3172+
interface PersonProps {
3173+
username: string;
3174+
}
3175+
const Person: React.FC<PersonProps> = (props): React.ReactElement => (
3176+
<div>{props.username}</div>
3177+
);
3178+
`,
3179+
parser: parsers['@TYPESCRIPT_ESLINT']
3180+
},
3181+
{
3182+
code: `
3183+
const Person: React.FunctionComponent<{ username: string }> = (props): React.ReactElement => (
3184+
<div>{props.username}</div>
3185+
);
3186+
`,
3187+
parser: parsers['@TYPESCRIPT_ESLINT']
3188+
},
3189+
{
3190+
code: `
3191+
type PersonProps = {
3192+
username: string;
3193+
}
3194+
const Person: React.FunctionComponent<PersonProps> = (props): React.ReactElement => (
3195+
<div>{props.username}</div>
3196+
);
3197+
`,
3198+
parser: parsers['@TYPESCRIPT_ESLINT']
31503199
}
31513200
]),
31523201
{
@@ -6621,6 +6670,42 @@ ruleTester.run('prop-types', rule, {
66216670
}
66226671
],
66236672
parser: parsers['@TYPESCRIPT_ESLINT']
6673+
},
6674+
{
6675+
code: `
6676+
import React from 'react';
6677+
interface PersonProps {
6678+
test: string;
6679+
}
6680+
const Person: React.FunctionComponent<PersonProps> = (props): React.ReactElement => (
6681+
<div>{props.username}</div>
6682+
);
6683+
`,
6684+
errors: [
6685+
{
6686+
messageId: 'missingPropType',
6687+
data: {name: 'username'}
6688+
}
6689+
],
6690+
parser: parsers['@TYPESCRIPT_ESLINT']
6691+
},
6692+
{
6693+
code: `
6694+
import React from 'react';
6695+
interface PersonProps {
6696+
username: string;
6697+
}
6698+
const Person: React.FunctionComponent<PersonProps> = (props): React.ReactElement => (
6699+
<div>{props.test}</div>
6700+
);
6701+
`,
6702+
errors: [
6703+
{
6704+
messageId: 'missingPropType',
6705+
data: {name: 'test'}
6706+
}
6707+
],
6708+
parser: parsers['@TYPESCRIPT_ESLINT']
66246709
}
66256710
])
66266711
)

0 commit comments

Comments
 (0)