Skip to content

Commit 2dbe952

Browse files
committed
fix: use type args prop type util
1 parent 0377afb commit 2dbe952

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

lib/util/propTypes.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,8 @@ module.exports = function propTypesInstructions(context, components, utils) {
627627
typeName = node.typeName.name;
628628
const leftMostName = getLeftMostTypeName(node.typeName);
629629
const shouldTraverseTypeParams = genericReactTypesImport.has(leftMostName);
630-
if (shouldTraverseTypeParams && node.typeParameters && node.typeParameters.length !== 0) {
630+
const nodeType = node.typeArguments || node.typeParameters;
631+
if (shouldTraverseTypeParams && nodeType && nodeType.length !== 0) {
631632
// All react Generic types are derived from:
632633
// type PropsWithChildren<P> = P & { children?: ReactNode | undefined }
633634
// So we should construct an optional children prop
@@ -638,7 +639,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
638639
const idx = genericTypeParamIndexWherePropsArePresent[
639640
leftMostName !== rightMostName ? rightMostName : importedName
640641
];
641-
const nextNode = node.typeParameters.params[idx];
642+
const nextNode = nodeType.params[idx];
642643
this.visitTSNode(nextNode);
643644
return;
644645
}
@@ -727,9 +728,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
727728

728729
convertReturnTypeToPropTypes(node) {
729730
// ReturnType<T> should always have one parameter
730-
if (node.typeParameters) {
731-
if (node.typeParameters.params.length === 1) {
732-
let returnType = node.typeParameters.params[0];
731+
const nodeType = node.typeArguments || node.typeParameters;
732+
if (nodeType) {
733+
if (nodeType.params.length === 1) {
734+
let returnType = nodeType.params[0];
733735
// This line is trying to handle typescript-eslint-parser
734736
// typescript-eslint-parser TSTypeQuery is wrapped by TSTypeReference
735737
if (astUtil.isTSTypeReference(returnType)) {
@@ -761,8 +763,9 @@ module.exports = function propTypesInstructions(context, components, utils) {
761763
case 'ObjectExpression':
762764
iterateProperties(context, res.properties, (key, value, propNode) => {
763765
if (propNode && propNode.argument && propNode.argument.type === 'CallExpression') {
764-
if (propNode.argument.typeParameters) {
765-
this.visitTSNode(propNode.argument.typeParameters);
766+
const propNodeType = propNode.argument.typeArguments || propNode.argument.typeParameters;
767+
if (propNodeType) {
768+
this.visitTSNode(propNodeType);
766769
} else {
767770
// Ignore this CallExpression return value since it doesn't have any typeParameters to let us know it's types.
768771
this.shouldIgnorePropTypes = true;
@@ -782,8 +785,8 @@ module.exports = function propTypesInstructions(context, components, utils) {
782785
});
783786
break;
784787
case 'CallExpression':
785-
if (res.typeParameters) {
786-
this.visitTSNode(res.typeParameters);
788+
if (res.typeArguments || res.typeParameters) {
789+
this.visitTSNode(res.typeArguments || res.typeParameters);
787790
} else {
788791
// Ignore this CallExpression return value since it doesn't have any typeParameters to let us know it's types.
789792
this.shouldIgnorePropTypes = true;
@@ -960,8 +963,9 @@ module.exports = function propTypesInstructions(context, components, utils) {
960963
break;
961964
case 'GenericTypeAnnotation':
962965
if (propTypes.id.name === '$ReadOnly') {
966+
const propType = propTypes.typeArguments || propTypes.typeParameters;
963967
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(
964-
propTypes.typeParameters.params[0],
968+
propType.params[0],
965969
declaredPropTypes
966970
);
967971
} else {
@@ -1000,8 +1004,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
10001004
if (
10011005
node.parent
10021006
&& node.parent.callee
1003-
&& node.parent.typeParameters
1004-
&& node.parent.typeParameters.params
1007+
&& (
1008+
(node.parent.typeArguments && node.parent.typeArguments.params)
1009+
|| (node.parent.typeParameters && node.parent.typeParameters.params)
1010+
)
10051011
&& (
10061012
node.parent.callee.name === 'forwardRef' || (
10071013
node.parent.callee.object
@@ -1011,9 +1017,9 @@ module.exports = function propTypesInstructions(context, components, utils) {
10111017
)
10121018
)
10131019
) {
1014-
const propTypes = node.parent.typeParameters.params[1];
1020+
const propTypes = node.parent.typeArguments || node.parent.typeParameters;
10151021
const declaredPropTypes = {};
1016-
const obj = new DeclarePropTypesForTSTypeAnnotation(propTypes, declaredPropTypes);
1022+
const obj = new DeclarePropTypesForTSTypeAnnotation(propTypes.params[1], declaredPropTypes);
10171023
components.set(node, {
10181024
declaredPropTypes: obj.declaredPropTypes,
10191025
ignorePropsValidation: obj.shouldIgnorePropTypes,
@@ -1059,7 +1065,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
10591065
if (
10601066
annotation
10611067
&& annotation.type !== 'TSTypeReference'
1062-
&& annotation.typeParameters == null
1068+
&& (annotation.typeArguments == null || annotation.typeParameters == null)
10631069
) {
10641070
return;
10651071
}

0 commit comments

Comments
 (0)