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

Conversation

alexzherdev
Copy link
Contributor

@alexzherdev alexzherdev commented Aug 15, 2018

So I was adding test cases to both rules to bring them to parity after the refactoring, but a couple of things didn't make sense.
In particular, having just one flag for ignoring props validation doesn't seem to be enough (and that was the case before the refactoring as well). For instance, let's say we encounter a spread in props e.g.

type Props = {
  ...data,
  lastname: string
};

That means any this.props.foo usage is considered to have a corresponding propType and it's ignored in prop-types via ignorePropsValidation flag we set in the helper.
However, this shouldn't preclude us from being able to detect whether lastname is an unused propType in no-unused-prop-types (this would be ignored if we met a {...props} spread). But we can't rely on the same flag, because that flag means a different thing. So I'm proposing to split it into two.

Also see comments inline.

Resolves #836

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.

@@ -1801,34 +1801,6 @@ ruleTester.run('no-unused-prop-types', rule, {
' bar: PropTypes.bool',
'};'
].join('\n')
}, {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So these tests are now able to detect that lastname is not used. I understand this is a breaking change? Not insisting on any of these to go in, but seems like a good change to me.

Copy link
Member

Choose a reason for hiding this comment

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

I think we could consider it a bug that the rule missed it previously.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Can these new test cases also be added that don't use flow?

@alexzherdev
Copy link
Contributor Author

Sure. Wanted to start with a smaller set to validate the idea.

@alexzherdev
Copy link
Contributor Author

Travis is flakey.

@ljharb
Copy link
Member

ljharb commented Aug 15, 2018

Indeed; don't worry about those errors, I habitually rerun them when i see them.

@alexzherdev
Copy link
Contributor Author

It’d be great if this could get merged before I push the used prop type refactoring, as they’re slightly interrelated.

@ThiefMaster
Copy link
Contributor

I think this also fixed #1885 and #1473

@ghost ghost mentioned this pull request Jan 12, 2019
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants