๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT/Etc

ESLint + Prettier + React ํ™˜๊ฒฝ ์„ค์ •

by Full~ day ๐Ÿ˜€ 2023. 1. 11.
728x90
๋ฐ˜์‘ํ˜•

 

 

ESLint๋ž€

Javascript ์ฝ”๋“œ์—์„œ ๋ฐœ๊ฒฌ๋˜๋Š” ๋ฌธ์ œ์‹œ ๋˜๋Š” ํŒจํ„ด๋“ค์„ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ ์ •์ ์ฝ”๋“œ ๋ถ„์„ ๋„๊ตฌ์ด๋‹ค.
- ์œ„ํ‚ค๋ฐฑ๊ณผ

1. ์ผ๊ด€๋œ ์Šคํƒ€์ผ ์œ ์ง€
2. ์ž˜๋ชป๋œ ์ฝ”๋“œ ์—๋Ÿฌ ํ‘œ์‹œ
3. ๋‹ค์–‘ํ•œ ํ”Œ๋Ÿฌ๊ทธ์ธ ์ง€์›

์ฝ”๋“œ๋ฅผ ์ด์˜๊ฒŒ ์œ ์ง€ํ•ด์ฃผ๊ณ  ์ผ๊ด€๋œ ์ฝ”๋“œ๋ฅผ ์œ ์ง€ํ•˜๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

 

Prettier ๋ž€

Code Formatter์ด๋‹ค. ๊ฐœ๋ฐœ์ž๋งˆ๋‹ค ๋‹ค๋ฅธ ๋ฐฉ์‹์˜ ์ฝ”๋”ฉ์„ ์ผ๊ด€๋œ ๋ฐฉ์‹์œผ๋กœ ์Šคํƒ€์ผ์„ ์œ ์ง€ํ• ์ˆ˜ ์žˆ๊ฒŒ ํˆด์ด๋‹ค.

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

 

728x90
๋ฐ˜์‘ํ˜•
LIST

'IT > Etc' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

gitlab ssh key ๋“ฑ๋ก ๋ฐฉ๋ฒ• (credential store์™€ ๋น„๊ต)  (2) 2024.09.03
React + PWA Tutorial (notifications,push)  (0) 2023.06.21