์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

[React] ESLint์™€ Prettier ์„ค์ •ํ•˜๊ธฐ (++TypeScript)

๐Ÿฉท ๋ฏผ์˜ 2023. 12. 3. 13:30

์žŠ๊ธฐ์ „์— ๊ธฐ๋กํ•˜๊ธฐ

TypeScript ๊ธฐ๋ฐ˜ React์—์„œ ESLint์™€ Prettier ์„ค์ •๋ฒ•



  1. ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ
    ( ์ง„ํ–‰ํ•˜๋˜ ํ”„๋กœ์ ํŠธ์—์„œ ์„ค์ •ํ•  ๊ฒฝ์šฐ ์ถฉ๋Œ์ด ๋‚  ์ˆ˜ ์žˆ์œผ๋‹ˆ ํ”„๋กœ์ ํŠธ ์‹œ์ž‘์ „! ์„ค์ •ํ•˜์ž)
npx create-react-app my-app --template typescript




2. ESLint์™€ Prettier ์„ค์น˜

npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-config-prettier eslint-plugin-prettier prettier




3. ESLint ์„ค์ •


ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ ํด๋”์— .eslintrc.js๋ฅผ ์ƒ์„ฑ ํ›„ ๋‹ค์Œ ๋‚ด์šฉ ์ถ”๊ฐ€

module.exports = {
  parser: '@typescript-eslint/parser',
  extends: [
    'plugin:@typescript-eslint/recommended',
    'prettier',
  ],
  plugins: ['@typescript-eslint', 'prettier'],
  rules: {
    'prettier/prettier': 'error',
  },
};




4. Prettier ์„ค์ •


ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ ํด๋”์— .prettierrc ์ƒ์„ฑ ํ›„ ๋‹ค์Œ ๋‚ด์šฉ ์ถ”๊ฐ€

{
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "all",
  "printWidth": 80
}



++ ํŒจํ‚ค์ง€ ์˜์กด์„ฑ ๋ฒ„์ „ ์ถฉ๋Œ ํ•ด๊ฒฐ


์—๋Ÿฌ ๋ฉ”์„ธ์ง€
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve


๋“ฑ๋“ฑ,,,

npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-config-prettier eslint-plugin-prettier prettier --legacy-peer-deps

๋ฆฌ์•กํŠธ ํ”„๋กœ์ ํŠธ ์…‹ํŒ…์‹œ์—๋Š” ์ตœ์‹  ๋ฒ„์ „์ด ์„ค์น˜๋˜์–ด์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํ˜ธํ™˜์ด ์•ˆ ๋ ๋•Œ๊ฐ€ ์žˆ๋‹ค. --force ๋˜๋Š” --legacy-peer-deps : ๋ฅผ ์‚ฌ์šฉ ํ•ด์ฃผ๋ฉด ํ•ด๊ฒฐ์ด ๋œ๋‹ค.

  1. Delete โeslintprettier/prettier ์˜ค๋ฅ˜ ํ•ด๊ฒฐ
    โ ๋ฌธ์ž๋Š” ์ค„ ๋ฐ”๊ฟˆ ๋ฌธ์ž์˜ ํŠน์ˆ˜ํ•œ ํ˜•ํƒœ ์ค‘ ํ•˜๋‚˜์ด๋ฏ€๋กœ ๊ฐ„๋‹จํ•œ ์„ค์ •์„ ํ†ตํ•ด ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋‹ค.
    .eslintrc.js ํŒŒ์ผ ์ˆ˜์ •

module.exports = {
  parser: '@typescript-eslint/parser',
  extends: ['plugin:@typescript-eslint/recommended', 'prettier'],
  plugins: ['@typescript-eslint', 'prettier'],
  rules: {
    'prettier/prettier': [
      'error',
      {
        endOfLine: 'auto',
      },
    ],
  },
}

ESLint์„ค์ •ํ™” Pretteir ์„ค์ •์— ๋Œ€ํ•œ ๋งŽ์€ ๊ธ€์ด ๋‚˜์™€ ์žˆ๋Š”๋ฐ ๋„ˆ๋ฌด ๋งŽ์€ ์ •๋ณด๊ฐ€ ์žˆ์–ด ์ฒซ ์„ค์ • ๋‹น์‹œ ๋”์šฑ ํž˜๋“ค์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค

๋” ์ด์ƒ ๋ฐฉํ™ฉํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ด ์—†๊ธฐ๋ฅผ !