@@ -41,7 +41,8 @@ function isFunctionType(node) {
41
41
*/
42
42
function isSuperTypeParameterPropsDeclaration ( node ) {
43
43
if ( node && ( node . type === 'ClassDeclaration' || node . type === 'ClassExpression' ) ) {
44
- if ( node . superTypeParameters && node . superTypeParameters . params . length > 0 ) {
44
+ const superTypeArguments = 'superTypeArguments' in node ? node . superTypeArguments : node . superTypeParameters ;
45
+ if ( superTypeArguments && superTypeArguments . params . length > 0 ) {
45
46
return true ;
46
47
}
47
48
}
@@ -581,6 +582,19 @@ module.exports = function propTypesInstructions(context, components, utils) {
581
582
) ;
582
583
}
583
584
585
+ /**
586
+ * Avoid deprecation errors around referencing typeParameters, while also
587
+ * maintain backwards-compatibility after typescript-eslint renamed
588
+ * `typeParameters` to `typeArguments`
589
+ * https://typescript-eslint.io/troubleshooting/faqs/general/#the-key-property-is-deprecated-on-type-nodes-use-key-instead-warnings
590
+ * @param {ASTNode } node The AST node being checked.
591
+ * @returns {string | undefined }
592
+ */
593
+ function getNodeTypeArguments ( node ) {
594
+ if ( ! node ) return ;
595
+ return 'typeArguments' in node ? node . typeArguments : node . typeParameters ;
596
+ }
597
+
584
598
class DeclarePropTypesForTSTypeAnnotation {
585
599
constructor ( propTypes , declaredPropTypes , rootNode ) {
586
600
this . propTypes = propTypes ;
@@ -637,8 +651,8 @@ module.exports = function propTypesInstructions(context, components, utils) {
637
651
typeName = node . typeName . name ;
638
652
const leftMostName = getLeftMostTypeName ( node . typeName ) ;
639
653
const shouldTraverseTypeParams = genericReactTypesImport . has ( leftMostName ) ;
640
- const nodeTypeParams = node . typeParameters ;
641
- if ( shouldTraverseTypeParams && nodeTypeParams && nodeTypeParams . length !== 0 ) {
654
+ const nodeTypeArgs = getNodeTypeArguments ( node ) ;
655
+ if ( shouldTraverseTypeParams && nodeTypeArgs && nodeTypeArgs . length !== 0 ) {
642
656
// All react Generic types are derived from:
643
657
// type PropsWithChildren<P> = P & { children?: ReactNode | undefined }
644
658
// So we should construct an optional children prop
@@ -660,7 +674,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
660
674
const idx = genericTypeParamIndexWherePropsArePresent [
661
675
leftMostName !== rightMostName ? rightMostName : importedName
662
676
] ;
663
- const nextNode = nodeTypeParams . params [ idx ] ;
677
+ const nextNode = nodeTypeArgs . params [ idx ] ;
664
678
this . visitTSNode ( nextNode ) ;
665
679
return ;
666
680
}
@@ -749,10 +763,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
749
763
750
764
convertReturnTypeToPropTypes ( node , rootNode ) {
751
765
// ReturnType<T> should always have one parameter
752
- const nodeTypeParams = node . typeParameters ;
753
- if ( nodeTypeParams ) {
754
- if ( nodeTypeParams . params . length === 1 ) {
755
- let returnType = nodeTypeParams . params [ 0 ] ;
766
+ const nodeTypeArgs = getNodeTypeArguments ( node ) ;
767
+ if ( nodeTypeArgs ) {
768
+ if ( nodeTypeArgs . params . length === 1 ) {
769
+ let returnType = nodeTypeArgs . params [ 0 ] ;
756
770
// This line is trying to handle typescript-eslint-parser
757
771
// typescript-eslint-parser TSTypeQuery is wrapped by TSTypeReference
758
772
if ( astUtil . isTSTypeReference ( returnType ) ) {
@@ -784,11 +798,11 @@ module.exports = function propTypesInstructions(context, components, utils) {
784
798
case 'ObjectExpression' :
785
799
iterateProperties ( context , res . properties , ( key , value , propNode ) => {
786
800
if ( propNode && propNode . argument && propNode . argument . type === 'CallExpression' ) {
787
- const propNodeTypeParams = propNode . argument . typeParameters ;
788
- if ( propNodeTypeParams ) {
789
- this . visitTSNode ( propNodeTypeParams ) ;
801
+ const propNodeTypeArgs = getNodeTypeArguments ( propNode . argument ) ;
802
+ if ( propNodeTypeArgs ) {
803
+ this . visitTSNode ( propNodeTypeArgs ) ;
790
804
} else {
791
- // Ignore this CallExpression return value since it doesn't have any typeParameters to let us know it's types.
805
+ // Ignore this CallExpression return value since it doesn't have any typeArguments to let us know it's types.
792
806
this . shouldIgnorePropTypes = true ;
793
807
return ;
794
808
}
@@ -806,10 +820,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
806
820
} ) ;
807
821
break ;
808
822
case 'CallExpression' :
809
- if ( res . typeParameters ) {
810
- this . visitTSNode ( res . typeParameters ) ;
823
+ if ( getNodeTypeArguments ( res ) ) {
824
+ this . visitTSNode ( getNodeTypeArguments ( res ) ) ;
811
825
} else {
812
- // Ignore this CallExpression return value since it doesn't have any typeParameters to let us know it's types.
826
+ // Ignore this CallExpression return value since it doesn't have any typeArguments to let us know it's types.
813
827
this . shouldIgnorePropTypes = true ;
814
828
}
815
829
break ;
@@ -992,9 +1006,9 @@ module.exports = function propTypesInstructions(context, components, utils) {
992
1006
break ;
993
1007
case 'GenericTypeAnnotation' :
994
1008
if ( propTypes . id . name === '$ReadOnly' ) {
995
- const propTypeParams = propTypes . typeParameters ;
1009
+ const propTypeArgs = getNodeTypeArguments ( propTypes ) ;
996
1010
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation (
997
- propTypeParams . params [ 0 ] ,
1011
+ propTypeArgs . params [ 0 ] ,
998
1012
declaredPropTypes
999
1013
) ;
1000
1014
} else {
@@ -1034,8 +1048,8 @@ module.exports = function propTypesInstructions(context, components, utils) {
1034
1048
if (
1035
1049
node . parent
1036
1050
&& node . parent . callee
1037
- && node . parent . typeParameters
1038
- && node . parent . typeParameters . params
1051
+ && getNodeTypeArguments ( node . parent )
1052
+ && getNodeTypeArguments ( node . parent ) . params
1039
1053
&& (
1040
1054
node . parent . callee . name === 'forwardRef' || (
1041
1055
node . parent . callee . object
@@ -1045,9 +1059,9 @@ module.exports = function propTypesInstructions(context, components, utils) {
1045
1059
)
1046
1060
)
1047
1061
) {
1048
- const propTypesParams = node . parent . typeParameters ;
1062
+ const propTypesArguments = getNodeTypeArguments ( node . parent ) ;
1049
1063
const declaredPropTypes = { } ;
1050
- const obj = new DeclarePropTypesForTSTypeAnnotation ( propTypesParams . params [ 1 ] , declaredPropTypes , rootNode ) ;
1064
+ const obj = new DeclarePropTypesForTSTypeAnnotation ( propTypesArguments . params [ 1 ] , declaredPropTypes , rootNode ) ;
1051
1065
components . set ( node , {
1052
1066
declaredPropTypes : obj . declaredPropTypes ,
1053
1067
ignorePropsValidation : obj . shouldIgnorePropTypes ,
@@ -1093,7 +1107,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
1093
1107
if (
1094
1108
annotation
1095
1109
&& annotation . type !== 'TSTypeReference'
1096
- && annotation . typeParameters == null
1110
+ && getNodeTypeArguments ( annotation ) == null
1097
1111
) {
1098
1112
return ;
1099
1113
}
0 commit comments