@frontendfixer/eslint-config-react

2.0.3 • Public • Published

Eslint Prettier TypeScript Setup from @frontendfixer

Eslint Prettier TypeScript Setup for React from @frontendfixer

Table of contents


Features

  • Lints JavaScript and TypeScript based on the latest standards
  • Fixes issues and formatting errors with Prettier
  • Lints + Fixes inside of html script tags
  • Lints + Fixes React via eslint-config-airbnb
  • Lints + Fixes Typescript via airbnb/typescript
  • Lints + Fixes imports according to standard rules

You can see all the base rules and typescript rule

  • You are very welcome to overwrite any of these settings, or just fork the entire thing to create your own.

Installation

  • without typescript
npm install -D eslint prettier@^2.8.8 @frontendfixer/eslint-config-react
  • with typescript
npm install -D eslint prettier@^2.8.8 typescript @frontendfixer/eslint-config-react

Configuration

We need to put our eslint settings in a file in the root of our project. We should create a new .eslintrc or .eslintrc.js file that lives where package.json does:

React config

You can add it in package.json, anywhere top level. Like right under your "scripts" object.

"eslintConfig": {
  "extends": ["@frontendfixer/react"]
}

Or put this in a .eslintrc file

{
  "extends": ["@frontendfixer/react"]
}

Typescript config

{
  "extends": ["@frontendfixer/react"],
  "overrides": [
    {
      "files": ["**/*.{ts,tsx}"],
      "extends": ["@frontendfixer/react/typescript"]
    }
  ]
}

TypeScript users will also need a tsconfig.json file in their project. An empty object ({}) will do!

Add script

You can add two scripts to your package.json to lint and/or fix:

"scripts": {
  "lint": "eslint . --ext .js,.jsx,.ts,.tsx",
  "lint:fix": "eslint --fix . --ext .js,.jsx,.ts,.tsx"
},
  • Now you can manually lint your code by running npm run lint and fix all fixable issues with npm run lint:fix. You probably want your editor to do this though.

Settings

EsLint Rules

If you'd like to overwrite eslint or prettier settings, you can add the rules in your .eslintrc file. The ESLint rules go directly under "rules".

{
  "extends": [
    "@frontendfixer/react"
  ],
  "rules": {
    "no-console": "error",
  }
}
// You can also use {0, 1, 2} for {"off", "warn", "error"}

Prettier Rules

By default these rules are enable for linting

{
  semi: true,
  singleQuote: true,
}

If you want custom prettier options, it's recommended to create a .prettierrc file in your root directory like so:

{
  "trailingComma": "es5",
  "printWidth": 80,
  "quoteProps": "as-needed",
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "bracketSameLine": true,
  "jsxBracketSameLine": true,
  "jsxSingleQuote": false
}
  • this .prettierrc file is present in config if you want different rules then create a new config

Note if you are switching to double quotes, you'll also need to add this eslint rule, or they will fight to the death!

quotes: ['error', 'double'];

Auto fix lint error with VSCode

You should read this entire thing. Serious!

Once you have done one, or both, of the above installs. You probably want your editor to lint and fix for you. Here are the instructions for VS Code:

  1. Install the ESLint package
  2. Now we need to setup some VS Code settings via Code/FilePreferencesSettings. It's easier to enter these settings while editing the settings.json file, so click the Open (Open Settings) icon in the top right corner:
// These are all my auto-save configs
"editor.formatOnSave": true,
// turn it off for JS and JSX, we will do this via eslint
"[javascript][javascriptreact][typescript][typescriptreact]": {
  "editor.formatOnSave": false
},
// tell the ESLint plugin to run on save
"editor.codeActionsOnSave": {
  "source.fixAll.eslint": true
},

After attempting to lint your file for the first time, you may need to click on 'ESLint' in the bottom right and select 'Allow Everywhere' in the alert window.

Finally you'll usually need to restart VS code. They say you don't need to, but it's never worked for me until I restart.


With Create React App

  1. Run npx install-peerdeps --dev @frontendfixer/eslint-config-react
  2. Crack open your package.json and replace "extends": "react-app" with "extends": "@frontendfixer/react"

With Gatsby

  1. Run npx install-peerdeps --dev @frontendfixer/eslint-config-react
  2. follow the Local / Per Project Install steps above

Inspirations

  1. eslint-config-standard
  2. eslint-config-wesbos

Package Sidebar

Install

npm i @frontendfixer/eslint-config-react

Weekly Downloads

6

Version

2.0.3

License

MIT

Unpacked Size

16.1 kB

Total Files

11

Last publish

Collaborators

  • frontendfixer