Skip to content

Commit

Permalink
Merge pull request #2115 from drx/class_body_prop_types
Browse files Browse the repository at this point in the history
Fix propType detection inside class bodies
  • Loading branch information
ljharb committed Jan 3, 2019
2 parents 40f2565 + 41974e5 commit d2aa260
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/util/propTypes.js
Expand Up @@ -68,15 +68,15 @@ function iterateProperties(context, properties, fn) {
}

/**
* Checks if a node is inside a class property.
* Checks if a node is inside a class body.
*
* @param {ASTNode} node the AST node being checked.
* @returns {Boolean} True if the node has a ClassProperty ancestor, false if not.
* @returns {Boolean} True if the node has a ClassBody ancestor, false if not.
*/
function isInsideClassProperty(node) {
function isInsideClassBody(node) {
let parent = node.parent;
while (parent) {
if (parent.type === 'ClassProperty') {
if (parent.type === 'ClassBody') {
return true;
}
parent = parent.parent;
Expand Down Expand Up @@ -572,7 +572,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
return;
}

if (isInsideClassProperty(node)) {
if (isInsideClassBody(node)) {
return;
}

Expand Down
33 changes: 32 additions & 1 deletion tests/lib/rules/no-unused-prop-types.js
Expand Up @@ -2935,7 +2935,7 @@ ruleTester.run('no-unused-prop-types', rule, {
used: string,
}
class Hello extends React.Component<Props> {
renderHelper = ({unused}: {unused: string}) => {
renderHelper = ({notAProp}: {notAProp: string}) => {
return <div />;
}
render() {
Expand All @@ -2944,6 +2944,37 @@ ruleTester.run('no-unused-prop-types', rule, {
}
`,
parser: 'babel-eslint'
}, {
code: `
type Props = {
used: string,
}
class Hello extends React.Component<Props> {
componentDidMount() {
foo(
({notAProp}: {notAProp: string}) => (<div />)
);
}
render() {
return <div>{this.props.used}</div>;
}
}
`,
parser: 'babel-eslint'
}, {
code: `
type Props = {
used: string,
}
class Hello extends React.Component<Props> {
render() {
return <QueryRenderer
render={({notAProp}: {notAProp: string}) => <div>{this.props.used}</div>}
/>;
}
}
`,
parser: 'babel-eslint'
}
],

Expand Down

0 comments on commit d2aa260

Please sign in to comment.