Skip to content

Commit 291d1bc

Browse files
committed
Avoid typescript-eslint deprecation warnings about typeParameters renamed to typeArguments
1 parent c6fdccd commit 291d1bc

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

lib/util/propTypes.js

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ function isFunctionType(node) {
4141
*/
4242
function isSuperTypeParameterPropsDeclaration(node) {
4343
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) {
4546
return true;
4647
}
4748
}
@@ -581,6 +582,19 @@ module.exports = function propTypesInstructions(context, components, utils) {
581582
);
582583
}
583584

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+
584598
class DeclarePropTypesForTSTypeAnnotation {
585599
constructor(propTypes, declaredPropTypes, rootNode) {
586600
this.propTypes = propTypes;
@@ -637,8 +651,8 @@ module.exports = function propTypesInstructions(context, components, utils) {
637651
typeName = node.typeName.name;
638652
const leftMostName = getLeftMostTypeName(node.typeName);
639653
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) {
642656
// All react Generic types are derived from:
643657
// type PropsWithChildren<P> = P & { children?: ReactNode | undefined }
644658
// So we should construct an optional children prop
@@ -660,7 +674,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
660674
const idx = genericTypeParamIndexWherePropsArePresent[
661675
leftMostName !== rightMostName ? rightMostName : importedName
662676
];
663-
const nextNode = nodeTypeParams.params[idx];
677+
const nextNode = nodeTypeArgs.params[idx];
664678
this.visitTSNode(nextNode);
665679
return;
666680
}
@@ -749,10 +763,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
749763

750764
convertReturnTypeToPropTypes(node, rootNode) {
751765
// 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];
756770
// This line is trying to handle typescript-eslint-parser
757771
// typescript-eslint-parser TSTypeQuery is wrapped by TSTypeReference
758772
if (astUtil.isTSTypeReference(returnType)) {
@@ -784,11 +798,11 @@ module.exports = function propTypesInstructions(context, components, utils) {
784798
case 'ObjectExpression':
785799
iterateProperties(context, res.properties, (key, value, propNode) => {
786800
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);
790804
} 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.
792806
this.shouldIgnorePropTypes = true;
793807
return;
794808
}
@@ -806,10 +820,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
806820
});
807821
break;
808822
case 'CallExpression':
809-
if (res.typeParameters) {
810-
this.visitTSNode(res.typeParameters);
823+
if (getNodeTypeArguments(res)) {
824+
this.visitTSNode(getNodeTypeArguments(res));
811825
} 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.
813827
this.shouldIgnorePropTypes = true;
814828
}
815829
break;
@@ -992,9 +1006,9 @@ module.exports = function propTypesInstructions(context, components, utils) {
9921006
break;
9931007
case 'GenericTypeAnnotation':
9941008
if (propTypes.id.name === '$ReadOnly') {
995-
const propTypeParams = propTypes.typeParameters;
1009+
const propTypeArgs = getNodeTypeArguments(propTypes);
9961010
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(
997-
propTypeParams.params[0],
1011+
propTypeArgs.params[0],
9981012
declaredPropTypes
9991013
);
10001014
} else {
@@ -1034,8 +1048,8 @@ module.exports = function propTypesInstructions(context, components, utils) {
10341048
if (
10351049
node.parent
10361050
&& node.parent.callee
1037-
&& node.parent.typeParameters
1038-
&& node.parent.typeParameters.params
1051+
&& getNodeTypeArguments(node.parent)
1052+
&& getNodeTypeArguments(node.parent).params
10391053
&& (
10401054
node.parent.callee.name === 'forwardRef' || (
10411055
node.parent.callee.object
@@ -1045,9 +1059,9 @@ module.exports = function propTypesInstructions(context, components, utils) {
10451059
)
10461060
)
10471061
) {
1048-
const propTypesParams = node.parent.typeParameters;
1062+
const propTypesArguments = getNodeTypeArguments(node.parent);
10491063
const declaredPropTypes = {};
1050-
const obj = new DeclarePropTypesForTSTypeAnnotation(propTypesParams.params[1], declaredPropTypes, rootNode);
1064+
const obj = new DeclarePropTypesForTSTypeAnnotation(propTypesArguments.params[1], declaredPropTypes, rootNode);
10511065
components.set(node, {
10521066
declaredPropTypes: obj.declaredPropTypes,
10531067
ignorePropsValidation: obj.shouldIgnorePropTypes,
@@ -1093,7 +1107,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
10931107
if (
10941108
annotation
10951109
&& annotation.type !== 'TSTypeReference'
1096-
&& annotation.typeParameters == null
1110+
&& getNodeTypeArguments(annotation) == null
10971111
) {
10981112
return;
10991113
}

0 commit comments

Comments
 (0)