-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix issues with IntersectionTypeAnnotation for prop-types and no-unused-prop-types #1415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
87aca04
a771125
20f75ee
19a2911
3e4d184
3e9aefe
0ee5e93
357dcda
09c4ed7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -724,6 +724,70 @@ module.exports = { | |
}); | ||
} | ||
|
||
/** | ||
* Marks all props found inside ObjectTypeAnnotaiton as declared. | ||
* | ||
* Modifies the declaredProperties object | ||
* @param {ASTNode} propTypes | ||
* @param {Object} declaredPropTypes | ||
* @returns {Boolean} True if propTypes should be ignored (e.g. when a type can't be resolved, when it is imported) | ||
*/ | ||
function declarePropTypesForObjectTypeAnnotation(propTypes, declaredPropTypes) { | ||
let ignorePropsValidation = false; | ||
|
||
iterateProperties(propTypes.properties, (key, value) => { | ||
if (!value) { | ||
ignorePropsValidation = true; | ||
return; | ||
} | ||
|
||
let types = buildTypeAnnotationDeclarationTypes(value, key); | ||
if (types === true) { | ||
types = {}; | ||
} | ||
types.fullName = key; | ||
types.name = key; | ||
types.node = value; | ||
declaredPropTypes.push(types); | ||
}); | ||
|
||
return ignorePropsValidation; | ||
} | ||
|
||
/** | ||
* Marks all props found inside IntersectionTypeAnnotation as declared. | ||
* Since InterSectionTypeAnnotations can be nested, this handles recursively. | ||
* | ||
* Modifies the declaredPropTypes object | ||
* @param {ASTNode} propTypes | ||
* @param {Object} declaredPropTypes | ||
* @returns {Boolean} True if propTypes should be ignored (e.g. when a type can't be resolved, when it is imported) | ||
*/ | ||
function declarePropTypesForIntersectionTypeAnnotation(propTypes, declaredPropTypes) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can these two functions live somewhere they can be shared across the rules that currently duplicate them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I'm working on it 😄 The idea will be that the rule will be declared as:
And this will take care of ALL the shared code between |
||
return propTypes.types.reduce((ignorePropsValidation, annotation) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since this is reducing to a boolean, could it use |
||
// If we already decided to skip props validation then we don't have to continue processing anything else | ||
if (ignorePropsValidation) { | ||
return ignorePropsValidation; | ||
} | ||
|
||
if (annotation.type === 'ObjectTypeAnnotation') { | ||
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(annotation, declaredPropTypes); | ||
} else { | ||
const typeNode = typeScope(annotation.id.name); | ||
|
||
if (!typeNode) { | ||
ignorePropsValidation = true; | ||
} else if (typeNode.type === 'IntersectionTypeAnnotation') { | ||
ignorePropsValidation = declarePropTypesForIntersectionTypeAnnotation(typeNode, declaredPropTypes); | ||
} else { | ||
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(typeNode, declaredPropTypes); | ||
} | ||
} | ||
|
||
return ignorePropsValidation; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i can't decide if this style, or early-returning each place this var is assigned, is better There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly I'm in general not really happy about the fact that a function with a name Perhaps a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could rename the function instead :-) |
||
}, false); | ||
} | ||
|
||
/** | ||
* Mark a prop type as declared | ||
* @param {ASTNode} node The AST node being checked. | ||
|
@@ -736,20 +800,7 @@ module.exports = { | |
|
||
switch (propTypes && propTypes.type) { | ||
case 'ObjectTypeAnnotation': | ||
iterateProperties(propTypes.properties, (key, value) => { | ||
if (!value) { | ||
ignorePropsValidation = true; | ||
return; | ||
} | ||
let types = buildTypeAnnotationDeclarationTypes(value, key); | ||
if (types === true) { | ||
types = {}; | ||
} | ||
types.fullName = key; | ||
types.name = key; | ||
types.node = value; | ||
declaredPropTypes.push(types); | ||
}); | ||
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(propTypes, declaredPropTypes); | ||
break; | ||
case 'ObjectExpression': | ||
iterateProperties(propTypes.properties, (key, value) => { | ||
|
@@ -791,24 +842,7 @@ module.exports = { | |
} | ||
break; | ||
case 'IntersectionTypeAnnotation': | ||
propTypes.types.forEach(annotation => { | ||
const propsType = typeScope(annotation.id.name); | ||
iterateProperties(propsType.properties, (key, value) => { | ||
if (!value) { | ||
ignorePropsValidation = true; | ||
return; | ||
} | ||
|
||
let types = buildTypeAnnotationDeclarationTypes(value, key); | ||
if (types === true) { | ||
types = {}; | ||
} | ||
types.fullName = key; | ||
types.name = key; | ||
types.node = value; | ||
declaredPropTypes.push(types); | ||
}); | ||
}); | ||
ignorePropsValidation = declarePropTypesForIntersectionTypeAnnotation(propTypes, declaredPropTypes); | ||
break; | ||
case null: | ||
break; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of reusing
types
here, can we use multiple vars and useconst
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is from the existing code so didn't touch it yet. But I think we can simply do:
Edit: Ah! We don't support spread operator yet. 😞
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meh, another problem is that
buildTypeAnnotationDeclarationTypes
returns either a boolean or an object which isn't great either. I try to refactor it to always return an object, but it needs changes in more places than just here.But I do think it's a good refactor to do 😄