-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix incorrect propType warning inside .map #1304
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
Fix incorrect propType warning inside .map #1304
Conversation
lib/rules/no-unused-prop-types.js
Outdated
@@ -234,8 +234,7 @@ module.exports = { | |||
* @returns {Boolean} True if the prop is used, false if not. | |||
*/ | |||
function isPropUsed(node, prop) { | |||
for (let i = 0, l = node.usedPropTypes.length; i < l; i++) { | |||
const usedProp = node.usedPropTypes[i]; | |||
for (const usedProp of node.usedPropTypes) { |
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.
I'd prefer to avoid for..of
in favor of .forEach
or .map
' two: PropTypes.string', | ||
'};' | ||
].join('\n'), | ||
parser: 'babel-eslint' |
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.
nothing about this test case should require babel-eslint; can it use the default parser instead?
lib/util/Components.js
Outdated
@@ -52,8 +52,10 @@ Components.prototype.add = function(node, confidence) { | |||
* @returns {Object} Component object, undefined if the component is not found | |||
*/ | |||
Components.prototype.get = function(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.
Are there any tests for this change, btw?
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.
I didn't see a good place to put a test for this specific change, and the logic behind get
changes, the test I did add will fail.
54e41b1
to
bc455b8
Compare
Updated |
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.
Your fix LGTM. I'm not stoked on the findParent
arg, but I'll let other collabs weigh in.
Components.prototype.get = function(node) { | ||
const id = this._getId(node); | ||
return this._list[id]; | ||
Components.prototype.get = function(node, findParent) { |
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.
This function is used very heavily across many rules.
I would not recommend adding this new argument here and instead handling the findParent
logic inside the no-unused-prop-types
rule directly.
@@ -617,7 +617,7 @@ module.exports = { | |||
throw new Error(`${node.type} ASTNodes are not handled by markPropTypesAsUsed`); | |||
} | |||
|
|||
const component = components.get(utils.getParentComponent()); | |||
const component = components.get(utils.getParentComponent() || node, true); |
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.
I'm guessing that this would only happen address direct map()
calls. So if you have a nested map, it would still throw a false positive right? eg.
'const Outer = (props) => {',
' let team = props.names.map(() => (',
' ["anotherThing"].map(() => {',
' <Inner innerOne={props.one} innerTwo={props.two} />',
' });',
' ));',
' return <ul>{team}</ul>;',
'};'
@DianaSuvorova had a better fix in #1322 😄 This PR isn't needed anymore. |
Opening this PR for discussion. This PR fixes this false error:
'one' PropType is defined but prop is never used
Caused by this test case:
However, looks like my fix caused a few other tests to fail. I intend to push an update once I do some more investigation.