Skip to content

Commit

Permalink
buildReactDeclarationTypes to always return an object
Browse files Browse the repository at this point in the history
  • Loading branch information
Joachim Seminck committed Sep 7, 2017
1 parent 0ee5e93 commit 357dcda
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
31 changes: 11 additions & 20 deletions lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ module.exports = {
value.callee.object &&
hasCustomValidator(value.callee.object.name)
) {
return true;
return {};
}

if (
Expand Down Expand Up @@ -387,23 +387,20 @@ module.exports = {
switch (callName) {
case 'shape':
if (skipShapeProps) {
return true;
return {};
}

if (argument.type !== 'ObjectExpression') {
// Invalid proptype or cannot analyse statically
return true;
return {};
}
const shapeTypeDefinition = {
type: 'shape',
children: []
};
iterateProperties(argument.properties, (childKey, childValue) => {
const fullName = [parentName, childKey].join('.');
let types = buildReactDeclarationTypes(childValue, fullName);
if (types === true) {
types = {};
}
const types = buildReactDeclarationTypes(childValue, fullName);
types.fullName = fullName;
types.name = childKey;
types.node = childValue;
Expand All @@ -413,10 +410,7 @@ module.exports = {
case 'arrayOf':
case 'objectOf':
const fullName = [parentName, '*'].join('.');
let child = buildReactDeclarationTypes(argument, fullName);
if (child === true) {
child = {};
}
const child = buildReactDeclarationTypes(argument, fullName);
child.fullName = fullName;
child.name = '__ANY_KEY__';
child.node = argument;
Expand All @@ -430,7 +424,7 @@ module.exports = {
!argument.elements.length
) {
// Invalid proptype or cannot analyse statically
return true;
return {};
}
const unionTypeDefinition = {
type: 'union',
Expand All @@ -439,7 +433,7 @@ module.exports = {
for (let i = 0, j = argument.elements.length; i < j; i++) {
const type = buildReactDeclarationTypes(argument.elements[i], parentName);
// keep only complex type
if (type !== true) {
if (Object.keys(type).length > 0) {
if (type.children === true) {
// every child is accepted for one type, abort type analysis
unionTypeDefinition.children = true;
Expand All @@ -451,7 +445,7 @@ module.exports = {
}
if (unionTypeDefinition.length === 0) {
// no complex type found, simply accept everything
return true;
return {};
}
return unionTypeDefinition;
case 'instanceOf':
Expand All @@ -462,11 +456,11 @@ module.exports = {
};
case 'oneOf':
default:
return true;
return {};
}
}
// Unknown property or accepts everything (any, object, ...)
return true;
return {};
}

/**
Expand Down Expand Up @@ -792,10 +786,7 @@ module.exports = {
ignorePropsValidation = true;
return;
}
let types = buildReactDeclarationTypes(value, key);
if (types === true) {
types = {};
}
const types = buildReactDeclarationTypes(value, key);
types.fullName = key;
types.name = key;
types.node = value;
Expand Down
18 changes: 9 additions & 9 deletions lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ module.exports = {
// If it's a computed property, we can't make any further analysis, but is valid
return key === '__COMPUTED_PROP__';
}
if (propType === true || typeof propType === 'object' && Object.keys(propType).length === 0) {
if (typeof propType === 'object' && Object.keys(propType).length === 0) {
return true;
}
// Consider every children as declared
Expand Down Expand Up @@ -379,7 +379,7 @@ module.exports = {
* Creates the representation of the React propTypes for the component.
* The representation is used to verify nested used properties.
* @param {ASTNode} value Node of the PropTypes for the desired property
* @return {Object|Boolean} The representation of the declaration, true means
* @return {Object} The representation of the declaration, empty object means
* the property is declared without the need for further analysis.
*/
function buildReactDeclarationTypes(value) {
Expand All @@ -389,7 +389,7 @@ module.exports = {
value.callee.object &&
hasCustomValidator(value.callee.object.name)
) {
return true;
return {};
}

if (
Expand Down Expand Up @@ -418,7 +418,7 @@ module.exports = {
case 'shape':
if (argument.type !== 'ObjectExpression') {
// Invalid proptype or cannot analyse statically
return true;
return {};
}
const shapeTypeDefinition = {
type: 'shape',
Expand All @@ -442,7 +442,7 @@ module.exports = {
!argument.elements.length
) {
// Invalid proptype or cannot analyse statically
return true;
return {};
}
const unionTypeDefinition = {
type: 'union',
Expand All @@ -451,7 +451,7 @@ module.exports = {
for (let i = 0, j = argument.elements.length; i < j; i++) {
const type = buildReactDeclarationTypes(argument.elements[i]);
// keep only complex type
if (type !== true) {
if (Object.keys(type).length > 0) {
if (type.children === true) {
// every child is accepted for one type, abort type analysis
unionTypeDefinition.children = true;
Expand All @@ -463,7 +463,7 @@ module.exports = {
}
if (unionTypeDefinition.length === 0) {
// no complex type found, simply accept everything
return true;
return {};
}
return unionTypeDefinition;
case 'instanceOf':
Expand All @@ -474,11 +474,11 @@ module.exports = {
};
case 'oneOf':
default:
return true;
return {};
}
}
// Unknown property or accepts everything (any, object, ...)
return true;
return {};
}

/**
Expand Down

0 comments on commit 357dcda

Please sign in to comment.