Open SoYoung210 opened 4 years ago
eslint ์ค์ ์ผ๋ก ์ฝ๋ฉ Formatting์ ์๋ํ ํ๊ณ , ์ด๋ฅผ ์ ์ฅํ ๋๋ง๋ค ์๋์ผ๋ก ๊ณ ์ณ์ฃผ๋ ์ ํ ์ด๋ค. CRA๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ค์ ํ ๋์๋ dependency๋ค์ด ๊ธฐ๋ณธ์ผ๋ก ์ค์น๋์ด ์๋๋ฐ, CRA์์ด ์ง์ ์ ํ ํ๋ ๊ฒฝ์ฐ์๋ plugin์ด๋ config๋ฅผ ํ๋ํ๋ ์ก์์ฃผ์ด์ผ ํ๋ค.
์ธ ๊ฐ์ง ์ค๋ฅ๋ฅผ ๊ฒช์๊ณ ์ฐจ๊ทผ์ฐจ๊ทผ(..) ํด๊ฒฐํ๋ค.
๋ค์๊ณผ ๊ฐ์ ์ต์ ์์ ์งํํ๋ค.
{ "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์์๊ฐ ์๋์ผ๋ก ์ ๋ ฌ๋๋๋กํ์๋ค.
react-app
prettier
simple-import-sort
๐ CRA์์ lint๋ฅผ ์ก์๋์๋ ์์๋ ์ค์ ์ด ๋ช๊ฐ์ง ์ถ๊ฐ ๋์๋ค.
TS๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ์์ง๋ง๊ณ ๊ผญ ๋ฃ์ด์ฃผ์ด์ผ ์ ์ ์๋ํ๋ค.
@typescript-eslint' ์ค์ ์ด ๋์ด ์์ง ์์์ 3๋ฒ ์ค๋ฅ๋ฅผ ์ฒซ๋ฒ์งธ๋ก ๋ง๋ฌ๊ณ ์ด๋ ๊ฒ ํด๊ฒฐํ๋ค. (์ฌ์ค ์ด ๋จ๊ณ์์๋ ๋ฐ๋ก ๋์ํ์ง ์๊ณ , ๋ช ๊ฐ์ง ๋ ์ค์นํด์ฃผ์ด์ผ ํ๋ค. )
@typescript-eslint'
์๋ง๋ peer dependency๋ก ์ถ์ธก๋๋ ๊ฒ๋ค ๋๋ฌธ์ ํ๋์ ์ ์ ์๋ํ์ง ์์๋ค. ์์ .eslintrc ๋ฅผ ์ ์ ์๋์ํค๊ธฐ ์ํ package.json์ ๋ค์๊ณผ ๊ฐ๋ค
.eslintrc
"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 ์ต์ ์ผ๋ก ๊ฒ์ฌ๋ฅผ ๋๋ ค๋ณด๋ฉด ์ด๋ค ํ๋ฌ๊ทธ์ธ์ด ๋๋ฝ๋์ด ๊ฒ์ฌ๋ฅผ ์๋ฒฝํ ์ํํ ์ ์๋์ง ์๋ ค์ค๋ค.
eslint --debug filename.ts
์ด๋ ๊ฒ workspace root (depth๊ฐ ์์ผ๋ฉด ์๋๋ค.) ์ setting.json ์ ์ถ๊ฐํ๋ค.
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 ๋ ๋์ํ์ง ์๋๋ก ๋ง์์ฃผ์ด์ผ ํ๋ค.
formatOnSave
jbee.io/ormatting-code-automatically/ https://github.com/typescript-eslint/typescript-eslint/issues/641
typescript-eslint/eslint-plugin ์ ์ ์๋ฒ์ ์ ๋ฒ๊ทธ๊ฐ ์๋๊ฒ์ผ๋ก ๋ณด์ธ๋ค. carany ์ต์ ์ผ๋ก alpha ๋ฒ์ ์ ์ค์นํด์ฃผ์๋ค.
typescript-eslint/eslint-plugin
๐ Desc
eslint ์ค์ ์ผ๋ก ์ฝ๋ฉ Formatting์ ์๋ํ ํ๊ณ , ์ด๋ฅผ ์ ์ฅํ ๋๋ง๋ค ์๋์ผ๋ก ๊ณ ์ณ์ฃผ๋ ์ ํ ์ด๋ค. CRA๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ค์ ํ ๋์๋ dependency๋ค์ด ๊ธฐ๋ณธ์ผ๋ก ์ค์น๋์ด ์๋๋ฐ, CRA์์ด ์ง์ ์ ํ ํ๋ ๊ฒฝ์ฐ์๋ plugin์ด๋ config๋ฅผ ํ๋ํ๋ ์ก์์ฃผ์ด์ผ ํ๋ค.
์ธ ๊ฐ์ง ์ค๋ฅ๋ฅผ ๊ฒช์๊ณ ์ฐจ๊ทผ์ฐจ๊ทผ(..) ํด๊ฒฐํ๋ค.
โป๏ธenv
๋ค์๊ณผ ๊ฐ์ ์ต์ ์์ ์งํํ๋ค.
.eslintrc
โ ๏ธTS setting
๐ CRA์์ lint๋ฅผ ์ก์๋์๋ ์์๋ ์ค์ ์ด ๋ช๊ฐ์ง ์ถ๊ฐ ๋์๋ค.
TS๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ์์ง๋ง๊ณ ๊ผญ ๋ฃ์ด์ฃผ์ด์ผ ์ ์ ์๋ํ๋ค.
๐Peer Dependency?
์๋ง๋ peer dependency๋ก ์ถ์ธก๋๋ ๊ฒ๋ค ๋๋ฌธ์ ํ๋์ ์ ์ ์๋ํ์ง ์์๋ค. ์์
.eslintrc
๋ฅผ ์ ์ ์๋์ํค๊ธฐ ์ํ package.json์ ๋ค์๊ณผ ๊ฐ๋ค๐โโ๏ธ Formatting ์๋ํ
์ด๋ ๊ฒ workspace root (depth๊ฐ ์์ผ๋ฉด ์๋๋ค.) ์
setting.json
์ ์ถ๊ฐํ๋ค.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 ๋ฒ์ ์ ์ค์นํด์ฃผ์๋ค.