ESLint, Prettier, and Husky

Code formatting with Eslint, Prettier, and Husky

A person fixing a wooden puzzle
A person fixing a wooden puzzle

ESLint

To Set up ESLint

  1. Install ESLint from scratch for a React Typescript App.
    N/B: If you are using Create-React-App, it comes with preinstalled ESLint so you would not need to do this.
yarn add eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react —-dev

or

npm install eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react —-dev
{
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
],
"env": {
"es6": true,
"browser": true,
"jest": true,
"node": true
},
"settings": {
"react": {
"version": "detect"
}
},
"rules": {
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/indent": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/no-use-before-define": 0,
"@typescript-eslint/explicit-module-boundary-types": 0,
"no-console": [
2,
{
"allow": ["warn", "error"]
}
],
"@typescript-eslint/ban-ts-comment": 0,
"@typescript-eslint/no-empty-function": 0
}
//eslintignore file
build
node_modules
coverage

Prettier

To set up Prettier

  1. To install prettier, run the code below
yarn add prettier eslint-config-prettier --dev
or
npm install prettier eslint-config-prettier --dev
{
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"semi": false,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"arrowParens": "always"
}
"extends": [
...
"prettier"
]
build 
node_modules
coverage

Run on CLI

“scripts”: {
...
"lint": "eslint '*/**/*.{js,ts,tsx}' --quiet --fix"
"format": "prettier --write .",
}
Yarn Lint output
yarn lint
Yarn Format output
yarn format

Run On Save

  • For Prettier: Search for “format on save”. Check the “ format on save” option
formatting prettier on Save
formatting prettier on Save
  • For ESLint: Search for “Code Actions On Save”. Click “Edit in settings.json” to add “source.fixAll.eslint” : true to the “editor.codeActionsOnSave”.
Formatting eslint on Save
Formatting eslint on Save file

Pre-commit Hooks (Husky)

To set pre-commit Hooks

npm install — save-dev husky lint-staged
or
yarn add --dev husky lint-staged
npx husky install
npm set-script prepare "husky install"
npx husky add .husky/pre-commit “npx lint-staged”
"devDependencies":{
...
}
"lint-staged": {
"*.{ts,tsx,js}": "yarn lint",
"*.css": "yarn format"
}
git commit output for pre-commit hooks using husky
git commit output for pre-commit hooks using husky
npm set-script type-check "tsc --noEmit"npx husky add .husky/pre-push "npm run type-check"

Conclusion

Useful resources

--

--

Software developer. Imaginative. I love to try,. I write to relearn while sharing the knowledge.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sandygoodnews

Software developer. Imaginative. I love to try,. I write to relearn while sharing the knowledge.