Skip to content

Commit 3e9aefe

Browse files
author
Joachim Seminck
committed
Make buildTypeAnnotationDeclarationTypes always return an object instead of boolean or an object
1 parent 3e4d184 commit 3e9aefe

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

lib/rules/no-unused-prop-types.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ module.exports = {
474474
* The representation is used to verify nested used properties.
475475
* @param {ASTNode} annotation Type annotation for the props class property.
476476
* @param {String} parentName Name of the parent prop node.
477-
* @return {Object|Boolean} The representation of the declaration, true means
477+
* @return {Object} The representation of the declaration, an empty object means
478478
* the property is declared without the need for further analysis.
479479
*/
480480
function buildTypeAnnotationDeclarationTypes(annotation, parentName) {
@@ -483,21 +483,18 @@ module.exports = {
483483
if (typeScope(annotation.id.name)) {
484484
return buildTypeAnnotationDeclarationTypes(typeScope(annotation.id.name), parentName);
485485
}
486-
return true;
486+
return {};
487487
case 'ObjectTypeAnnotation':
488488
if (skipShapeProps) {
489-
return true;
489+
return {};
490490
}
491491
const shapeTypeDefinition = {
492492
type: 'shape',
493493
children: []
494494
};
495495
iterateProperties(annotation.properties, (childKey, childValue) => {
496496
const fullName = [parentName, childKey].join('.');
497-
let types = buildTypeAnnotationDeclarationTypes(childValue, fullName);
498-
if (types === true) {
499-
types = {};
500-
}
497+
const types = buildTypeAnnotationDeclarationTypes(childValue, fullName);
501498
types.fullName = fullName;
502499
types.name = childKey;
503500
types.node = childValue;
@@ -512,7 +509,7 @@ module.exports = {
512509
for (let i = 0, j = annotation.types.length; i < j; i++) {
513510
const type = buildTypeAnnotationDeclarationTypes(annotation.types[i], parentName);
514511
// keep only complex type
515-
if (type !== true) {
512+
if (Object.keys(type).length > 0) {
516513
if (type.children === true) {
517514
// every child is accepted for one type, abort type analysis
518515
unionTypeDefinition.children = true;
@@ -523,16 +520,13 @@ module.exports = {
523520
unionTypeDefinition.children.push(type);
524521
}
525522
if (unionTypeDefinition.children.length === 0) {
526-
// no complex type found, simply accept everything
527-
return true;
523+
// no complex type found
524+
return {};
528525
}
529526
return unionTypeDefinition;
530527
case 'ArrayTypeAnnotation':
531528
const fullName = [parentName, '*'].join('.');
532-
let child = buildTypeAnnotationDeclarationTypes(annotation.elementType, fullName);
533-
if (child === true) {
534-
child = {};
535-
}
529+
const child = buildTypeAnnotationDeclarationTypes(annotation.elementType, fullName);
536530
child.fullName = fullName;
537531
child.name = '__ANY_KEY__';
538532
child.node = annotation;
@@ -542,7 +536,7 @@ module.exports = {
542536
};
543537
default:
544538
// Unknown or accepts everything.
545-
return true;
539+
return {};
546540
}
547541
}
548542

@@ -741,10 +735,7 @@ module.exports = {
741735
return;
742736
}
743737

744-
let types = buildTypeAnnotationDeclarationTypes(value, key);
745-
if (types === true) {
746-
types = {};
747-
}
738+
const types = buildTypeAnnotationDeclarationTypes(value, key);
748739
types.fullName = key;
749740
types.name = key;
750741
types.node = value;

lib/rules/prop-types.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ module.exports = {
249249
function _isDeclaredInComponent(declaredPropTypes, keyList) {
250250
for (let i = 0, j = keyList.length; i < j; i++) {
251251
const key = keyList[i];
252+
252253
const propType = (
253254
declaredPropTypes && (
254255
// Check if this key is declared
@@ -261,7 +262,7 @@ module.exports = {
261262
// If it's a computed property, we can't make any further analysis, but is valid
262263
return key === '__COMPUTED_PROP__';
263264
}
264-
if (propType === true) {
265+
if (propType === true || typeof propType === 'object' && Object.keys(propType).length === 0) {
265266
return true;
266267
}
267268
// Consider every children as declared
@@ -308,6 +309,7 @@ module.exports = {
308309
function isDeclaredInComponent(node, names) {
309310
while (node) {
310311
const component = components.get(node);
312+
311313
const isDeclared =
312314
component && component.confidence === 2 &&
313315
_isDeclaredInComponent(component.declaredPropTypes || {}, names)
@@ -483,7 +485,7 @@ module.exports = {
483485
* Creates the representation of the React props type annotation for the component.
484486
* The representation is used to verify nested used properties.
485487
* @param {ASTNode} annotation Type annotation for the props class property.
486-
* @return {Object|Boolean} The representation of the declaration, true means
488+
* @return {Object} The representation of the declaration, empty object means
487489
* the property is declared without the need for further analysis.
488490
*/
489491
function buildTypeAnnotationDeclarationTypes(annotation) {
@@ -492,7 +494,7 @@ module.exports = {
492494
if (typeScope(annotation.id.name)) {
493495
return buildTypeAnnotationDeclarationTypes(typeScope(annotation.id.name));
494496
}
495-
return true;
497+
return {};
496498
case 'ObjectTypeAnnotation':
497499
let containsObjectTypeSpread = false;
498500
const shapeTypeDefinition = {
@@ -509,7 +511,7 @@ module.exports = {
509511

510512
// nested object type spread means we need to ignore/accept everything in this object
511513
if (containsObjectTypeSpread) {
512-
return true;
514+
return {};
513515
}
514516
return shapeTypeDefinition;
515517
case 'UnionTypeAnnotation':
@@ -520,7 +522,7 @@ module.exports = {
520522
for (let i = 0, j = annotation.types.length; i < j; i++) {
521523
const type = buildTypeAnnotationDeclarationTypes(annotation.types[i]);
522524
// keep only complex type
523-
if (type !== true) {
525+
if (Object.keys(type).length > 0) {
524526
if (type.children === true) {
525527
// every child is accepted for one type, abort type analysis
526528
unionTypeDefinition.children = true;
@@ -532,7 +534,7 @@ module.exports = {
532534
}
533535
if (unionTypeDefinition.children.length === 0) {
534536
// no complex type found, simply accept everything
535-
return true;
537+
return {};
536538
}
537539
return unionTypeDefinition;
538540
case 'ArrayTypeAnnotation':
@@ -544,7 +546,7 @@ module.exports = {
544546
};
545547
default:
546548
// Unknown or accepts everything.
547-
return true;
549+
return {};
548550
}
549551
}
550552

@@ -726,6 +728,7 @@ module.exports = {
726728
ignorePropsValidation = true;
727729
return;
728730
}
731+
729732
declaredPropTypes[key] = buildTypeAnnotationDeclarationTypes(value);
730733
});
731734

0 commit comments

Comments
 (0)