Skip to content
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

Additional cases for prop-types and no-unused-prop-types #1939

Merged
merged 3 commits into from Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/rules/no-unused-prop-types.js
Expand Up @@ -146,7 +146,7 @@ module.exports = {
function mustBeValidated(component) {
return Boolean(
component &&
!component.ignorePropsValidation
!component.ignoreUnusedPropTypesValidation
);
}

Expand Down Expand Up @@ -397,7 +397,7 @@ module.exports = {

const component = components.get(utils.getParentComponent());
const usedPropTypes = component && component.usedPropTypes || [];
let ignorePropsValidation = component && component.ignorePropsValidation || false;
let ignoreUnusedPropTypesValidation = component && component.ignoreUnusedPropTypesValidation || false;

switch (type) {
case 'direct':
Expand All @@ -414,7 +414,7 @@ module.exports = {
case 'destructuring':
for (let k = 0, l = (properties || []).length; k < l; k++) {
if (hasSpreadOperator(properties[k]) || properties[k].computed) {
ignorePropsValidation = true;
ignoreUnusedPropTypesValidation = true;
break;
}
const propName = getKeyValue(properties[k]);
Expand All @@ -441,7 +441,7 @@ module.exports = {

components.set(component ? component.node : node, {
usedPropTypes: usedPropTypes,
ignorePropsValidation: ignorePropsValidation
ignoreUnusedPropTypesValidation: ignoreUnusedPropTypesValidation
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prop-types.js
Expand Up @@ -191,7 +191,7 @@ module.exports = {
return true;
}
// Consider every children as declared
if (propType.children === true) {
if (propType.children === true || propType.containsSpread) {
return true;
}
if (propType.acceptedProperties) {
Expand Down
10 changes: 5 additions & 5 deletions lib/util/propTypes.js
Expand Up @@ -143,10 +143,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
}
});

// nested object type spread means we need to ignore/accept everything in this object
if (containsObjectTypeSpread) {
return {};
}
// Mark if this shape has spread. We will know to consider all props from this shape as having propTypes,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the case with propTypes, if the shape contains a spread plus some properties e.g.

type Props = {
  person: {
    ...$Exact<BasePerson>,
    lastname: string
  }
};

it would be unwise to throw away the information we have about lastname just because we have a spread. By setting an additional flag and leaving the children data in place, we can detect if lastname is being used.

// but still have the ability to detect unused children of this shape.
shapeTypeDefinition.containsSpread = containsObjectTypeSpread;

return shapeTypeDefinition;
},

Expand Down Expand Up @@ -669,7 +669,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
JSXSpreadAttribute: function(node) {
const component = components.get(utils.getParentComponent());
components.set(component ? component.node : node, {
ignorePropsValidation: true
ignoreUnusedPropTypesValidation: true
});
},

Expand Down