-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Rule proposal: disallow referencing another component's propTypes #696
Comments
I think there should be a way to allow it in case you use the PropTypes of a given component as part of the PropTypes definition in a containing component. This is a contrived example that doesn't really gain much of anything, but I think that in such constructs, it's better to validate the proptypes if you know what they're supposed to be than taking in import React, { PropTypes } from 'react'
import Child from './Child'
export const Parent = ({ childProps }) => (
<Child {...childProps}/>
)
Parent.propTypes = {
childProps: PropTypes.shape(Child.propTypes).isRequired
} |
I think it is fine to allow this as long as we can reasonably detect this type of usage. |
@oliviertassinari @nkt any thoughts on this proposal or interest in picking it up? :) |
@lencioni I have missed that proposal. That's a good idea! You are definitely not the only one wondering. That's the biggest risk I'm aware of when using this babel transformation plugin. Regarding the implementation, I don't think that eslint is able to resolve imports. |
I think referencing the propTypes property on any object would be good enough. |
So, it would error out on things like these: Foo.propTypes;
const { propTypes } = Foo;
Foo['propTypes']; There may be other patterns to look for. @ljharb can probably think of some. |
@oliviertassinari |
If this is specific to |
The goal is to be able to strip propTypes but ensure that doing so won't break any code. I don't have any intuition about where the best place for the error is, but having it in eslint might let it be caught earlier. |
People may want to use babel-plugin-transform-react-remove-prop-types to remove propTypes from their components in production builds, as an optimization. The `forbib-foreign-prop-types` rule forbids using another component's prop types unless they are explicitly imported/exported, which makes that optimization less prone to error. Fixes jsx-eslint#696
People may want to use babel-plugin-transform-react-remove-prop-types to remove propTypes from their components in production builds, as an optimization. The `forbib-foreign-prop-types` rule forbids using another component's prop types unless they are explicitly imported/exported, which makes that optimization less prone to error. Fixes jsx-eslint#696
People may want to use babel-plugin-transform-react-remove-prop-types to remove propTypes from their components in production builds, as an optimization. The `forbib-foreign-prop-types` rule forbids using another component's prop types unless they are explicitly imported/exported, which makes that optimization less prone to error. Fixes jsx-eslint#696
People may want to use babel-plugin-transform-react-remove-prop-types to remove propTypes from their components in production builds, as an optimization. The `forbib-foreign-prop-types` rule forbids using another component's prop types unless they are explicitly imported/exported, which makes that optimization less prone to error. Fixes jsx-eslint#696
People may want to use babel-plugin-transform-react-remove-prop-types to remove propTypes from their components in production builds, as an optimization. The `forbib-foreign-prop-types` rule forbids using another component's prop types unless they are explicitly imported/exported, which makes that optimization less prone to error. Fixes jsx-eslint#696
Some people might want to use babel-plugin-transform-react-remove-prop-types to remove propTypes from their components in production builds, as an optimization. This transform changes code like:
into:
This works, but might end up breaking things in production if you import a component and then reference that component's propTypes. Instead, the propTypes should be separately exported in this case and referenced directly from the export.
Bad:
Good:
This rule would make the aforementioned Babel plugin safe to use.
The text was updated successfully, but these errors were encountered: