Skip to content

Commit 9fb0d42

Browse files
HenryBrown0ljharb
authored andcommitted
[Refactor] propTypes: extract type params to var
1 parent 41d9b61 commit 9fb0d42

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1414
* [`jsx-key`]: detect conditional returns ([#3630][] @yialo)
1515
* [`jsx-newline`]: prevent a crash when `allowMultilines ([#3633][] @ljharb)
1616

17+
### Changed
18+
* [Refactor] `propTypes`: extract type params to var ([#3634][] @HenryBrown0)
19+
1720
[#3638]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3638
21+
[#3634]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3634
1822
[#3633]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3633
1923
[#3630]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3630
2024
[#3623]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3623

lib/util/propTypes.js

+14-10
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 nodeTypeParams = node.typeParameters;
631+
if (shouldTraverseTypeParams && nodeTypeParams && nodeTypeParams.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 = nodeTypeParams.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 nodeTypeParams = node.typeParameters;
732+
if (nodeTypeParams) {
733+
if (nodeTypeParams.params.length === 1) {
734+
let returnType = nodeTypeParams.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 propNodeTypeParams = propNode.argument.typeParameters;
767+
if (propNodeTypeParams) {
768+
this.visitTSNode(propNodeTypeParams);
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;
@@ -960,8 +963,9 @@ module.exports = function propTypesInstructions(context, components, utils) {
960963
break;
961964
case 'GenericTypeAnnotation':
962965
if (propTypes.id.name === '$ReadOnly') {
966+
const propTypeParams = propTypes.typeParameters;
963967
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(
964-
propTypes.typeParameters.params[0],
968+
propTypeParams.params[0],
965969
declaredPropTypes
966970
);
967971
} else {
@@ -1011,9 +1015,9 @@ module.exports = function propTypesInstructions(context, components, utils) {
10111015
)
10121016
)
10131017
) {
1014-
const propTypes = node.parent.typeParameters.params[1];
1018+
const propTypesParams = node.parent.typeParameters;
10151019
const declaredPropTypes = {};
1016-
const obj = new DeclarePropTypesForTSTypeAnnotation(propTypes, declaredPropTypes);
1020+
const obj = new DeclarePropTypesForTSTypeAnnotation(propTypesParams.params[1], declaredPropTypes);
10171021
components.set(node, {
10181022
declaredPropTypes: obj.declaredPropTypes,
10191023
ignorePropsValidation: obj.shouldIgnorePropTypes,

0 commit comments

Comments
 (0)