20
loading...
This website collects cookies to deliver better user experience
npm init -y
//package.json
...
"name": "eslint-config-ramonak"
...
npx eslint --init
@typescript-eslint/eslint-plugin
, @typescript-eslint/parser
, eslint
, eslint-plugin-react
packages as dev dependencies in the project (in your specific use case packages might be different, depending on your answers). And also, it will add .eslintrc file with the basic config.npm i -D prettier eslint-config-prettier eslint-plugin-prettier
plugin:prettier/recommended
as the last extension in your .eslintrc
file:{
"extends": [
///other plugins
...
"plugin:prettier/recommended"
]
}
{
"extends": ["prettier"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error",
"arrow-body-style": "off",
"prefer-arrow-callback": "off"
}
}
As this blog post's final goal is to create an NPM package, I'll add a prettier config directly into the eslint config without making a separate .prettierrc file. But this might conflict with text editor extensions (like prettier-vscode, for example), as they read directly from .prettierrc file. Possible solutions might be:
1.Create .prettierrc file in your project and copy the configuration from .eslintrc file there.
2.Uninstall (if already installed) a prettier extension and config your text editor to use ESLint extension only for code formatting. More on that here.
//.eslintrc
...
"rules": {
"prettier/prettier": [
"error",
{
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 100,
"semi": true
}
]
}
npm i -D eslint-plugin-react-hooks eslint-plugin-simple-import-sort
{
"extends": [
// ...
"plugin:react-hooks/recommended"
],
"plugins": ["simple-import-sort"], //other plugins omitted
"rules": {
"simple-import-sort/imports": "error",
"simple-import-sort/exports": "error"
//other rules omitted
}
}
index.js
file with the following content:const eslintrc = require("./.eslintrc.json");
module.exports = eslintrc;
package.json
using the peerDependencies
field. So just copy all dev dependencies in the package.json
file to peerDependencies
field://package.json
...
"peerDependencies": {
"@typescript-eslint/eslint-plugin": "^5.5.0",
"@typescript-eslint/parser": "^5.5.0",
"eslint": "^8.4.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.27.1",
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"prettier": "^2.5.1"
}
npm link
npm link <name of your eslint-config>
npm link eslint-config-ramonak
packages.json
file after running that command. But your eslint-config will be added into node_modules
folder.npm i -D @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-config-prettier eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-simple-import-sort prettier
package.json
add or edit (if already exists) eslintConfig
field:
"eslintConfig": {
"extends": [
"ramonak" //your eslint-config module name
]
}
.eslintrc
file with the following content:
{
"extends": ["ramonak"] //your eslint-config module name
}
npx eslint .
npx eslint . --fix
npm publish
Uninstall or disable any previously installed prettier extensions.
Install (if haven't already) ESLint extension
Edit VSCode settings by pressing CMD + SHIFT + P on Mac (or Ctrl + SHIFT + P on Windows), type settings
and choose Preferences: Open Settings (JSON)
. Edit or add the following settings:
// Format a file on save
"editor.formatOnSave": true,
// show eslint icon at bottom toolbar
"eslint.alwaysShowStatus": true,
// turns on Auto Fix for all providers including ESLint
"editor.codeActionsOnSave": {
"source.fixAll": true
}
"editor.defaultFormatter": "esbenp.prettier-vscode"
line if you had it before.npx mrm@2 lint-staged
package.json
:
"scripts": {
//other scripts omitted
"lint:fix": "eslint . --fix" //add linting script
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": "npm run lint:fix" //run linting script only on JS and TypeScript files
}