Skip to content

Commit a6ff867

Browse files
HenryBrown0ljharb
authored andcommitted
[Fix] function-component-definition, boolean-prop-naming, jsx-first-prop-new-line, jsx-props-no-multi-spaces, propTypes: use type args
1 parent a4b0bbc commit a6ff867

File tree

6 files changed

+23
-16
lines changed

6 files changed

+23
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Change Log
2+
23
All notable changes to this project will be documented in this file.
34
This project adheres to [Semantic Versioning](https://semver.org/).
45
This change log adheres to standards from [Keep a CHANGELOG](https://keepachangelog.com).
@@ -19,6 +20,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1920
* [`no-invalid-html-attribute`]: substitute placeholders in suggestion messages ([#3759][] @mdjermanovic)
2021
* [`sort-prop-types`]: single line type ending without semicolon ([#3784][] @akulsr0)
2122
* [`require-default-props`]: report when required props have default value ([#3785][] @akulsr0)
23+
* [`function-component-definition`], [`boolean-prop-naming`], [`jsx-first-prop-new-line`], [`jsx-props-no-multi-spaces`], `propTypes`: use type args ([#3629][] @HenryBrown0)
2224

2325
### Changed
2426
* [Refactor] `variableUtil`: Avoid creating a single flat variable scope for each lookup ([#3782][] @DanielRosenwasser)
@@ -34,6 +36,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
3436
[#3748]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3748
3537
[#3724]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3724
3638
[#3694]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3694
39+
[#3629]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3629
3740

3841
## [7.34.4] - 2024.07.13
3942

lib/rules/boolean-prop-naming.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ module.exports = {
256256
return;
257257
}
258258

259-
const annotationTypeParams = component.node.parent.id.typeAnnotation.typeAnnotation.typeParameters;
259+
const typeAnnotation = component.node.parent.id.typeAnnotation.typeAnnotation;
260+
const annotationTypeParams = typeAnnotation.typeArguments || typeAnnotation.typeParameters;
260261
if (
261262
annotationTypeParams && (
262263
annotationTypeParams.type === 'TSTypeParameterInstantiation'

lib/rules/function-component-definition.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const UNNAMED_FUNCTION_TEMPLATES = {
3434
};
3535

3636
function hasOneUnconstrainedTypeParam(node) {
37-
const nodeTypeParams = node.typeParameters;
37+
const nodeTypeParams = node.typeArguments || node.typeParameters;
3838

3939
return nodeTypeParams
4040
&& nodeTypeParams.params
@@ -206,7 +206,7 @@ module.exports = {
206206
options.range,
207207
buildFunction(options.template, {
208208
typeAnnotation,
209-
typeParams: getNodeText(node.typeParameters, source),
209+
typeParams: getNodeText(node.typeArguments || node.typeParameters, source),
210210
params: getParams(node, source),
211211
returnType: getNodeText(node.returnType, source),
212212
body: getBody(node, source),

lib/rules/jsx-first-prop-new-line.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = {
5555
report(context, messages.propOnNewLine, 'propOnNewLine', {
5656
node: decl,
5757
fix(fixer) {
58-
return fixer.replaceTextRange([(node.typeParameters || node.name).range[1], decl.range[0]], '\n');
58+
return fixer.replaceTextRange([(node.typeArguments || node.typeParameters || node.name).range[1], decl.range[0]], '\n');
5959
},
6060
});
6161
}

lib/rules/jsx-props-no-multi-spaces.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module.exports = {
103103
}
104104

105105
function containsGenericType(node) {
106-
const nodeTypeParams = node.typeParameters;
106+
const nodeTypeParams = node.typeArguments || node.typeParameters;
107107
if (typeof nodeTypeParams === 'undefined') {
108108
return false;
109109
}
@@ -114,7 +114,7 @@ module.exports = {
114114
function getGenericNode(node) {
115115
const name = node.name;
116116
if (containsGenericType(node)) {
117-
const type = node.typeParameters;
117+
const type = node.typeArguments || node.typeParameters;
118118

119119
return Object.assign(
120120
{},

lib/util/propTypes.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
637637
typeName = node.typeName.name;
638638
const leftMostName = getLeftMostTypeName(node.typeName);
639639
const shouldTraverseTypeParams = genericReactTypesImport.has(leftMostName);
640-
const nodeTypeParams = node.typeParameters;
640+
const nodeTypeParams = node.typeArguments || node.typeParameters;
641641
if (shouldTraverseTypeParams && nodeTypeParams && nodeTypeParams.length !== 0) {
642642
// All react Generic types are derived from:
643643
// type PropsWithChildren<P> = P & { children?: ReactNode | undefined }
@@ -749,7 +749,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
749749

750750
convertReturnTypeToPropTypes(node, rootNode) {
751751
// ReturnType<T> should always have one parameter
752-
const nodeTypeParams = node.typeParameters;
752+
const nodeTypeParams = node.typeArguments || node.typeParameters;
753753
if (nodeTypeParams) {
754754
if (nodeTypeParams.params.length === 1) {
755755
let returnType = nodeTypeParams.params[0];
@@ -784,7 +784,8 @@ module.exports = function propTypesInstructions(context, components, utils) {
784784
case 'ObjectExpression':
785785
iterateProperties(context, res.properties, (key, value, propNode) => {
786786
if (propNode && propNode.argument && propNode.argument.type === 'CallExpression') {
787-
const propNodeTypeParams = propNode.argument.typeParameters;
787+
const propNodeTypeParams = propNode.argument.typeArguments
788+
|| propNode.argument.typeParameters;
788789
if (propNodeTypeParams) {
789790
this.visitTSNode(propNodeTypeParams);
790791
} else {
@@ -806,8 +807,8 @@ module.exports = function propTypesInstructions(context, components, utils) {
806807
});
807808
break;
808809
case 'CallExpression':
809-
if (res.typeParameters) {
810-
this.visitTSNode(res.typeParameters);
810+
if (res.typeArguments || res.typeParameters) {
811+
this.visitTSNode(res.typeArguments || res.typeParameters);
811812
} else {
812813
// Ignore this CallExpression return value since it doesn't have any typeParameters to let us know it's types.
813814
this.shouldIgnorePropTypes = true;
@@ -992,7 +993,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
992993
break;
993994
case 'GenericTypeAnnotation':
994995
if (propTypes.id.name === '$ReadOnly') {
995-
const propTypeParams = propTypes.typeParameters;
996+
const propTypeParams = propTypes.typeArguments || propTypes.typeParameters;
996997
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(
997998
propTypeParams.params[0],
998999
declaredPropTypes
@@ -1034,8 +1035,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
10341035
if (
10351036
node.parent
10361037
&& node.parent.callee
1037-
&& node.parent.typeParameters
1038-
&& node.parent.typeParameters.params
1038+
&& (
1039+
(node.parent.typeArguments && node.parent.typeArguments.params)
1040+
|| (node.parent.typeParameters && node.parent.typeParameters.params)
1041+
)
10391042
&& (
10401043
node.parent.callee.name === 'forwardRef' || (
10411044
node.parent.callee.object
@@ -1045,7 +1048,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
10451048
)
10461049
)
10471050
) {
1048-
const propTypesParams = node.parent.typeParameters;
1051+
const propTypesParams = node.parent.typeArguments || node.parent.typeParameters;
10491052
const declaredPropTypes = {};
10501053
const obj = new DeclarePropTypesForTSTypeAnnotation(propTypesParams.params[1], declaredPropTypes, rootNode);
10511054
components.set(node, {
@@ -1093,7 +1096,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
10931096
if (
10941097
annotation
10951098
&& annotation.type !== 'TSTypeReference'
1096-
&& annotation.typeParameters == null
1099+
&& (annotation.typeArguments == null || annotation.typeParameters == null)
10971100
) {
10981101
return;
10991102
}

0 commit comments

Comments
 (0)