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] no-unused-prop-types: false positive with callback #2375

Merged
merged 1 commit into from Sep 6, 2019

Conversation

golopot
Copy link
Contributor

@golopot golopot commented Aug 6, 2019

Fixes #2350

This pr fixes getParentStatelessComponent when the current traversed node is like a callback (is a function and is an argument). The function getParentStatelessComponent is revised to use a more whitelist-ish approach.

Fixes false positive like:

function Foo(props) {
  useEffect(() => {
    const { a } = props;
    document.title = a;
  });
  
  return <p/>;
}
  Foo.propTypes = {
  a: PropTypes.string,
}

tests/lib/rules/no-this-in-sfc.js Show resolved Hide resolved
case 'AssignmentExpression':
case 'Property':
case 'ReturnStatement':
case 'ExportDefaultDeclaration': {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These cases are

const Foo = () => <p />;

module.exports = function Foo() {};

module.exports = {
  Foo() {},
};

function hof() {
  return function Foo() {};
}

export default function Foo() {}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add explicit test cases for each of them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the tests cases for them are scattered in several rules, do you want a set of test cases centralized in, say, tests/rules/prop-types.js?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it'd probably be ideal for every rule that uses component detection to have all of these cases, tbh

@golopot
Copy link
Contributor Author

golopot commented Aug 19, 2019

The tests are broken by the merge of #2184, which added a test case: https://github.com/yannickcr/eslint-plugin-react/blob/7bc55ccb6ed2c32d381f09d2f43a77ca60afaae3/tests/lib/rules/no-multi-comp.js#L345-L349

Do we want to support components defined in comma operator? (IMO, no.)

@ljharb
Copy link
Member

ljharb commented Aug 31, 2019

Yes, we should support components in any expression position.

@golopot golopot force-pushed the issue-2350 branch 3 times, most recently from 73bb36d to 47e8640 Compare September 3, 2019 18:14
lib/util/Components.js Outdated Show resolved Hide resolved
lib/util/Components.js Outdated Show resolved Hide resolved
lib/util/Components.js Outdated Show resolved Hide resolved
lib/util/Components.js Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

react/no-unused-prop-types false positive in useEffect with destructuring
2 participants