Skip to content

Commit 357dcda

Browse files
author
Joachim Seminck
committed
buildReactDeclarationTypes to always return an object
1 parent 0ee5e93 commit 357dcda

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ module.exports = {
359359
value.callee.object &&
360360
hasCustomValidator(value.callee.object.name)
361361
) {
362-
return true;
362+
return {};
363363
}
364364

365365
if (
@@ -387,23 +387,20 @@ module.exports = {
387387
switch (callName) {
388388
case 'shape':
389389
if (skipShapeProps) {
390-
return true;
390+
return {};
391391
}
392392

393393
if (argument.type !== 'ObjectExpression') {
394394
// Invalid proptype or cannot analyse statically
395-
return true;
395+
return {};
396396
}
397397
const shapeTypeDefinition = {
398398
type: 'shape',
399399
children: []
400400
};
401401
iterateProperties(argument.properties, (childKey, childValue) => {
402402
const fullName = [parentName, childKey].join('.');
403-
let types = buildReactDeclarationTypes(childValue, fullName);
404-
if (types === true) {
405-
types = {};
406-
}
403+
const types = buildReactDeclarationTypes(childValue, fullName);
407404
types.fullName = fullName;
408405
types.name = childKey;
409406
types.node = childValue;
@@ -413,10 +410,7 @@ module.exports = {
413410
case 'arrayOf':
414411
case 'objectOf':
415412
const fullName = [parentName, '*'].join('.');
416-
let child = buildReactDeclarationTypes(argument, fullName);
417-
if (child === true) {
418-
child = {};
419-
}
413+
const child = buildReactDeclarationTypes(argument, fullName);
420414
child.fullName = fullName;
421415
child.name = '__ANY_KEY__';
422416
child.node = argument;
@@ -430,7 +424,7 @@ module.exports = {
430424
!argument.elements.length
431425
) {
432426
// Invalid proptype or cannot analyse statically
433-
return true;
427+
return {};
434428
}
435429
const unionTypeDefinition = {
436430
type: 'union',
@@ -439,7 +433,7 @@ module.exports = {
439433
for (let i = 0, j = argument.elements.length; i < j; i++) {
440434
const type = buildReactDeclarationTypes(argument.elements[i], parentName);
441435
// keep only complex type
442-
if (type !== true) {
436+
if (Object.keys(type).length > 0) {
443437
if (type.children === true) {
444438
// every child is accepted for one type, abort type analysis
445439
unionTypeDefinition.children = true;
@@ -451,7 +445,7 @@ module.exports = {
451445
}
452446
if (unionTypeDefinition.length === 0) {
453447
// no complex type found, simply accept everything
454-
return true;
448+
return {};
455449
}
456450
return unionTypeDefinition;
457451
case 'instanceOf':
@@ -462,11 +456,11 @@ module.exports = {
462456
};
463457
case 'oneOf':
464458
default:
465-
return true;
459+
return {};
466460
}
467461
}
468462
// Unknown property or accepts everything (any, object, ...)
469-
return true;
463+
return {};
470464
}
471465

472466
/**
@@ -792,10 +786,7 @@ module.exports = {
792786
ignorePropsValidation = true;
793787
return;
794788
}
795-
let types = buildReactDeclarationTypes(value, key);
796-
if (types === true) {
797-
types = {};
798-
}
789+
const types = buildReactDeclarationTypes(value, key);
799790
types.fullName = key;
800791
types.name = key;
801792
types.node = value;

lib/rules/prop-types.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ module.exports = {
262262
// If it's a computed property, we can't make any further analysis, but is valid
263263
return key === '__COMPUTED_PROP__';
264264
}
265-
if (propType === true || typeof propType === 'object' && Object.keys(propType).length === 0) {
265+
if (typeof propType === 'object' && Object.keys(propType).length === 0) {
266266
return true;
267267
}
268268
// Consider every children as declared
@@ -379,7 +379,7 @@ module.exports = {
379379
* Creates the representation of the React propTypes for the component.
380380
* The representation is used to verify nested used properties.
381381
* @param {ASTNode} value Node of the PropTypes for the desired property
382-
* @return {Object|Boolean} The representation of the declaration, true means
382+
* @return {Object} The representation of the declaration, empty object means
383383
* the property is declared without the need for further analysis.
384384
*/
385385
function buildReactDeclarationTypes(value) {
@@ -389,7 +389,7 @@ module.exports = {
389389
value.callee.object &&
390390
hasCustomValidator(value.callee.object.name)
391391
) {
392-
return true;
392+
return {};
393393
}
394394

395395
if (
@@ -418,7 +418,7 @@ module.exports = {
418418
case 'shape':
419419
if (argument.type !== 'ObjectExpression') {
420420
// Invalid proptype or cannot analyse statically
421-
return true;
421+
return {};
422422
}
423423
const shapeTypeDefinition = {
424424
type: 'shape',
@@ -442,7 +442,7 @@ module.exports = {
442442
!argument.elements.length
443443
) {
444444
// Invalid proptype or cannot analyse statically
445-
return true;
445+
return {};
446446
}
447447
const unionTypeDefinition = {
448448
type: 'union',
@@ -451,7 +451,7 @@ module.exports = {
451451
for (let i = 0, j = argument.elements.length; i < j; i++) {
452452
const type = buildReactDeclarationTypes(argument.elements[i]);
453453
// keep only complex type
454-
if (type !== true) {
454+
if (Object.keys(type).length > 0) {
455455
if (type.children === true) {
456456
// every child is accepted for one type, abort type analysis
457457
unionTypeDefinition.children = true;
@@ -463,7 +463,7 @@ module.exports = {
463463
}
464464
if (unionTypeDefinition.length === 0) {
465465
// no complex type found, simply accept everything
466-
return true;
466+
return {};
467467
}
468468
return unionTypeDefinition;
469469
case 'instanceOf':
@@ -474,11 +474,11 @@ module.exports = {
474474
};
475475
case 'oneOf':
476476
default:
477-
return true;
477+
return {};
478478
}
479479
}
480480
// Unknown property or accepts everything (any, object, ...)
481-
return true;
481+
return {};
482482
}
483483

484484
/**

0 commit comments

Comments
 (0)