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 issue with propTypes misclassifying props #2111

Merged
merged 1 commit into from Jan 3, 2019

Conversation

2 participants
@drx
Copy link
Contributor

drx commented Jan 3, 2019

While testing #2110, I found a bug with declaredPropTypes and the no-unused-prop-types rule.

The propTypes module incorrectly detects function parameters inside a class property as props of the class component, which results in this code being considered a warning:

type Props = {
  used: string,
}

class Hello extends React.Component<Props> {
  renderHelper = ({unused}: {unused: string}) => {
    return <div />;
  }
  render() {
    return <div>{this.props.used}</div>;
  }
}

It thinks that unused is an unused prop of Hello.

I'm not a 100% if the fix is the correct fix, but I made markAnnotatedFunctionArgumentsAsDeclared check if the node has a ClassProperty ancestor, and if so, bail. I also added a test to no-unused-prop-types to check for this.

Having these kinds of class properties is arguably an anti-pattern, but it came up as an issue when I was testing legacy code. Worse yet, because declaredPropTypes is an object, those helper properties can overwrite actual props.

@ljharb ljharb force-pushed the drx:prop_types_class_properties branch from 993c150 to ed1c787 Jan 3, 2019

@ljharb

ljharb approved these changes Jan 3, 2019

Copy link
Collaborator

ljharb left a comment

Thanks!

@ljharb ljharb force-pushed the drx:prop_types_class_properties branch from ed1c787 to e15bafa Jan 3, 2019

@ljharb ljharb added the bug label Jan 3, 2019

@ljharb ljharb merged commit b48b479 into yannickcr:master Jan 3, 2019

2 of 3 checks passed

continuous-integration/appveyor/pr Waiting for AppVeyor build to complete
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.005%) to 97.481%
Details

This was referenced Jan 4, 2019

This was referenced Jan 7, 2019

@ibf-renovate ibf-renovate referenced this pull request Jan 12, 2019

Open

Update linters #859

NimaSoroush added a commit to NimaSoroush/differencify that referenced this pull request Jan 17, 2019

