๐ŸŒˆESLint์™€ Prettier ์ ์šฉ ๋ฐ ์…‹ํŒ…

Bendesoยท2023๋…„ 9์›” 27์ผ
0
post-thumbnail

โœ‹ Typescript + React ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

//typescript ์‚ฌ์šฉ ์‹œ 
npx create-react-app [ํ”„๋กœ์ ํŠธ๋ช…] --template=typescript

//Typescript๋ฅผ ๋ฏธ์‚ฌ์šฉ
npx create-react-app [ํ”„๋กœ์ ํŠธ ๋ช…]

๐Ÿ˜Ž ESLint

1. ESLint ์„ค์น˜

npm install --save-dev eslint @typescript-eslint/parser@^5.0.0 @typescript-eslint/eslint-plugin@^5.0.0

์ตœ์‹  ๋ฒ„์ „์œผ๋กœ ๋‹ค์šด์„ ๋ฐ›๊ฒŒ ๋˜๋ฉด ์˜์กด์„ฑ ์ถฉ๋Œ์ด ์ผ์–ด๋‚˜ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

2. eslint-config-airbnb ๋กœ ์„ค์น˜ํ•˜๊ธฐ

npm info "eslint-config-airbnb@latest" peerDependencies๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์„ค์น˜ํ•ด์•ผํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋“ค์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.
๊ฐ์ž ์„ค์น˜๋ฅผ ํ•ด์ฃผ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
๋งŒ์•ฝ npm์ด v5์ด์ƒ ์ด๋ฉด ๋‹ค์Œ ์ปค๋งจํŠธ๋ฅผ ์ž…๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

npx install-peerdeps --dev eslint-config-airbnb

์ž๋™์œผ๋กœ ํ”Œ๋Ÿฌ๊ทธ์ธ ๋ฐ ์„ค์ •๋“ค์„ ์„ค์น˜ํ•ด์ค๋‹ˆ๋‹ค.

3. .eslintrc.json ํŒŒ์ผ ์ƒ์„ฑ

npx eslint --init

์œ„ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„ํŽธํ•˜๊ฒŒ eslintrc.json ํŒŒ์ผ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

package.json ํŒŒ์ผ์˜ script ๋‚ด ์š”์†Œ ์ถ”๊ฐ€

"lint": "eslint \"./src/**/*.{ts,tsx,js,jsx}\"",
"lint:fix": "eslint --fix \"./src/**/*.{ts,tsx,js,jsx}\""

์„ค์ •ํ•œ lint ์‹คํ–‰ ์‹œ eslint ๋ฌธ๋ฒ•์— ์–ด๊ธ‹๋‚œ ์ฝ”๋“œ๋ฅผ ์•Œ๋ ค์ค€๋‹ค.

4. eslint-config-airbnb-typescript

npm i --save-dev eslint-config-airbnb-typescript

Typescript๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ eslint-config-airbnb-typescript ๋ฅผ ์„ค์น˜ํ•œ ํ›„ .eslintrc.js์˜ extends ๋‚ด airbnb, airbnb-typescript๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

5. .eslintrc.json ํŒŒ์ผ ์…‹ํŒ…

{
  "env": {
    "browser": true,
    "es2021": true,
    "node": true
  },
  "extends": [
  	"eslint:recommended",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    "airbnb",
    "airbnb/hooks",
    "airbnb-typescript"
   ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": "latest",
    "sourceType": "module",
    "project": "./tsconfig.json"
  },
  "plugins": ["react", "@typescript-eslint"],
  "rules": {}
}

npm run lint ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด๋ณด๋ฉด airbnb ๊ฐ€ ์ ์šฉ๋œ๋‹ค.

๐Ÿ˜™ Prettier

1. Prettier ์„ค์น˜

npm install --save-dev prettier eslint-config-prettier eslint-plugin-prettier

2. .prettierrc.json ํŒŒ์ผ ์…‹ํŒ…

prettier ํŒŒ์ผ์„ ์…‹ํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

{
  "singleQuote": true,
  "semi": true,
  "useTabs": false,
  "tabWidth": 2,
  "trailingComma": "all",
  "printWidth": 120
}

.eslintrc.jsonํŒŒ์ผ๋„ ์ˆ˜์ •ํ•ด์ค๋‹ˆ๋‹ค.

"extends": [
	"eslint:recommended",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    "airbnb",
    "airbnb/hooks",
    "airbnb-typescript",
    "prettier"
 ]

package.jsonํŒŒ์ผ์˜ scripts์— prettier๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

"scripts": {
  //...
    "prettier": "prettier --write --config ./.prettierrc './src/**/*.{ts,tsx}'",
  //...
}
profile
์„ฑ์žฅ์„ ์œ„ํ•œ ๋ชธ๋ถ€๋ฆผ

0๊ฐœ์˜ ๋Œ“๊ธ€