Skip to content

Commit d2aa260

Browse files
authored
Merge pull request #2115 from drx/class_body_prop_types
Fix propType detection inside class bodies
2 parents 40f2565 + 41974e5 commit d2aa260

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

lib/util/propTypes.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ function iterateProperties(context, properties, fn) {
6868
}
6969

7070
/**
71-
* Checks if a node is inside a class property.
71+
* Checks if a node is inside a class body.
7272
*
7373
* @param {ASTNode} node the AST node being checked.
74-
* @returns {Boolean} True if the node has a ClassProperty ancestor, false if not.
74+
* @returns {Boolean} True if the node has a ClassBody ancestor, false if not.
7575
*/
76-
function isInsideClassProperty(node) {
76+
function isInsideClassBody(node) {
7777
let parent = node.parent;
7878
while (parent) {
79-
if (parent.type === 'ClassProperty') {
79+
if (parent.type === 'ClassBody') {
8080
return true;
8181
}
8282
parent = parent.parent;
@@ -572,7 +572,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
572572
return;
573573
}
574574

575-
if (isInsideClassProperty(node)) {
575+
if (isInsideClassBody(node)) {
576576
return;
577577
}
578578

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2935,7 +2935,7 @@ ruleTester.run('no-unused-prop-types', rule, {
29352935
used: string,
29362936
}
29372937
class Hello extends React.Component<Props> {
2938-
renderHelper = ({unused}: {unused: string}) => {
2938+
renderHelper = ({notAProp}: {notAProp: string}) => {
29392939
return <div />;
29402940
}
29412941
render() {
@@ -2944,6 +2944,37 @@ ruleTester.run('no-unused-prop-types', rule, {
29442944
}
29452945
`,
29462946
parser: 'babel-eslint'
2947+
}, {
2948+
code: `
2949+
type Props = {
2950+
used: string,
2951+
}
2952+
class Hello extends React.Component<Props> {
2953+
componentDidMount() {
2954+
foo(
2955+
({notAProp}: {notAProp: string}) => (<div />)
2956+
);
2957+
}
2958+
render() {
2959+
return <div>{this.props.used}</div>;
2960+
}
2961+
}
2962+
`,
2963+
parser: 'babel-eslint'
2964+
}, {
2965+
code: `
2966+
type Props = {
2967+
used: string,
2968+
}
2969+
class Hello extends React.Component<Props> {
2970+
render() {
2971+
return <QueryRenderer
2972+
render={({notAProp}: {notAProp: string}) => <div>{this.props.used}</div>}
2973+
/>;
2974+
}
2975+
}
2976+
`,
2977+
parser: 'babel-eslint'
29472978
}
29482979
],
29492980

0 commit comments

Comments
 (0)