Current Size:
Chakra BreakPoints
base0px
sm480px
md768px
lg992px
xl1280px
2xl1536px
Current Height:Width
widthpx
heightpx

Subjects

Eslint and Prettier General Reference Config

Date Created: 2020/10/21

Last Update: 2022/12/14

#dev-env #notes

CheatSheet

Check lint

TIMING=1 npm run lint

Support Absolute Paths

.eslint.json
{
//...
"settings": {
"import/resolver": {
"node": {
"paths": ["src"]
}
}
},
//...
}

Disable and Ignore Eslint

File Level

/* eslint-disable */

disable by rule on file

/* eslint no-use-before-define: 0 */ // --> OFF
or
/* eslint no-use-before-define: 2 */ // --> ON

Ignore next line

/* eslint-disable no-eval */
Put /* eslint-disable-line */ at the end of the line(s),
or /* eslint-disable-next-line */ right before the line.

Setting Up ESLint and Prettier

Machine Level

🗒️ Installing at the machine level is a matter of prefference

brew install eslint
brew install prettier

Project Level

VSCode Prep

".vscode/setting.json"
{
//....
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
//....
}

To have this Eslint and Prettier handle all languages

".vscode/setting.json"
{
//....
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
//....
}

or configure by language

".vscode/setting.json"
{
//....
"[javascript]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
},
//....
}

Node/JavaScript Project Setup

npm install --save-dev eslint-config-prettier eslint-plugin-prettier prettier
touch .prettierrc
npx eslint --init

Eslint Config Example

".eslint.json"
// .eslint.json
{
"env": {
"browser": true,
"es2021": true
},
"extends": ["prettier", "eslint:recommended", "plugin:react/recommended"],
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": ["prettier", "react"],
"rules": {
"prettier/prettier": ["error"]
}
}

Prettier Config Example

".prettierrc.json"
// .prettierrc.json
{
"arrowParens": "avoid",
"semi": false,
"singleQuote": true,
"printWidth": 70
}

Turning Rules off

".eslint.json"
{
// ...
"rules": {
"prefer-const": "off" // Turn rule off
}
}

TypeScript Project Setup

npm i --dev @typescript-eslint/eslint-plugin
".eslint.json"
{
"plugins": ["@typescript-eslint"],
"extends": [
"next/core-web-vitals",
"plugin:@typescript-eslint/recommended"
],
"rules": {
// I suggest you add those two rules:
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-explicit-any": "error"
}
}

References

Prettier & Eslint - Rw;eruch

More Notes

All Notes
HomeProjects

Links

Home Articles Notes Projects About Style Guide Site Credits

Contact

 [email protected]

Location

🌎 Earth