- Setup eslint
npm init @eslint/config
npm Link - Setup airbnb eslint config
i.
npx install-peerdeps --dev eslint-config-airbnb
ii. Add"extends": ["airbnb", "airbnb/hooks"]
to your.eslintrc
npm Link - Setup airbnb eslint config for typescript
i.
yarn add -D eslint-config-airbnb-typescript @typescript-eslint/eslint-plugin@^5.13.0 @typescript-eslint/parser@^5.0.0
ii. Update.eslintrc
with"extends": ["airbnb", "airbnb/hooks","airbnb-typescript"]
iii. Add parser option:"parserOptions": { "project": "./tsconfig.json" }
npm Link - To run linting:
npx eslint . --ext .js,.jsx,.ts,.tsx
Note: If eslint extension in vs code throws tsconfig.json not found error, then change the
eslint.workingDirectories
setting (File -> Preferences -> Settings -> search "eslint.workingDirectories" and edit with relative path of the tsconfig file with root of workspace)
- Add es-linting for emotion.js.
i.
yarn add -D @emotion/eslint-plugin
ii. Add @emotion plugin in .eslintrc:"plugins": ["@emotion"]
// Can add following rules:
"rules": {
"@emotion/pkg-renaming": "error",
"@emotion/jsx-import": "error",
"@emotion/no-vanilla": "error",
"@emotion/import-from-emotion": "error",
"@emotion/styled-import": "error"
}
> Note: Using `eslint-html-reporter` package for linting errors report.
- Install prettier:
yarn add --dev --exact prettier
- Add .prettierrc.json and add prettier rules in it:
echo {}> .prettierrc.json
- Add prettier commands in
package.json
: Addnpx
if these don't work. For checking errors:"format": "prettier --ignore-path .gitignore --check \"**/*.{js,jsx,ts,tsx,css,scss}\"",
For fixing errors:"format:write": "prettier --ignore-path .gitignore --write \"**/*.{js,jsx,ts,tsx,css,scss}\""
- To avoid conflicts between prettier and eslint, install
eslint-config-prettier
: i. Run command:yarn add --dev eslint-config-prettier
ii. Addeslint-config-prettier
in.eslintrc.json
=>extends
array.
eslint-plugin-prettier
is used to enforce prettier formatting issues as linting errors. This plugin is not recommended.
- Install
eslint-plugin-prettier
:yarn add --dev eslint-plugin-prettier
- Add
eslint-plugin-prettier
in.eslintrc.json
=>plugins
array.
{
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
}
}
More Info: Link
-
Install husky:
yarn add --dev husky
-
Enable Git hooks:
npx husky install
-
Add prepare command in
package.json
:npm pkg set scripts.prepare="cd .. && husky install client/.husky"
-
Run
yarn
. FYI – a .husky folder will appear in the same path as your package.json, in my case under ./client. -
Create a pre-commit hook.
npx husky add .husky/pre-commit "cd client"
Editpre-commit
hook:cd client
yarn lint && yarn format
-
Run:
git add .husky/pre-commit
-
Try to make a commit. If provided commands fail, your commit will be automatically aborted.
-
Add a pre-push hook:
npx husky add .husky/pre-push "cd client"
Editpre-push
hook:cd client
yarn test -- --watchAll=false
-
Run:
git add .husky/pre-push
-
Try to push a commit. If provided commands fail, your push will be automatically aborted.
References:
- For husky: https://scottsauber.com/2021/06/01/using-husky-git-hooks-and-lint-staged-with-nested-folders/
-
Install lint-staged:
yarn add --dev lint-staged
-
Add
.lintstagedrc.json
in root folder add following in it.{ "*.{ts,tsx}": [ "eslint" ], "*.{ts,tsx,js,jsx}": [ "prettier --write" ] }
-
Update
client\.husky\pre-commit
:yarn lint && yarn format
tonpx lint-staged
.