SoYoung210 / soso-tip

๐Ÿฏ์†Œ์†Œํ•œ ํŒ๋“ค๊ณผ ์ •๋ฆฌ, ๋ฒ„๊ทธ ํ•ด๊ฒฐ๊ธฐ๋ฅผ ๋ชจ์•„๋‘๋Š” ๋ ˆํฌ
24 stars 0 forks source link

[eslint] setting eslint and autoFix on VSCode (no CRA) #30

Open SoYoung210 opened 4 years ago

SoYoung210 commented 4 years ago

๐Ÿ“˜ Desc

eslint ์„ค์ •์œผ๋กœ ์ฝ”๋”ฉ Formatting์„ ์ž๋™ํ™” ํ•˜๊ณ , ์ด๋ฅผ ์ €์žฅํ• ๋•Œ๋งˆ๋‹ค ์ž๋™์œผ๋กœ ๊ณ ์ณ์ฃผ๋Š” ์…‹ํŒ…์ด๋‹ค. CRA๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์„ค์ •ํ• ๋•Œ์—๋Š” dependency๋“ค์ด ๊ธฐ๋ณธ์œผ๋กœ ์„ค์น˜๋˜์–ด ์žˆ๋Š”๋ฐ, CRA์—†์ด ์ง์ ‘ ์…‹ํŒ…ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” plugin์ด๋‚˜ config๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์žก์•„์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์„ธ ๊ฐ€์ง€ ์˜ค๋ฅ˜๋ฅผ ๊ฒช์—ˆ๊ณ  ์ฐจ๊ทผ์ฐจ๊ทผ(..) ํ•ด๊ฒฐํ–ˆ๋‹ค.

  1. @typescript-eslint/no-angle-bracket-type-assertion' was not found.
  2. ESLint couldn't find the plugin "eslint-plugin-flowtype"
  3. Failed to load plugin '@typescript-eslint' declared in '.eslintrc': Cannot find module 'typescript' Referenced from ...

โ™ป๏ธenv

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ต์…˜์—์„œ ์ง„ํ–‰ํ–ˆ๋‹ค.

.eslintrc

{
  "parser": "@typescript-eslint/parser",
  "extends": ["plugin:@typescript-eslint/recommended","react-app", "prettier"],
  "env": {
    "browser": true,
    "commonjs": true,
    "node": true,
    "es6": true
  },
  "parserOptions": {
    "ecmaVersion": 2018
  },
  "plugins": ["@typescript-eslint", "react-hooks", "simple-import-sort"],
  "rules": {
    "react-hooks/rules-of-hooks": "error",
    "padding-line-between-statements": [
      "error",
      { "blankLine": "always", "prev": "*", "next": "return" }
    ],
    "max-depth": ["error", { "max": 3 }],
    "no-unneeded-ternary": ["error", { "defaultAssignment": false }],
    "simple-import-sort/sort": "error",
    "sort-imports": "off",
    "import/order": "off",
    "semi": 2,
    "@typescript-eslint/no-angle-bracket-type-assertion": "off"
  }
}

๊ธฐ๋ณธ์ ์œผ๋กœ react-app ๊ณผ prettier ๋ฅผ extends ํ•˜๋„๋ก ์„ค์ •ํ–ˆ๊ณ , ์ถ”๊ฐ€์ ์œผ๋กœ simple-import-sort ๋ฅผ ์ด์šฉํ•ด์„œ import์ˆœ์„œ๊ฐ€ ์ž๋™์œผ๋กœ ์ •๋ ฌ๋˜๋„๋กํ•˜์˜€๋‹ค.

โš ๏ธTS setting

๐Ÿ“ CRA์—์„œ lint๋ฅผ ์žก์„๋•Œ์—๋Š” ์—†์—ˆ๋˜ ์„ค์ •์ด ๋ช‡๊ฐ€์ง€ ์ถ”๊ฐ€ ๋˜์—ˆ๋‹ค.

TS๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์žŠ์ง€๋ง๊ณ  ๊ผญ ๋„ฃ์–ด์ฃผ์–ด์•ผ ์ •์ƒ ์ž‘๋™ํ•œ๋‹ค.

