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

Add linting for React Hooks #186

Closed
kristianmandrup opened this issue Oct 27, 2018 · 15 comments
Closed

Add linting for React Hooks #186

kristianmandrup opened this issue Oct 27, 2018 · 15 comments

Comments

@kristianmandrup
Copy link

Similar to: https://www.npmjs.com/package/eslint-plugin-react-hooks

@Falieson
Copy link

Falieson commented Dec 9, 2018

Some more information about hook rules: https://reactjs.org/docs/hooks-rules.html

@eps1lon
Copy link

eps1lon commented Dec 20, 2018

I took a quick stab at it and while porting AST specific differences is pretty straightforward I hit a wall with codepath analysis. eslint has a built-in API for codepath analysis which is heavily used by eslint-plugin-react-hooks. A port is therefore not that trivial since one would have to analyse the codepath for a given node in tslint.

For now I'm using eslint with typescript-eslint-parser that will only use eslint-plugin-react-hooks. The following eslint config should work for hooks in ts files:

// .eslintrc.js
module.exports = {
  parser: 'typescript-eslint-parser',
  parserOptions: {
    jsx: true,
  },
  plugins: ['react-hooks'],
  rules: {
    'react-hooks/rules-of-hooks': 'error',
  },
};

I'm using this with "lint:hooks": "eslint --no-eslintrc -c \".eslintrc.ts-hooks.js\" \"src/**/*.{ts,tsx}\"",

@Gelio
Copy link

Gelio commented Feb 3, 2019

Take a look at https://github.com/Gelio/tslint-react-hooks

I implemented a TSLint rule for detecting invalid uses of React Hooks, based on eslint-plugin-react-hooks.

It relies on detecting nesting the possible misuses of hooks, like calling hooks inside loops, if statements, class components and functions that themselves are not components or custom hooks.

I would love to receive some feedback 😄 I created some tests for the rule (the test directory in the repository) and it seems to work quite well. Check it out

@rainstormza
Copy link

@Gelio
I've tried your tslint-react-hooks but look like it doesn't work.

npx create-react-app react-hooks --typescript
yarn add tslint-react-hooks --dev
touch tslint.json (and add your config)
yarn start
import React, { useState, useEffect } from 'react'

function Hooks() {
  const [counter, setCounter] = useState(0)
  const array = [1, 2, 3]

  array.forEach(value => {
    useEffect(() => console.log(value))
  })
  return <div>{counter}</div>
}

export default Hooks

It should be error at useEffect but my vscode does not warning anything.

@Gelio
Copy link

Gelio commented Feb 6, 2019

@rainstormza Please try installing tslint alongside tslint-react-hooks 🙂 , As far as I know, it is required for errors to show up and it does not seem to be installed by default via create-react-app --typescript

Having installed tslint I am seeing an error in vscode, as expected:
image

@rainstormza
Copy link

@Gelio
I think install tslint globally is enogh.
I have to install yarn add tslint --dev on local project too.

thanks :)

@thejohnfreeman
Copy link

@Gelio, have you considered opening a PR in this project?

@Gelio
Copy link

Gelio commented Feb 13, 2019

@thejohnfreeman Good idea 👍 I have just filed a PR: #204

@vzgo0022
Copy link

vzgo0022 commented Mar 4, 2019

@Gelio what you mean "try installing tslint alongside tslint-react-hooks"? Can you give me step by step instruction?

@Gelio
Copy link

Gelio commented Mar 4, 2019

@vzgo0022 Just run npm install tslint --save-dev. That should install tslint locally in a project.

Then, take a look at the readme of https://github.com/Gelio/tslint-react-hooks for instructions on how to install and configure the tslint-react-hooks rule

@adidahiya
Copy link
Contributor

adidahiya commented Mar 12, 2019

open to PRs, but I'm also curious to hear about your experiences (if any) using eslint-plugin-react with typescript-eslint, see #210 (edit: while I work on a migration guide...)

@enoh-barbu
Copy link

@Gelio seems that it doesn't work with .tsx files

@Gelio
Copy link

Gelio commented Apr 9, 2019

@enoh-barbu Please file an issue in https://github.com/Gelio/tslint-react-hooks with more information and reproductions steps. Let's not pollute this thread with bug-related comments regarding another project 🙂

@adidahiya
Copy link
Contributor

Closing due to #210, future work should be focused on getting the ESLint plugin working with TypeScript

@jrr
Copy link

jrr commented Aug 21, 2019

For anyone still here, it's time to get off of tslint. I've migrated a couple projects, and eslint-plugin-react-hooks works well with typescript-eslint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants