- 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.
- 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
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:
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"]
}
{
"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!
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 withnpm run lint:fix
. You probably want your editor to do this though.
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"}
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'];
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:
- Install the ESLint package
- Now we need to setup some VS Code settings via
Code/File
→Preferences
→Settings
. It's easier to enter these settings while editing thesettings.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.
- Run
npx install-peerdeps --dev @frontendfixer/eslint-config-react
- Crack open your
package.json
and replace"extends": "react-app"
with"extends": "@frontendfixer/react"
- Run
npx install-peerdeps --dev @frontendfixer/eslint-config-react
- follow the Local / Per Project Install steps above