@typescript-eslint' ์„ค์ •์ด ๋˜์–ด ์žˆ์ง€ ์•Š์•„์„œ 3๋ฒˆ ์˜ค๋ฅ˜๋ฅผ ์ฒซ๋ฒˆ์งธ๋กœ ๋งŒ๋‚ฌ๊ณ  ์ด๋ ‡๊ฒŒ ํ•ด๊ฒฐํ–ˆ๋‹ค. (์‚ฌ์‹ค ์ด ๋‹จ๊ณ„์—์„œ๋Š” ๋ฐ”๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๊ณ , ๋ช‡ ๊ฐ€์ง€ ๋” ์„ค์น˜ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. )

๐Ÿ”—Peer Dependency?

์•„๋งˆ๋„ peer dependency๋กœ ์ถ”์ธก๋˜๋Š” ๊ฒƒ๋“ค ๋•Œ๋ฌธ์— ํ•œ๋™์•ˆ ์ •์ƒ ์ž‘๋™ํ•˜์ง€ ์•Š์•˜๋‹ค. ์œ„์˜ .eslintrc ๋ฅผ ์ •์ƒ ์ž‘๋™์‹œํ‚ค๊ธฐ ์œ„ํ•œ package.json์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

  "devDependencies": {
    "eslint": "6.0.1",
    "eslint-config-prettier": "6.2.0",
    "eslint-config-react-app": "5.0.1",
    "eslint-plugin-flowtype": "4.3.0",
    "eslint-plugin-import": "2.18.0",
    "eslint-plugin-jsx-a11y": "6.2.3",
    "eslint-plugin-react": "7.14.3",
    "eslint-plugin-react-hooks": "2.0.1",
    "eslint-plugin-simple-import-sort": "4.0.0",
    "@typescript-eslint/eslint-plugin": "2.2.1-alpha.8",
    "@typescript-eslint/parser": "2.2.0",
    "prettier": "1.18.2"
  },

TIP ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•„์š”ํ• ๊ฒƒ ๊ฐ™์•„ ์–น์€๊ฒƒ์€ ์•„๋‹ˆ๊ณ , eslint --debug filename.ts ์˜ต์…˜์œผ๋กœ ๊ฒ€์‚ฌ๋ฅผ ๋Œ๋ ค๋ณด๋ฉด ์–ด๋–ค ํ”Œ๋Ÿฌ๊ทธ์ธ์ด ๋ˆ„๋ฝ๋˜์–ด ๊ฒ€์‚ฌ๋ฅผ ์™„๋ฒฝํžˆ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†๋Š”์ง€ ์•Œ๋ ค์ค€๋‹ค.

๐Ÿƒโ€โ™€๏ธ Formatting ์ž๋™ํ™”

image

์ด๋ ‡๊ฒŒ workspace root (depth๊ฐ€ ์žˆ์œผ๋ฉด ์•ˆ๋œ๋‹ค.) ์— setting.json ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

{
  "editor.formatOnSave": false,
  "javascript.format.enable": false,
  "eslint.alwaysShowStatus": true,
  "eslint.options": {
    "extensions": [".html", ".ts", ".js", ".tsx"]
  },
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    {
      "language": "typescript",
      "autoFix": true
    },
    {
      "language": "typescriptreact",
      "autoFix": true
    }
  ],
  "eslint.autoFixOnSave": true,
}

prettier๋Š” eslint์—์„œ extendsํ•ด์ฃผ์—ˆ์œผ๋ฏ€๋กœ formatOnSave ๋Š” ๋™์ž‘ํ•˜์ง€ ์•Š๋„๋ก ๋ง‰์•„์ฃผ์–ด์•ผ ํ•œ๋‹ค.

Refer

jbee.io/ormatting-code-automatically/ https://github.com/typescript-eslint/typescript-eslint/issues/641

Plus..

typescript-eslint/eslint-plugin ์˜ ์ •์‹๋ฒ„์ „์— ๋ฒ„๊ทธ๊ฐ€ ์žˆ๋Š”๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค. carany ์˜ต์…˜์œผ๋กœ alpha ๋ฒ„์ „์„ ์„ค์น˜ํ•ด์ฃผ์—ˆ๋‹ค.