Skip to content

Add more support for wrapped propTypes #1272

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

Merged
merged 7 commits into from
Jun 26, 2017

Conversation

dustinsoftware
Copy link
Contributor

@dustinsoftware dustinsoftware commented Jun 24, 2017

Fixes #1266

@ljharb
Copy link
Member

ljharb commented Jun 24, 2017

Thanks!

I think it'd be better, rather than only adding this option to individual rules, to also add it to "settings" (like the React pragma).

Basically, individual rule settings would take precedence, and lacking that, the global settings would be used.

@dustinsoftware
Copy link
Contributor Author

Ah, right after pushing this, I saw that you'd like to have it in settings and individual rule configuration. What case did you have in mind where it would be necessary to have the option to override it per-rule?

@dustinsoftware dustinsoftware changed the title Add support for wrapped propTypes to require-default-props Add more support for wrapped propTypes Jun 24, 2017
@ljharb
Copy link
Member

ljharb commented Jun 24, 2017

No use case specifically; just aiming for maximum configurability.

Let's keep it how you have it for now, and we can add the specific-rule settings later if needed.

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.

This is great! It's also not a breaking change because your previous PR hasn't been released yet.

It'll need some documentation in the readme, however.

@@ -63,7 +56,7 @@ module.exports = {
var configuration = Object.assign({}, defaults, context.options[0] || {});
var skipShapeProps = configuration.skipShapeProps;
var customValidators = configuration.customValidators || [];
var propWrapperFunctions = new Set(configuration.propWrapperFunctions || []);
var propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);
Copy link
Member

Choose a reason for hiding this comment

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

will context.settings always exist and be an object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I tested both with and without settings defined - context.settings will always be an object.

if (
node.type === 'CallExpression' &&
propWrapperFunctions.has(node.callee.name) &&
node.arguments && node.arguments[0]
Copy link
Member

Choose a reason for hiding this comment

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

this should probably be node.arguments.length > 0?

Copy link
Contributor Author

@dustinsoftware dustinsoftware Jun 24, 2017

Choose a reason for hiding this comment

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

It shouldn't matter, if arguments is empty, then arguments[0] will return undefined, which will cause this expression to evaluate false. It also handles the case where the first element of arguments contains something that is falsy :)

It looks like checking the arguments array this way also matches code style in other places in this project.

Copy link
Member

@ljharb ljharb Jun 24, 2017

Choose a reason for hiding this comment

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

in v8, accessing an arguments index that's greater than length deoptimizes, so it'd be a performance issue - altho in this case it's not an arguments object, so it's probably fine :-)

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.

LGTM, thanks!

@ljharb ljharb requested review from yannickcr, lencioni and EvHaus June 24, 2017 17:33
@ljharb
Copy link
Member

ljharb commented Jun 24, 2017

This needs to be merged before the next release, so if I don't hear any objections by Monday, I'm going to merge it.

@dustinsoftware
Copy link
Contributor Author

Pushed support for a couple more, I think that's all the major prop-type rules.

@ljharb
Copy link
Member

ljharb commented Jun 25, 2017

I wonder if, like Components.detect, we could make a PropTypes.detect function that would make this logic reusable?

@dustinsoftware
Copy link
Contributor Author

I think that could be helpful, but maybe out of scope for this PR. There's a lot of logic around finding declared prop types that is duplicated across multiple rules.

@ljharb ljharb merged commit 5a55e16 into jsx-eslint:master Jun 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants