Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

react-unused-props-and-state doesn't understand usage in setState #796

Closed
imsolost opened this issue Jan 24, 2019 · 3 comments
Closed

react-unused-props-and-state doesn't understand usage in setState #796

imsolost opened this issue Jan 24, 2019 · 3 comments
Labels
Difficulty: Medium People with non-trivial experience in TSLint should be able to send a pull request for this issue. 💀 R.I.P. 💀 Type: Bug

Comments

@imsolost
Copy link

Bug Report

  • tslint-microsoft-contrib version: 6.0.0
  • TSLint version: 5.12.1
  • TypeScript version: 3.2.4
  • Running TSLint via: CLI

TypeScript code being linted

https://github.com/imsolost/test-app/blob/master/src/App.tsx

interface IProps {
  readonly firstProp: boolean
  readonly secondProp: boolean
  readonly thirdProp: boolean
}

// ...

class App extends React.Component<
  IProps,
  IState
> {

  // ...

  public async componentWillMount() {
    this.setState((_state, props) => ({
      message: 'test message',
      firstProp: props.firstProp,
      secondProp: props.secondProp,
      thirdProp: props.thirdProp,
    }))
  }
}

with tslint.json configuration: https://github.com/imsolost/test-app/blob/master/tslint.json

{
  "rulesDirectory": [
    "node_modules/tslint-react/rules",
    "node_modules/tslint-microsoft-contrib/"
  ],
  "rules": {  
    "react-unused-props-and-state": [
      true,
      {
        "props-interface-regex": "Props$",
        "state-interface-regex": "State$"
      }
    ]
  }
}

Actual behavior

The props defined in the interface are flagged as unused by tslint with the following errors:

Unused React property defined in interface: firstProp (react-unused-props-and-state)
Unused React property defined in interface: secondProp (react-unused-props-and-state)
Unused React property defined in interface: thirdProp (react-unused-props-and-state)

Expected behavior

These properties are used in the setState function so they should not trigger this error.

@JoshuaKGoldberg
Copy link

Great catch! The (_state, props) updater passed to setState is a valid way to access props, but the rule doesn't seem to know that. Accepting PRs to fix that bug.

@JoshuaKGoldberg JoshuaKGoldberg added Type: Bug Status: Accepting PRs Difficulty: Medium People with non-trivial experience in TSLint should be able to send a pull request for this issue. labels Jan 26, 2019
@shiftkey
Copy link
Contributor

I've got my eye on addressing this one after #824 lands, now that I know more about how the rule works.

@JoshuaKGoldberg
Copy link

💀 It's time! 💀

TSLint is deprecated and no longer accepting pull requests other than security fixes. See #876. ☠️
We recommend you instead use typescript-eslint to lint your TypeScript code with ESLint. ✅

👋 It was a pleasure open sourcing with you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Difficulty: Medium People with non-trivial experience in TSLint should be able to send a pull request for this issue. 💀 R.I.P. 💀 Type: Bug
Projects
None yet
Development

No branches or pull requests

3 participants