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

[eslint-config-react-app] Recommended way of config integration with ESLint, TypeScript, Prettier and VS Code #8849

Open
bennettdams opened this issue Apr 17, 2020 · 17 comments

Comments

@bennettdams
Copy link

bennettdams commented Apr 17, 2020

People use VS Code a lot. It is also recommended in the docs, but it is not explained on how to configure it together with ESLint, Prettier & TypeScript together with eslint-config-react-app.

Every two months there is a new Medium article for this topic, as the configuration/dependencies always change. Wouldn't it be nice if CRA had an official documentation on how to integrate all these with a step-by-step tutorial on the CRA website?

Describe the solution you'd like

I would like the docs to provide all the steps needed to integrate eslint-config-react-app and the additional features in their development setup.

Two major parts:

  • What do I need to configure manually in the files of my CRA project? (e. g. .eslintrc.js)
    Do I need additional dependencies? How to integrate other ESLint plugins?
  • What do I need to configure manually in my VS Code setup? (e. g. for the ESLint plugin (dbaeumer.vscode-eslint or the Prettier plugin)

Possible combinations that are most common:

No. CRA dependencies eslint-config-react-app Prettier TypeScript
1 x x
2 x x x
3 x x x x

For ESLint and Prettier, there should be an explanation not only how to combine the configurations, but also on how to actually use the configuration in VS Code. Also, when using Prettier, you need to make sure that its rules don't conflict with ESLint rules.

The following is the configuration I'm running with right now for ESLint + TypeScript + Prettier (thanks, @robertcoopercode):

module.exports = {
  parser: "@typescript-eslint/parser",
  extends: [
    "react-app", // Uses the recommended rules Create React App
    "plugin:@typescript-eslint/recommended", // Uses the recommended rules from @typescript-eslint/eslint-plugin
    "prettier/@typescript-eslint", // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
    "plugin:prettier/recommended" // Should be last in the list. Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
  ],
  parserOptions: {
    ecmaFeatures: {
      jsx: true // Allows for the parsing of JSX
    },
    ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
    sourceType: "module" // Allows for the use of imports
  },
  rules: {},
  settings: {
    react: {
      version: "detect" // Tells eslint-plugin-react to automatically detect the version of React to use
    }
  }
};

It's a hassle to keep all the configuration up to date and that, in my opinion, is what CRA should try to solve.

Describe alternatives you've considered

A possible alternative to requiring manual changes in the config files would be a CLI tool that automatically creates/modifies them. An explanation on how to integrate with VS Code would still be needed on the website though.

UPDATE

I decided to create a website on my own, feel free to check it out and contribute:

Website: https://eslintconfig.dev
Repo: https://github.com/bennettdams/eslintconfig.dev

@KenCoder
Copy link

This is an excellent proposal. I just spent several hours trying to get the ESLint warnings displayed in VSCode to match those reported by CRA, and failed. As bennetdams said, the problem is that there are too many suggestions, and given how much CRA hides everything is voodoo for the average user.

@lmcarreiro
Copy link

@KenCoder I just spent several hours trying to get this to work too... Are you trying to use custom eslintrc config too?

Didn't this simple eslintrc config on your workspace worked?

{
  extends: ["react-app"]
}

@KenCoder
Copy link

I tried that and it didn't work. I am not trying to use custom eslintrc although I did create that file to try to solve my problems.

My ESLint output in VSCode displays the following error over and over again, and no amount of "yarn adding" I have tried solved it, nor has various incantations of eslintrc I found, including the one you mention.

Failed to load plugin 'import' declared in 'client/package.json » eslint-config-react-app': Cannot find module 'eslint-plugin-import'

@tobiaskraus
Copy link

tobiaskraus commented May 28, 2020

I also have spent hours without success in trying to make my custom .eslintrc.js (with @typescript-eslint/recomended, other plugins and own rules) work with npm run start (react-scripts start).

I added EXTEND_ESLINT=true to .env file, disabled the webpack cache in node_modules/react-scripts/config/webpack.config.js (see issue #9007), and removed "eslintConfig": { "extends": "react-app" }, from package.json (also tried it with it).

But there are still errors shown like @typescript-eslint/explicit-function-return-type which are not part of my config (not part of plugin:@typescript-eslint/recommended), and these errors/warnings don't show up when running my eslint src/**/*.{ts,tsx} which should use the same settings and therefore I expect the same result.

Somehow it seems, that react-scripts modifies my eslint settings (doesn't ignore them completely), as changing rules in rules array reflect in the yarn start output but there are more rules applied than the plugins / rule sets I extend from + my own rules.

@tobiaskraus
Copy link

I found my issue: webpack uses react-scripts own version of eslint plugins which was outdated and applied therefore different recommended rules (which i extended from).

In #9083 I explained this issue in details and proposed to give the option, to use own eslint plugins (or own versions).

This proves again the point of @bennettdams

I totally agree, that this whole topic is super complicated to research (so many articles and ways to implement) and many possible mistakes you can do. Therefore, a better documentation from create-react-app would help.

@bennettdams bennettdams changed the title Recommended way of usage for eslint-config-react-app with ESLint, TypeScript, Prettier and VS Code [eslint-config-react-app] Recommended way of config usage with ESLint, TypeScript, Prettier and VS Code Jun 8, 2020
@bennettdams bennettdams changed the title [eslint-config-react-app] Recommended way of config usage with ESLint, TypeScript, Prettier and VS Code [eslint-config-react-app] Recommended way of config integration with ESLint, TypeScript, Prettier and VS Code Jun 8, 2020
@Svish
Copy link

Svish commented Jul 14, 2020

May I suggest, if this is done, to base the examples on the simpler .eslintrc.json, rather than .js with module.exports and stuff?

When I started out in "the linting world", not too long ago, not too familiar with node and old (not ES6) module stuff, examples using .js added some unnecessary noise, in my opinion.

@anthonyalayo
Copy link
Contributor

Every 6 months to a year I need to search "medium eslint create react app" and some other tags to find the latest config. This would be amazing.

@bennettdams
Copy link
Author

bennettdams commented Aug 30, 2020

Hey!
We're now ~5 months in, so I decided to create a website on my own:

https://eslintconfig.dev/

It should be a place for curated collections of ESLint configurations for all the different templates (Create React App, Next.js, Vue, ...) and setups (TypeScript, Prettier, ... or combinations of those).

I would love contributions for other setups, versions or updates, so I don't have to read another Medium article again 😅

https://github.com/bennettdams/eslintconfig.dev

@tonystaark
Copy link

@bennettdams Damn the world owes u a Noble Prize..

@tonystaark
Copy link

@bennettdams your create-react-app with typescript and prettier using eslint@^7.0.0 which contradicts with the default dependecy requirement from CRA which is eslint@^6.6.0

@bennettdams
Copy link
Author

bennettdams commented Sep 16, 2020

@bennettdams your create-react-app with typescript and prettier using eslint@^7.0.0 which contradicts with the default dependecy requirement from CRA which is eslint@^6.6.0

The same config should also work for ESLint v6.6.0.
Let us use the repository for future questions to avoid spamming this CRA issue.

Btw: CRA v4 is right around the corner, which also comes with ESLint v7.

@toadeelali
Copy link

Hey!
We're now ~5 months in, so I decided to create a website on my own:

https://eslintconfig.dev/

It should be a place for curated collections of ESLint configurations for all the different templates (Create React App, Next.js, Vue, ...) and setups (TypeScript, Prettier, ... or combinations of those).

I would love contributions for other setups, versions or updates, so I don't have to read another Medium article again 😅

https://github.com/bennettdams/eslintconfig.dev

config for js + eslint + prettier not working

kwajiehao pushed a commit to isomerpages/isomercms-frontend that referenced this issue Mar 15, 2021
In recent commits, we upgraded our react-scripts version from 3.4.4
to 4.0.3. This is because CRA (create-react-app) v3 uses an outdated
version of eslint (facebook/create-react-app#8849). This introduced
a bug related to the css-loader library, which can no longer resolve
assets in the public folder:
- facebook/create-react-app#9870 (comment)
- webpack-contrib/css-loader#1136 (comment)

This commit fixes this bug by moving the referenced image to the
relevant sub-directory in the src directory.
@federicoruf
Copy link

Try to generate anything with this tool but seems to not be working
https://eslintconfig.dev/

@rickstaa
Copy link

@federicoruf Your right everything except the TypeScript & Prettier configuration is throwing a 404 error. This is a known issue since all the other configurations are not yet implemented (see bennettdams/eslintconfig.dev#4). You can create a feature request or a pull request for the configuration you need. What configuration are you looking for?

@federicoruf
Copy link

@federicoruf Your right everything except the TypeScript & Prettier configuration is throwing a 404 error. This is a known issue since all the other configurations are not yet implemented (see bennettdams/eslintconfig.dev#4). You can create a feature request or a pull request for the configuration you need. What configuration are you looking for?

Hi @rickstaa , thanks for yout replay. Sorry, I didn't see that thread with the error I was mentioning. Actually I'm creating a new React project and I wanted to set the ground first for let's say stilyng the code with ESlint and Prettier, and I was curious about your tool. It's great, don't worry I actually manage to integrate ESlint using the cmd.

Thanks!

@rickstaa
Copy link

@federicoruf All of the credit for creating this very useful tool goes to @bennettdams. I'm just a user like you whole stumbled upon your comment and out of curiosity went on to investigate what was going wrong.

prestonlimlianjie pushed a commit to isomerpages/isomercms-frontend that referenced this issue Apr 22, 2021
In recent commits, we upgraded our react-scripts version from 3.4.4
to 4.0.3. This is because CRA (create-react-app) v3 uses an outdated
version of eslint (facebook/create-react-app#8849). This introduced
a bug related to the css-loader library, which can no longer resolve
assets in the public folder:
- facebook/create-react-app#9870 (comment)
- webpack-contrib/css-loader#1136 (comment)

This commit fixes this bug by moving the referenced image to the
relevant sub-directory in the src directory.
prestonlimlianjie added a commit to isomerpages/isomercms-frontend that referenced this issue Apr 22, 2021
* fix: outdated packages with vulnerabilities

* feat: install eslint and initiate config

* feat: install prettier and set prettier options

* feat: install eslint-config-prettier

* feat: install eslint-plugin-prettier

* chore: reformat eslint config

* feat: add @trivago/prettier-plugin-sort-imports, define preferred import order

* fix: css-loader file resolution bug introduced by CRA v4

In recent commits, we upgraded our react-scripts version from 3.4.4
to 4.0.3. This is because CRA (create-react-app) v3 uses an outdated
version of eslint (facebook/create-react-app#8849). This introduced
a bug related to the css-loader library, which can no longer resolve
assets in the public folder:
- facebook/create-react-app#9870 (comment)
- webpack-contrib/css-loader#1136 (comment)

This commit fixes this bug by moving the referenced image to the
relevant sub-directory in the src directory.

* chore: temporarily disable eslint

* chore: add more files and folders to .prettierignore

* chore: upgrade prettier-plugin-sort-imports to 2.0.2

fixes trivago/prettier-plugin-sort-imports#22

* chore: temporarily disable prettier

* chore: remove prettier config temporarily

* chore: remove jsx-a11y references temporarily

* temporarily remove import/prefer-default-export reference

Co-authored-by: jiehao <jiehao@open.gov.sg>
Co-authored-by: Preston Lim <prestonlimlianjie@gmail.com>
@0xdevalias
Copy link

0xdevalias commented Apr 28, 2021

In case it's useful to others, I was having issues after upgrading from create-react-app 3:

⇒  npx eslint --print-config src/index.tsx

Oops! Something went wrong! :(

ESLint: 7.25.0

ESLint couldn't find the config "react-app" to extend from. Please check that the name of the config is correct.

The config "react-app" was referenced from the config file in "/Users/devalias/dev/sparkle/sparkle/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

After a lot of digging/messing around, I found that using npm dedup seemed to resolve the issue for me:

deleting my lock file (package-lock.json) and then npm i solved my issue.

@InSuperposition Thanks for this! It helped me narrow down the issue, and while deleting our package-lock.json wasn't something we were willing to do, I found that by using npm dedup it also seemed to solve the issue for us (see sparkletown/sparkle#1102 (comment)):

Originally posted by @0xdevalias in #9083 (comment)

alexanderleegs pushed a commit to isomerpages/isomercms-frontend that referenced this issue Apr 29, 2021
* fix: outdated packages with vulnerabilities

* feat: install eslint and initiate config

* feat: install prettier and set prettier options

* feat: install eslint-config-prettier

* feat: install eslint-plugin-prettier

* chore: reformat eslint config

* feat: add @trivago/prettier-plugin-sort-imports, define preferred import order

* fix: css-loader file resolution bug introduced by CRA v4

In recent commits, we upgraded our react-scripts version from 3.4.4
to 4.0.3. This is because CRA (create-react-app) v3 uses an outdated
version of eslint (facebook/create-react-app#8849). This introduced
a bug related to the css-loader library, which can no longer resolve
assets in the public folder:
- facebook/create-react-app#9870 (comment)
- webpack-contrib/css-loader#1136 (comment)

This commit fixes this bug by moving the referenced image to the
relevant sub-directory in the src directory.

* chore: temporarily disable eslint

* chore: add more files and folders to .prettierignore

* chore: upgrade prettier-plugin-sort-imports to 2.0.2

fixes trivago/prettier-plugin-sort-imports#22

* chore: temporarily disable prettier

* chore: remove prettier config temporarily

* chore: remove jsx-a11y references temporarily

* temporarily remove import/prefer-default-export reference

Co-authored-by: jiehao <jiehao@open.gov.sg>
Co-authored-by: Preston Lim <prestonlimlianjie@gmail.com>
gweiying added a commit to isomerpages/isomercms-frontend that referenced this issue Apr 30, 2021
* Add linting and formatting tools (#378)

* fix: outdated packages with vulnerabilities

* feat: install eslint and initiate config

* feat: install prettier and set prettier options

* feat: install eslint-config-prettier

* feat: install eslint-plugin-prettier

* chore: reformat eslint config

* feat: add @trivago/prettier-plugin-sort-imports, define preferred import order

* fix: css-loader file resolution bug introduced by CRA v4

In recent commits, we upgraded our react-scripts version from 3.4.4
to 4.0.3. This is because CRA (create-react-app) v3 uses an outdated
version of eslint (facebook/create-react-app#8849). This introduced
a bug related to the css-loader library, which can no longer resolve
assets in the public folder:
- facebook/create-react-app#9870 (comment)
- webpack-contrib/css-loader#1136 (comment)

This commit fixes this bug by moving the referenced image to the
relevant sub-directory in the src directory.

* chore: temporarily disable eslint

* chore: add more files and folders to .prettierignore

* chore: upgrade prettier-plugin-sort-imports to 2.0.2

fixes trivago/prettier-plugin-sort-imports#22

* chore: temporarily disable prettier

* chore: remove prettier config temporarily

* chore: remove jsx-a11y references temporarily

* temporarily remove import/prefer-default-export reference

Co-authored-by: jiehao <jiehao@open.gov.sg>
Co-authored-by: Preston Lim <prestonlimlianjie@gmail.com>

* Fix/resource color (#430)

* fix: resource page header changed to bg-secondary

* fix: using isResourcePage to determine page header

* Fix/rearrange layout (#427)

* fix: simplify directoryFile utils for retrieve and update

* fix: update methods using directoryFile utils

* fix: introduce FolderReorderingModal

* fix: refactors FolderContent

* fix: update params for FolderContentItem

* fix: fix breadcrumb display

* fix: add propTypes for FolderReorderingModal

* fix: add Cancel button to FolderReorderingModal

* fix: updates draggable-id for React dnd

* fix: updates dropdown button behavior for reordering

* fix: updates copy text

* fix: updates copy text

* fix: updates variable naming for directory file output

* refactor: clean up ProtectedRoute and LoginContext (#431)

* refactor: clean up ProtectedRoute and LoginContext

* refactor: make LoginContext testable

create 3 exports: LoginContext itself, LoginProvider, LoginConsumer

* refactor: make route selector testable in App.jsx

* refactor: group routing components

* feat: add basic routing tests

* refactor: move __tests__ folder to correct place

Required for jest to find the test files

* style: delete unnecessary div

* refactor: make exports more obvious for LoginContext.js

* refactor: delete duplicate route

* chore: add rest of routing tests

* style: remove unused declarations

* Fix/fine-tune react-query settings (#389)

* fix: turn off refetching on window focus for useQuery

This commit sets the `refetchOnWindowFocus` flag for the useQuery
on the following layouts/components to be `false`:
- PageSettingsModal
- EditNavBar
- EditPage

The reason we turn off this flag is because these pages involve user
changes - with this flag on, any unsaved changes by the user would
be overwritten by the refetched data.

This commit also sets the
- `refetchOnReconnect`
- `refetchInterval`
- `refetchIntervalInBackground`
flags to be false for the same reasons explained above.

* fix: invalidate queries after mutation

This commit adds cache invalidation of all our GET queries
(the useQuery invocations) after we perform a mutation. This
allows us to reset our cache in a more granular manner, as opposed
to simply setting the cache time for our GET queries to be 0.

Additionally, this commit also adds a PAGE_SETTINGS_KEY constant
to replace the string literal that was being used as the query key
for the PageSettingsModal.

Refer to the following documentation for more details:
- https://react-query.tanstack.com/guides/query-invalidation
- https://react-query.tanstack.com/guides/invalidations-from-mutations

* feat: standardize output of GET API calls to return resp.data

This commit standardizes the output of our GET API functions to
return resp.data instead of just the response from the api call.
resp.data is a better choice as we are able to use that in
dependency arrays, whereas the resp object pointer always changes,
which will trigger the useEffect every time even if the object data
hasn't actually changed.

* fix: load yaml content when reading directory file

* feat: disable useQuery if component tracks local state

As per our discussion (refer to meeting minutes here:
https://docs.google.com/document/d/1br6T6wVX0KrcA3nwQEo7OhUrcT4veLnaz0vByEXjVvo/edit#heading=h.hyx8t36v9z3n)
we will be discussing our `useQuery` functions if there are changes
to the local state that are being tracked, to avoid refetching
behavior from overwriting local changes.

* fix: rebase errors

* fix: invocation of LoginContext

* Fix: rebase errors

* Fix: update resource category and resource page get calls to return data directly

* Refactor: use invalidateQueries instead of passing refetch function for CollectionPagesSection

* Fix: error when retrieving page settings

* Fix: rebase errors

* Fix: invalidate query instead of reload when changing page settings

* Feat: add success toast when changing settings

* Fix: change folder naming to use invalidation instead of reload

* Fix: invalidate correct resource folder key

* Fix: update success toast messages

* Fix: remove log statement

* Fix: add successtoast

Co-authored-by: Alexander Lee <alexander@open.gov.sg>

* fix: pass parameters to wrapped components (#439)

* fix: fixes toast popup on item select, folder deletion modal (#440)

* fix: fixes toast popup on item select, folder deletion modal

* fix: add condition for folder deletion

Co-authored-by: kwajiehao <31984694+kwajiehao@users.noreply.github.com>
Co-authored-by: jiehao <jiehao@open.gov.sg>
Co-authored-by: Preston Lim <prestonlimlianjie@gmail.com>
Co-authored-by: Alexander Lee <alexander@open.gov.sg>
Co-authored-by: Alexander Lee <alexleegs@gmail.com>
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