Update eslint-plugin-react to the latest version 🚀 (#117)
## The devDependency [eslint-plugin-react](https://github.com/yannickcr/eslint-plugin-react) was updated from `7.12.2` to `7.12.3`.
This version is **not covered** by your **current version range**.

If you don’t accept this pull request, your project will work just like it did before. However, you might be missing out on a bunch of new features, fixes and/or performance improvements from the dependency update.

---

<details>
<summary>Release Notes for v7.12.3</summary>

<h3>Fixed</h3>
<ul>
<li><a href="/yannickcr/eslint-plugin-react/blob/v7.12.3/docs/rules/jsx-indent.md"><code>jsx-indent</code></a>: Prevent crash on valueless props (<a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/issues/2120" data-hovercard-type="pull_request" data-hovercard-url="/yannickcr/eslint-plugin-react/pull/2120/hovercard">#2120</a>, <a class="user-mention" data-hovercard-type="user" data-hovercard-url="/hovercards?user_id=416773" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://urls.greenkeeper.io/jomasti">@jomasti</a>)</li>
<li><a href="/yannickcr/eslint-plugin-react/blob/v7.12.3/docs/rules/jsx-fragments.md"><code>jsx-fragments</code></a>: avoid crashing on self-closing fragments (<a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/issues/2113" data-hovercard-type="pull_request" data-hovercard-url="/yannickcr/eslint-plugin-react/pull/2113/hovercard">#2113</a>, <a class="user-mention" data-hovercard-type="user" data-hovercard-url="/hovercards?user_id=93752" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://urls.greenkeeper.io/alexzherdev">@alexzherdev</a>)</li>
<li><a href="/yannickcr/eslint-plugin-react/blob/v7.12.3/docs/rules/no-unused-prop-types.md"><code>no-unused-prop-types</code></a>: Fix propType detection inside class bodies (<a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/issues/2115" data-hovercard-type="pull_request" data-hovercard-url="/yannickcr/eslint-plugin-react/pull/2115/hovercard">#2115</a>, <a class="user-mention" data-hovercard-type="user" data-hovercard-url="/hovercards?user_id=233040" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://urls.greenkeeper.io/drx">@drx</a>)</li>
<li><a href="/yannickcr/eslint-plugin-react/blob/v7.12.3/docs/rules/no-unused-prop-types.md"><code>no-unused-prop-types</code></a>: fix issue with propTypes misclassifying props (<a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/issues/2111" data-hovercard-type="pull_request" data-hovercard-url="/yannickcr/eslint-plugin-react/pull/2111/hovercard">#2111</a>, <a class="user-mention" data-hovercard-type="user" data-hovercard-url="/hovercards?user_id=233040" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://urls.greenkeeper.io/drx">@drx</a>)</li>
<li><a href="/yannickcr/eslint-plugin-react/blob/v7.12.3/docs/rules/display-name.md"><code>display-name</code></a>: fix false positive for <code>React.memo</code> (<a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/issues/2109" data-hovercard-type="pull_request" data-hovercard-url="/yannickcr/eslint-plugin-react/pull/2109/hovercard">#2109</a>, <a class="user-mention" data-hovercard-type="user" data-hovercard-url="/hovercards?user_id=416773" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://urls.greenkeeper.io/jomasti">@jomasti</a>)</li>
</ul>
<h3>Changed</h3>
<ul>
<li>[Docs] add a missing comma in the JSON settings (<a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/issues/2117" data-hovercard-type="pull_request" data-hovercard-url="/yannickcr/eslint-plugin-react/pull/2117/hovercard">#2117</a>, <a class="user-mention" data-hovercard-type="user" data-hovercard-url="/hovercards?user_id=11673029" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://urls.greenkeeper.io/haideralsh">@haideralsh</a>)</li>
<li>[Docs] update README to document React version detection (<a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/issues/2114" data-hovercard-type="pull_request" data-hovercard-url="/yannickcr/eslint-plugin-react/pull/2114/hovercard">#2114</a>, <a class="user-mention" data-hovercard-type="user" data-hovercard-url="/hovercards?user_id=7688231" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://urls.greenkeeper.io/mohsinulhaq">@mohsinulhaq</a>)</li>
</ul>
</details>

<details>
<summary>Commits</summary>
<p>The new version differs by 14 commits.</p>
<ul>
<li><a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/commit/2f5cec96eca70cfe579038c8b9a81ba5a6a88594"><code>2f5cec9</code></a> <code>Update CHANGELOG and bump version</code></li>
<li><a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/commit/096161a1c69c6d2f6ad0e8e1db7310fdf3061ede"><code>096161a</code></a> <code>Merge pull request #2120 from jomasti/issue-2119</code></li>
<li><a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/commit/9d3955adddc6e922e4a0714f3be0b9d0ba968d42"><code>9d3955a</code></a> <code>Prevent crash of jsx-indent rule</code></li>
<li><a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/commit/230c7e018b876a17336729f0f079f535227e3d0b"><code>230c7e0</code></a> <code>Merge pull request #2117 from haideralsh/patch-1</code></li>
<li><a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/commit/b4b0e7268b84593cbed6bc0ddf242629d0a30f70"><code>b4b0e72</code></a> <code>fix: add a missing in the JSON settings</code></li>
<li><a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/commit/d2aa2602043979dbfa79705dfad5865936f37ccb"><code>d2aa260</code></a> <code>Merge pull request #2115 from drx/class_body_prop_types</code></li>
<li><a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/commit/41974e5a89e527b79e97e097210a001de6e42788"><code>41974e5</code></a> <code>Fix propType detection inside class bodies</code></li>
<li><a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/commit/40f25659b9c801131bb0abed2030fdc54d88a074"><code>40f2565</code></a> <code>[Docs] update README to document React version detection</code></li>
<li><a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/commit/dd0757fbf7ab96770282a2a64c9b696856a47235"><code>dd0757f</code></a> <code>Merge pull request #2113 from alexzherdev/2112-fix-self-closing-fragments</code></li>
<li><a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/commit/e997f6c1bfcf62aee34274364f0766173ffc2829"><code>e997f6c</code></a> <code>[Fix] avoid crashing on self-closing fragments</code></li>
<li><a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/commit/b48b479716d684945d6286fde8e89b0c44653f38"><code>b48b479</code></a> <code>Merge pull request #2111 from drx/prop_types_class_properties</code></li>
<li><a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/commit/e15bafa35ae1d70983551e259879e3858e9e9347"><code>e15bafa</code></a> <code>[Fix] <code>no-unused-prop-types</code>: fix issue with propTypes misclassifying props</code></li>
<li><a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/commit/a86b3391deb53f20d10dc028c7b885f021f014b0"><code>a86b339</code></a> <code>Merge pull request #2109 from jomasti/issue-2105</code></li>
<li><a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/commit/5f9863e0856703c28e1e7cb9b1c78724dc12416b"><code>5f9863e</code></a> <code>Fix display-name false positive for React.memo</code></li>
</ul>
<p>See the <a href="https://urls.greenkeeper.io/yannickcr/eslint-plugin-react/compare/c93cbc60b1847ef09c0b569ec997fb20dfca7c5a...2f5cec96eca70cfe579038c8b9a81ba5a6a88594">full diff</a></p>
</details>

<details>
  <summary>FAQ and help</summary>

  There is a collection of [frequently asked questions](https://greenkeeper.io/faq.html). If those don’t help, you can always [ask the humans behind Greenkeeper](https://github.com/greenkeeperio/greenkeeper/issues/new).
</details>

---


Your [Greenkeeper](https://greenkeeper.io) bot 🌴
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment