-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Extract used propTypes detection #1946
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
Extract used propTypes detection #1946
Conversation
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.
LGTM overall
lib/util/usedPropTypes.js
Outdated
'Program:exit': function() { | ||
const list = components.list(); | ||
|
||
for (const component in list) { |
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.
for..in
should be avoided; can this use normal array iteration methods?
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.
Sure. Object.keys(list).forEach
perhaps?
There's about a dozen other files already doing the same, handle them all?
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.
yes, that would be great!
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.
already done in b35ea0c 😄
35f717c
to
014e9d2
Compare
I went ahead and fixed other |
bdd9d22
to
b35ea0c
Compare
lib/rules/boolean-prop-naming.js
Outdated
@@ -248,7 +247,7 @@ module.exports = { | |||
} | |||
} | |||
|
|||
if (!has(list, component) || (list[component].invalidProps || []).length) { | |||
if ((list[component].invalidProps || []).length) { |
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.
maybe if (list[component].invalidProps && list[component].invalidProps.length > 0)
?
lib/rules/display-name.js
Outdated
continue; | ||
Object.keys(list).forEach(component => { | ||
if (list[component].hasDisplayName) { | ||
return; |
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.
Object.keys(list).filter(component => !list[component].hasDisplayName).forEach
?
lib/rules/no-multi-comp.js
Outdated
for (const component in list) { | ||
if (!has(list, component) || isIgnored(list[component]) || ++i === 1) { | ||
continue; | ||
Object.keys(list).forEach(component => { |
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.
(component, i)
and then we don't need to ++
it, nor have let i = 0
above.
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.
Can't do this because we only want to ++i
if isIgnored
is false and doesn't short-circuit. I.e. we want to start reporting from the second non-ignored component.
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.
Actually found a way to achieve this, also using filter
.
lib/rules/no-set-state.js
Outdated
for (const component in list) { | ||
if (!has(list, component) || isValid(list[component])) { | ||
continue; | ||
Object.keys(list).forEach(component => { |
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.
Object.keys(list).filter(component => !isValid(list[component])).forEach
?
lib/rules/no-unused-prop-types.js
Outdated
for (const component in list) { | ||
if (!has(list, component) || !mustBeValidated(list[component])) { | ||
continue; | ||
Object.keys(list).forEach(component => { |
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.
Object.keys(list).filter(component => mustBeValidated(list[component])).forEach
?
lib/util/usedPropTypes.js
Outdated
|
||
if (LIFE_CYCLE_METHODS.indexOf(name) >= 0) { | ||
return true; | ||
} else if (checkAsyncSafeLifeCycles && ASYNC_SAFE_LIFE_CYCLE_METHODS.indexOf(name) >= 0) { |
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.
no need for the else
lib/util/usedPropTypes.js
Outdated
|
||
if (node.kind === 'constructor') { | ||
return true; | ||
} else if (LIFE_CYCLE_METHODS.indexOf(nodeKeyName) >= 0) { |
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.
also here (ie, the no-else-return
eslint 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.
yeah, I wonder why this isn't warning
lib/util/usedPropTypes.js
Outdated
switch (type) { | ||
case 'direct': | ||
// Ignore Object methods | ||
if (Object.prototype[name]) { |
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.
name in Object.prototype
would be better, so as not to trigger getters
lib/util/usedPropTypes.js
Outdated
// method -- mark it for used props. | ||
if (isNodeALifeCycleMethod(node.parent.parent)) { | ||
node.properties.forEach((property, i) => { | ||
if (i === 0) { |
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 is just:
if (node.properties.length > 0) {
markPropTypesAsUsed(node.parent);
}
the forEach seems useless?
lib/util/usedPropTypes.js
Outdated
'Program:exit': function() { | ||
const list = components.list(); | ||
|
||
Object.keys(list).forEach(component => { |
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.
Object.keys(list).filter(component => mustBeValidated(list[component])).forEach
?
b35ea0c
to
5678833
Compare
5678833
to
fd03bd7
Compare
I believe this change caused #2017. The old logic was: function isPropTypesUsage(node) {
const isClassUsage = (
(utils.getParentES6Component() || utils.getParentES5Component()) &&
node.object.type === 'ThisExpression' && node.property.name === 'props'
);
const isStatelessFunctionUsage = node.object.name === 'props' && !isAssignmentToProp(node);
const isNextPropsUsage = node.object.name === 'nextProps' && (inComponentWillReceiveProps() || inShouldComponentUpdate());
return isClassUsage || isStatelessFunctionUsage || isNextPropsUsage;
} and it got replaced with: function isPropTypesUsage(node) {
const isClassUsage = (
(utils.getParentES6Component() || utils.getParentES5Component()) &&
((node.object.type === 'ThisExpression' && node.property.name === 'props')
|| isPropArgumentInSetStateUpdater(node))
);
const isStatelessFunctionUsage = node.object.name === 'props' && !isAssignmentToProp(node);
return isClassUsage || isStatelessFunctionUsage || inLifeCycleMethod();
} |
Pushing for visibility.This is failing one existing test, which will get fixed after #1939 is merged (also will need a rebase).
Resolves #963, resolves #1871.