Skip to content
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 propType detection inside class bodies #2115

Merged
merged 1 commit into from Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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