Skip to content

Commit 1bec55d

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 b4b7497 commit 1bec55d

5 files changed

+20
-16
lines changed

lib/rules/boolean-prop-naming.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ module.exports = {
250250
return;
251251
}
252252

253-
const annotationTypeParams = component.node.parent.id.typeAnnotation.typeAnnotation.typeParameters;
253+
const typeAnnotation = component.node.parent.id.typeAnnotation.typeAnnotation;
254+
const annotationTypeParams = typeAnnotation.typeArguments || typeAnnotation.typeParameters;
254255
if (
255256
annotationTypeParams && (
256257
annotationTypeParams.type === 'TSTypeParameterInstantiation'

lib/rules/function-component-definition.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const UNNAMED_FUNCTION_TEMPLATES = {
3333
};
3434

3535
function hasOneUnconstrainedTypeParam(node) {
36-
const nodeTypeParams = node.typeParameters;
36+
const nodeTypeParams = node.typeArguments || node.typeParameters;
3737

3838
return nodeTypeParams
3939
&& nodeTypeParams.params
@@ -205,7 +205,7 @@ module.exports = {
205205
options.range,
206206
buildFunction(options.template, {
207207
typeAnnotation,
208-
typeParams: getNodeText(node.typeParameters, source),
208+
typeParams: getNodeText(node.typeArguments || node.typeParameters, source),
209209
params: getParams(node, source),
210210
returnType: getNodeText(node.returnType, source),
211211
body: getBody(node, source),

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module.exports = {
5454
report(context, messages.propOnNewLine, 'propOnNewLine', {
5555
node: decl,
5656
fix(fixer) {
57-
return fixer.replaceTextRange([(node.typeParameters || node.name).range[1], decl.range[0]], '\n');
57+
return fixer.replaceTextRange([(node.typeArguments || node.typeParameters || node.name).range[1], decl.range[0]], '\n');
5858
},
5959
});
6060
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ module.exports = {
9898
}
9999

100100
function containsGenericType(node) {
101-
const nodeTypeParams = node.typeParameters;
101+
const nodeTypeParams = node.typeArguments || node.typeParameters;
102102
if (typeof nodeTypeParams === 'undefined') {
103103
return false;
104104
}
@@ -109,7 +109,7 @@ module.exports = {
109109
function getGenericNode(node) {
110110
const name = node.name;
111111
if (containsGenericType(node)) {
112-
const type = node.typeParameters;
112+
const type = node.typeArguments || node.typeParameters;
113113

114114
return Object.assign(
115115
{},

lib/util/propTypes.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ 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-
const nodeTypeParams = node.typeParameters;
630+
const nodeTypeParams = node.typeArguments || node.typeParameters;
631631
if (shouldTraverseTypeParams && nodeTypeParams && nodeTypeParams.length !== 0) {
632632
// All react Generic types are derived from:
633633
// type PropsWithChildren<P> = P & { children?: ReactNode | undefined }
@@ -728,7 +728,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
728728

729729
convertReturnTypeToPropTypes(node) {
730730
// ReturnType<T> should always have one parameter
731-
const nodeTypeParams = node.typeParameters;
731+
const nodeTypeParams = node.typeArguments || node.typeParameters;
732732
if (nodeTypeParams) {
733733
if (nodeTypeParams.params.length === 1) {
734734
let returnType = nodeTypeParams.params[0];
@@ -763,7 +763,8 @@ module.exports = function propTypesInstructions(context, components, utils) {
763763
case 'ObjectExpression':
764764
iterateProperties(context, res.properties, (key, value, propNode) => {
765765
if (propNode && propNode.argument && propNode.argument.type === 'CallExpression') {
766-
const propNodeTypeParams = propNode.argument.typeParameters;
766+
const propNodeTypeParams = propNode.argument.typeArguments
767+
|| propNode.argument.typeParameters;
767768
if (propNodeTypeParams) {
768769
this.visitTSNode(propNodeTypeParams);
769770
} else {
@@ -785,8 +786,8 @@ module.exports = function propTypesInstructions(context, components, utils) {
785786
});
786787
break;
787788
case 'CallExpression':
788-
if (res.typeParameters) {
789-
this.visitTSNode(res.typeParameters);
789+
if (res.typeArguments || res.typeParameters) {
790+
this.visitTSNode(res.typeArguments || res.typeParameters);
790791
} else {
791792
// Ignore this CallExpression return value since it doesn't have any typeParameters to let us know it's types.
792793
this.shouldIgnorePropTypes = true;
@@ -963,7 +964,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
963964
break;
964965
case 'GenericTypeAnnotation':
965966
if (propTypes.id.name === '$ReadOnly') {
966-
const propTypeParams = propTypes.typeParameters;
967+
const propTypeParams = propTypes.typeArguments || propTypes.typeParameters;
967968
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(
968969
propTypeParams.params[0],
969970
declaredPropTypes
@@ -1004,8 +1005,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
10041005
if (
10051006
node.parent
10061007
&& node.parent.callee
1007-
&& node.parent.typeParameters
1008-
&& node.parent.typeParameters.params
1008+
&& (
1009+
(node.parent.typeArguments && node.parent.typeArguments.params)
1010+
|| (node.parent.typeParameters && node.parent.typeParameters.params)
1011+
)
10091012
&& (
10101013
node.parent.callee.name === 'forwardRef' || (
10111014
node.parent.callee.object
@@ -1015,7 +1018,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
10151018
)
10161019
)
10171020
) {
1018-
const propTypesParams = node.parent.typeParameters;
1021+
const propTypesParams = node.parent.typeArguments || node.parent.typeParameters;
10191022
const declaredPropTypes = {};
10201023
const obj = new DeclarePropTypesForTSTypeAnnotation(propTypesParams.params[1], declaredPropTypes);
10211024
components.set(node, {
@@ -1063,7 +1066,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
10631066
if (
10641067
annotation
10651068
&& annotation.type !== 'TSTypeReference'
1066-
&& annotation.typeParameters == null
1069+
&& (annotation.typeArguments == null || annotation.typeParameters == null)
10671070
) {
10681071
return;
10691072
}

0 commit comments

Comments
 (0)