ESLint๋
- ์ํค๋ฐฑ๊ณผ
1. ์ผ๊ด๋ ์คํ์ผ ์ ์ง
2. ์๋ชป๋ ์ฝ๋ ์๋ฌ ํ์
3. ๋ค์ํ ํ๋ฌ๊ทธ์ธ ์ง์
์ฝ๋๋ฅผ ์ด์๊ฒ ์ ์งํด์ฃผ๊ณ ์ผ๊ด๋ ์ฝ๋๋ฅผ ์ ์งํ๊ฒ ํด์ค๋๋ค.
Prettier ๋
1. ์ผ๊ด๋ ์คํ์ผ ์ ์ฉ
2. ๊ฐ๋ ์ฑ ์ฆ๊ฐ
3. ๊ฐ๋ฐ์๋ ํฅ์
Install
yarn add -D eslint prettier eslint-plugin-import eslint-plugin-react eslint-plugin-react-hooks eslint-config-prettier eslint-plugin-prettier
๊ฐ ํจํค์ง๋ค์ ์ ์ค์นํด์ผํ๋์ง ์ง์ ์ฐพ์๋ณด์๊ธธ ๋ค ์ด์ ๊ฐ ์์
.eslintrc.json
{
"root": true,
"env": {
"browser": true,
"node": true
},
"parserOptions": {
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 12
},
"plugins": ["import", "react", "react-hooks", "prettier"],
"extends": [
"plugin:import/recommended",
"plugin:react/recommended",
"react-app",
"react-app/jest",
"plugin:prettier/recommended"
],
"rules": {
"react/prop-types": "off",
"react/react-in-jsx-scope": "off",
"no-useless-computed-key": "off",
"react-hooks/exhaustive-deps": "off",
"react-hooks/rules-of-hooks": "error",
"jsx-a11y/anchor-is-valid": "off",
"no-useless-escape": "off",
"no-unused-vars": "off",
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
}
],
"react/display-name": ["off", { "ignoreTranspilerName": false }]
}
}
plugin, extends์ ์๋ prettier์ฌ์ฉ ์ด์ ๋ eslint ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ prettier๊ท์น์ ์ฒดํฌํ์ฌ ์๋ฌํ์๋ฅผ ํ๊ธฐ ์ํด์ ์ ๋๋ค.
"rules"๋ ๊ฐ์ ์์์ ์ค์ ํ๋ฉด ๋ฉ๋๋ค.
plugin: ํด๋น ๋ชจ๋์ ์ฌ์ฉ
extends: eslint ๊ท์น์ ํ์ฅ์ฑ
rules: ๊ท์น์ค์
env: ์ ์ฉํ ํ๊ฒฝ
.prettierrc
{
"tabWidth": 2,
"printWidth": 90,
"arrowParens": "always",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"jsxSingleQuote": false,
"quoteProps": "as-needed",
"semi": true,
"singleQuote": false,
"trailingComma": "es5"
}
package.json
"scripts": {
...
"format": "prettier --check ./src",
"format:fix": "prettier --write ./src",
"lint": "eslint src --ext .js",
"lint:fix": "eslint --fix src --ext .js"
},
yarn format = src์๋ ๋ชจ๋ ํ์ผ์ ๋ํด์ ์ฝ๋๋ฅผ ์์๊ฒ ํ ํ์์ฑ์ด ์๋ ํ์ผ์ ์ฒดํฌํฉ๋๋ค.
yarn format:fix = src์๋ ๋ชจ๋ ํ์ผ์ ๋ํด์ ์ฝ๋๋ฅผ ์์๊ฒ ํ ํ์์ฑ์ด ์๋ ํ์ผ์ ์์ ํฉ๋๋ค.
lint ๋ช ๋ น์ด๋ ๋์ผํด์
๊ทธ๋ฆฌ๊ณ React๋ฅผ ์ฒ์์ ์์ฑํ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก eslint๊ฐ ์ ์ฉ๋์ด์๋ค.
-- package.json
"eslintConfig": {
"extends": [
...
]
}
์ด๋ถ๋ถ์ ํ์๊ฐ ์๋ค. ๋ง์ฝ ์๊ท๋ชจ, ๊ฐ์ธํ๋ก์ ํธ๋ฉด ์์ ์์ฑํด๋ ์๊ด์์ด์.
๊ทธ๋ฐ๋ฐ ๋ญ๋ ๊ฐ์ package์ ์ ๋๊ฑด ๋์ค์ ์ ์ง๋ณด์์ธก๋ฉด์์ ์ข์ง ์์ ๊ฒ ๊ฐ์์.
๊ทธ๋์ ์ ๋ ๊ทธ๋ฅ ์ฒจ๋ถํฐ ์ ๋ถ๋ถ์ ์ญ์ ํด์ ๋ฐ๋ก configํ์ผ์ ์์ฑํด์ ์ฌ์ฉํด์.
VSCode
ํ์ฅ์ ์ค์น
settings.json
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
๊ฐ ์ต์ ๋ค์ ์์ธํ ์ ๋ณด๋ฅผ ์๊ณ ์ถ์ผ๋ฉด
1.eslint rules
https://eslint.org/docs/latest/rules/
Rules - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
eslint.org
2.prettier options
https://prettier.io/docs/en/options.html
Prettier · Opinionated Code Formatter
Opinionated Code Formatter
prettier.io
'IT > Etc' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
gitlab ssh key ๋ฑ๋ก ๋ฐฉ๋ฒ (credential store์ ๋น๊ต) (2) | 2024.09.03 |
---|---|
React + PWA Tutorial (notifications,push) (0) | 2023.06.21 |