-
-
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
prop-types improved handling for destructuring and member expressions #1605
base: master
Are you sure you want to change the base?
Conversation
…ing except for MemberExpressions outside of VariableDeclarators
…ypesAsUsed into its own function and simplified processing flow
tests/lib/rules/prop-types.js
Outdated
...SharedPropTypes // eslint-disable-line object-shorthand | ||
}; | ||
`, | ||
a: React.PropTypes.string, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you've made this have 3 spaces of indentation from A.propTypes =
; not that it really matters in tests, but can you revert the extra spaces?
@@ -2445,7 +2443,6 @@ ruleTester.run('prop-types', rule, { | |||
parser: 'babel-eslint', | |||
errors: [ | |||
{message: '\'names\' is missing in props validation'}, | |||
{message: '\'names.map\' is missing in props validation'}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good fix here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ljharb Am I good on all of the changes you asked for?
tests/lib/rules/prop-types.js
Outdated
@@ -34,7 +34,6 @@ require('babel-eslint'); | |||
|
|||
const ruleTester = new RuleTester({parserOptions}); | |||
ruleTester.run('prop-types', rule, { | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this change?
lib/util/Components.js
Outdated
@@ -463,6 +463,7 @@ function componentRule(rule, context) { | |||
let scope = context.getScope(); | |||
while (scope) { | |||
const node = scope.block; | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this change?
lib/rules/prop-types.js
Outdated
* @param {ASTNode} node a VariableDeclarator node. | ||
* @returns {Boolean} True if destructing, false if not. | ||
*/ | ||
function isDestucturedVariableDeclaration(node) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isDestucturedVariableDeclaration → isDestructuredVariableDeclaration
lib/rules/prop-types.js
Outdated
} | ||
|
||
// Ignore Array methods | ||
if (Array.prototype[propertyName]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's be a bit stricter here and check for typeof === 'function'
? Also, this will include Object.prototype methods.
Also, what about when a method is shimmed/polyfilled onto Array.prototype in their code, but not in the node process that eslint is running in? I'm not sure this is a safe way to check it.
lib/rules/prop-types.js
Outdated
const startingNames = processedExpression.parentUsage ? processedExpression.parentUsage.allNames : []; | ||
traverseMemberExpression(processedExpression.startingNode, (propertyName, currentNames, currentNode) => { | ||
// Ignore Object methods | ||
if (Object.prototype[propertyName] || propertyName === '__COMPUTED_PROP__') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same question here about typeof
…rray methods are functions
@cbranch101 out of curiosity, why? |
@MatthewHerbst you can't know statically if |
This rule would really help me out! Looks like the outstanding question is to @ljharb, and a conflict to be resolved. |
@cbranch101 this needs a rebase; I tried doing it but it wasn't a clean one. |
Any progress with this issue? Is there anything that I can do to further this along? |
@davidlewallen if you could make a branch that has these commits, rebased and passing tests, and you post a link to it here (without creating a duplicate PR) then I'd be happy to update this PR with your rebase/additions :-) |
I have a PR upcoming that deduplicates propType usage detection from |
Hey guys, sorry for the delay, got pulled off into some other things, I'll take a look at the rebase this week |
ping @cbranch101, are you still interested in completing this PR? |
@cbranch101 unfortunately it seems like @alexzherdev's propType detection refactors makes this PR not trivially rebaseable. I was able to manually preserve the test cases, but I think you'll need to be the one to look at how to rebase the implementation changes. I'll mark this as a draft in the meantime; please unmark it when it's rebased and ready to go. |
59af733
to
865ed16
Compare
069314a
to
181c68f
Compare
380e32c
to
51d342b
Compare
This adds handling for multiple previously unhandled situations related to destructuring and member expressions. It required that I pretty heavily rework the existing code related to marking prop types usages, but I think it actually simplifies it a bit from what was there before.
Multi-staged destructuring
Destructuring off of member expressions
Renamed destructuring
Member expressions of off destructured variables
Everything should be working as it was before with one exception.
This test used to be reporting an error on
names.map
not being defined in prop type, but I'm now ignoring all array methods when checking prop types off a member expression.Fixes #1422
Fixes #1447