Skip to content

Commit 2348bf4

Browse files
bensteppljharb
authored andcommitted
fix no-unused-prop-types + async class properties and methods
There was an inconsistency with access and storage of the usedPropTypes of a component that would cause propTypes to not be correctly marked as used if more than one prop was used in the body of an async function class property or async class method. Fixes #1053 --- bug source: First time around - get the parentComponent using utils.getParentComponent() https://github.com/yannickcr/eslint-plugin-react/blob/master/lib/rules/no-unused-prop-types.js#L594 - save off the usedPropTypes array from what component was found. - modify the array with my new used props. - set the new array on the node https://github.com/yannickcr/eslint-plugin-react/blob/master/lib/rules/no-unused-prop-types.js#L638 Note that in this case the node is a MemberExpression - Components#set will then just crawl the parents of the current node (the MemberExpresion) until one was found in the internal this._list. - Because all async FunctionExpressions, ArrowFunctionExpressions, and FunctionDeclaration are treated as components of confidence 0 (not a component). The unusedPropTypes would be attached to this. (Which is usually fine). However, if the component tries to mark a prop as used again, it will read from the parentComponent using utils.getParentComponent() (which in this case will return the class) which does not have the previously used methods. The array would then be modified (created because it doesnt exist), and then set them onto the async arrow function overwriting anything that was previously there. This change just attaches used props to the found component (where the previous usedPropTypes are pulled from) if it exists, otherwise to the current node. --- Squashed: Added test cases for factory methods on classes that return async functions. Added test cases using the default eslint parser and defining an async function in the render method.
1 parent ab7bd25 commit 2348bf4

File tree

2 files changed

+392
-1
lines changed

2 files changed

+392
-1
lines changed

lib/rules/no-unused-prop-types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ module.exports = {
661661
break;
662662
}
663663

664-
components.set(node, {
664+
components.set(component ? component.node : node, {
665665
usedPropTypes: usedPropTypes,
666666
ignorePropsValidation: ignorePropsValidation
667667
});

0 commit comments

Comments
 (0)