Skip to content

Commit 1535846

Browse files
committed
[Refactor] extract super type arguments with a utility
1 parent 1c2d17e commit 1535846

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/util/propTypes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function isFunctionType(node) {
4141
*/
4242
function isSuperTypeParameterPropsDeclaration(node) {
4343
if (node && (node.type === 'ClassDeclaration' || node.type === 'ClassExpression')) {
44-
const parameters = node.superTypeParameters || node.superTypeArguments;
44+
const parameters = propsUtil.getSuperTypeArguments(node);
4545
if (parameters && parameters.params.length > 0) {
4646
return true;
4747
}
@@ -1127,7 +1127,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
11271127
*/
11281128
function resolveSuperParameterPropsType(node) {
11291129
let propsParameterPosition;
1130-
const parameters = node.superTypeParameters || node.superTypeArguments;
1130+
const parameters = propsUtil.getSuperTypeArguments(node);
11311131

11321132
try {
11331133
// Flow <=0.52 had 3 required TypedParameters of which the second one is the Props.

lib/util/props.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ function getTypeArguments(node) {
105105
return node.typeParameters;
106106
}
107107

108+
/**
109+
* Returns the super type arguments of a node or super type parameters if type arguments are not available.
110+
* @param {ASTNode} node The node to get the super type arguments from.
111+
* @returns {ASTNode} The super type arguments or parameters of the node.
112+
*/
113+
function getSuperTypeArguments(node) {
114+
if ('superTypeArguments' in node) {
115+
return node.superTypeArguments;
116+
}
117+
return node.superTypeParameters;
118+
}
119+
108120
module.exports = {
109121
isPropTypesDeclaration,
110122
isContextTypesDeclaration,
@@ -114,4 +126,5 @@ module.exports = {
114126
isDisplayNameDeclaration,
115127
isRequiredPropType,
116128
getTypeArguments,
129+
getSuperTypeArguments,
117130
};

0 commit comments

Comments
 (0)