Content-Length: 12077 | pFad | http://github.com/jsx-eslint/eslint-plugin-react/pull/3634.patch
thub.com
From 9fb0d42af9f735df28b911ea6ee2d4e9e07f28bd Mon Sep 17 00:00:00 2001
From: HenryBrown0 <26250092+HenryBrown0@users.noreply.github.com>
Date: Sun, 24 Sep 2023 13:30:04 +0100
Subject: [PATCH 1/4] [Refactor] `propTypes`: extract type params to var
---
CHANGELOG.md | 4 ++++
lib/util/propTypes.js | 24 ++++++++++++++----------
2 files changed, 18 insertions(+), 10 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1b69232437..210cc8666b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,7 +14,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [`jsx-key`]: detect conditional returns ([#3630][] @yialo)
* [`jsx-newline`]: prevent a crash when `allowMultilines ([#3633][] @ljharb)
+### Changed
+* [Refactor] `propTypes`: extract type params to var ([#3634][] @HenryBrown0)
+
[#3638]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3638
+[#3634]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3634
[#3633]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3633
[#3630]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3630
[#3623]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3623
diff --git a/lib/util/propTypes.js b/lib/util/propTypes.js
index c5aa35eb61..a189f871ca 100644
--- a/lib/util/propTypes.js
+++ b/lib/util/propTypes.js
@@ -627,7 +627,8 @@ module.exports = function propTypesInstructions(context, components, utils) {
typeName = node.typeName.name;
const leftMostName = getLeftMostTypeName(node.typeName);
const shouldTraverseTypeParams = genericReactTypesImport.has(leftMostName);
- if (shouldTraverseTypeParams && node.typeParameters && node.typeParameters.length !== 0) {
+ const nodeTypeParams = node.typeParameters;
+ if (shouldTraverseTypeParams && nodeTypeParams && nodeTypeParams.length !== 0) {
// All react Generic types are derived from:
// type PropsWithChildren = P & { children?: ReactNode | undefined }
// So we should construct an optional children prop
@@ -638,7 +639,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
const idx = genericTypeParamIndexWherePropsArePresent[
leftMostName !== rightMostName ? rightMostName : importedName
];
- const nextNode = node.typeParameters.params[idx];
+ const nextNode = nodeTypeParams.params[idx];
this.visitTSNode(nextNode);
return;
}
@@ -727,9 +728,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
convertReturnTypeToPropTypes(node) {
// ReturnType should always have one parameter
- if (node.typeParameters) {
- if (node.typeParameters.params.length === 1) {
- let returnType = node.typeParameters.params[0];
+ const nodeTypeParams = node.typeParameters;
+ if (nodeTypeParams) {
+ if (nodeTypeParams.params.length === 1) {
+ let returnType = nodeTypeParams.params[0];
// This line is trying to handle typescript-eslint-parser
// typescript-eslint-parser TSTypeQuery is wrapped by TSTypeReference
if (astUtil.isTSTypeReference(returnType)) {
@@ -761,8 +763,9 @@ module.exports = function propTypesInstructions(context, components, utils) {
case 'ObjectExpression':
iterateProperties(context, res.properties, (key, value, propNode) => {
if (propNode && propNode.argument && propNode.argument.type === 'CallExpression') {
- if (propNode.argument.typeParameters) {
- this.visitTSNode(propNode.argument.typeParameters);
+ const propNodeTypeParams = propNode.argument.typeParameters;
+ if (propNodeTypeParams) {
+ this.visitTSNode(propNodeTypeParams);
} else {
// Ignore this CallExpression return value since it doesn't have any typeParameters to let us know it's types.
this.shouldIgnorePropTypes = true;
@@ -960,8 +963,9 @@ module.exports = function propTypesInstructions(context, components, utils) {
break;
case 'GenericTypeAnnotation':
if (propTypes.id.name === '$ReadOnly') {
+ const propTypeParams = propTypes.typeParameters;
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(
- propTypes.typeParameters.params[0],
+ propTypeParams.params[0],
declaredPropTypes
);
} else {
@@ -1011,9 +1015,9 @@ module.exports = function propTypesInstructions(context, components, utils) {
)
)
) {
- const propTypes = node.parent.typeParameters.params[1];
+ const propTypesParams = node.parent.typeParameters;
const declaredPropTypes = {};
- const obj = new DeclarePropTypesForTSTypeAnnotation(propTypes, declaredPropTypes);
+ const obj = new DeclarePropTypesForTSTypeAnnotation(propTypesParams.params[1], declaredPropTypes);
components.set(node, {
declaredPropTypes: obj.declaredPropTypes,
ignorePropsValidation: obj.shouldIgnorePropTypes,
From 0714704b73fcc9dfe260dc1327ee82b9c6f97168 Mon Sep 17 00:00:00 2001
From: HenryBrown0 <26250092+HenryBrown0@users.noreply.github.com>
Date: Sun, 24 Sep 2023 13:36:54 +0100
Subject: [PATCH 2/4] [Refactor] `boolean-prop-naming`: invert if statement
Co-authored-by: HenryBrown0 <26250092+HenryBrown0@users.noreply.github.com>
Co-authored-by: Jordan Harband
---
CHANGELOG.md | 1 +
lib/rules/boolean-prop-naming.js | 27 ++++++++++++++++-----------
2 files changed, 17 insertions(+), 11 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 210cc8666b..16cb6cc0a8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
### Changed
* [Refactor] `propTypes`: extract type params to var ([#3634][] @HenryBrown0)
+* [Refactor] [`boolean-prop-naming`]: invert if statement ([#3634][] @HenryBrown0)
[#3638]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3638
[#3634]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3634
diff --git a/lib/rules/boolean-prop-naming.js b/lib/rules/boolean-prop-naming.js
index f1c7420a7f..b5dd37b529 100644
--- a/lib/rules/boolean-prop-naming.js
+++ b/lib/rules/boolean-prop-naming.js
@@ -240,19 +240,24 @@ module.exports = {
}
if (
- component.node.parent
- && component.node.parent.type === 'VariableDeclarator'
- && component.node.parent.id
- && component.node.parent.id.type === 'Identifier'
- && component.node.parent.id.typeAnnotation
- && component.node.parent.id.typeAnnotation.typeAnnotation
- && component.node.parent.id.typeAnnotation.typeAnnotation.typeParameters
- && (
- component.node.parent.id.typeAnnotation.typeAnnotation.typeParameters.type === 'TSTypeParameterInstantiation'
- || component.node.parent.id.typeAnnotation.typeAnnotation.typeParameters.type === 'TypeParameterInstantiation'
+ !component.node.parent
+ || component.node.parent.type !== 'VariableDeclarator'
+ || !component.node.parent.id
+ || component.node.parent.id.type !== 'Identifier'
+ || !component.node.parent.id.typeAnnotation
+ || !component.node.parent.id.typeAnnotation.typeAnnotation
+ ) {
+ return;
+ }
+
+ const annotationTypeParams = component.node.parent.id.typeAnnotation.typeAnnotation.typeParameters;
+ if (
+ annotationTypeParams && (
+ annotationTypeParams.type === 'TSTypeParameterInstantiation'
+ || annotationTypeParams.type === 'TypeParameterInstantiation'
)
) {
- return component.node.parent.id.typeAnnotation.typeAnnotation.typeParameters.params.find(
+ return annotationTypeParams.params.find(
(param) => param.type === 'TSTypeReference' || param.type === 'GenericTypeAnnotation'
);
}
From aef6e7d94377ebf80400cb4c9f4548057d0288ce Mon Sep 17 00:00:00 2001
From: HenryBrown0 <26250092+HenryBrown0@users.noreply.github.com>
Date: Sun, 24 Sep 2023 13:40:35 +0100
Subject: [PATCH 3/4] [Refactor] `function-component-definition`: exit early if
no type params
Co-authored-by: HenryBrown0 <26250092+HenryBrown0@users.noreply.github.com>
Co-authored-by: Jordan Harband
---
CHANGELOG.md | 1 +
lib/rules/function-component-definition.js | 12 +++++-------
2 files changed, 6 insertions(+), 7 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 16cb6cc0a8..9e048ec074 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
### Changed
* [Refactor] `propTypes`: extract type params to var ([#3634][] @HenryBrown0)
* [Refactor] [`boolean-prop-naming`]: invert if statement ([#3634][] @HenryBrown0)
+* [Refactor] [`function-component-definition`]: exit early if no type params ([#3634][] @HenryBrown0)
[#3638]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3638
[#3634]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3634
diff --git a/lib/rules/function-component-definition.js b/lib/rules/function-component-definition.js
index e0a27dedca..f8a95a9da0 100644
--- a/lib/rules/function-component-definition.js
+++ b/lib/rules/function-component-definition.js
@@ -33,14 +33,12 @@ const UNNAMED_FUNCTION_TEMPLATES = {
};
function hasOneUnconstrainedTypeParam(node) {
- if (node.typeParameters) {
- return (
- node.typeParameters.params.length === 1
- && !node.typeParameters.params[0].constraint
- );
- }
+ const nodeTypeParams = node.typeParameters;
- return false;
+ return nodeTypeParams
+ && nodeTypeParams.params
+ && nodeTypeParams.params.length === 1
+ && !nodeTypeParams.params[0].constraint;
}
function hasName(node) {
From ca30f77196d410c7cdb1e4fe11f11bfffb46c84f Mon Sep 17 00:00:00 2001
From: HenryBrown0 <26250092+HenryBrown0@users.noreply.github.com>
Date: Sun, 24 Sep 2023 13:42:03 +0100
Subject: [PATCH 4/4] [Refactor] `jsx-props-no-multi-spaces`: extract type
parameters to var
---
CHANGELOG.md | 1 +
lib/rules/jsx-props-no-multi-spaces.js | 8 ++++++--
2 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9e048ec074..5ff0e08075 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [Refactor] `propTypes`: extract type params to var ([#3634][] @HenryBrown0)
* [Refactor] [`boolean-prop-naming`]: invert if statement ([#3634][] @HenryBrown0)
* [Refactor] [`function-component-definition`]: exit early if no type params ([#3634][] @HenryBrown0)
+* [Refactor] [`jsx-props-no-multi-spaces`]: extract type parameters to var ([#3634][] @HenryBrown0)
[#3638]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3638
[#3634]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3634
diff --git a/lib/rules/jsx-props-no-multi-spaces.js b/lib/rules/jsx-props-no-multi-spaces.js
index b0d34d59dd..b9cabafe32 100644
--- a/lib/rules/jsx-props-no-multi-spaces.js
+++ b/lib/rules/jsx-props-no-multi-spaces.js
@@ -98,8 +98,12 @@ module.exports = {
}
function containsGenericType(node) {
- const containsTypeParams = typeof node.typeParameters !== 'undefined';
- return containsTypeParams && node.typeParameters.type === 'TSTypeParameterInstantiation';
+ const nodeTypeParams = node.typeParameters;
+ if (typeof nodeTypeParams === 'undefined') {
+ return false;
+ }
+
+ return nodeTypeParams.type === 'TSTypeParameterInstantiation';
}
function getGenericNode(node) {
--- a PPN by Garber Painting Akron. With Image Size Reduction included!
Fetched URL: http://github.com/jsx-eslint/eslint-plugin-react/pull/3634.patch
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy