Notice
Recent Posts
Recent Comments
Link
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ ์ปจ๋ฒค์…˜] ESLint, Prettier, airbnb style guide๋กœ ์„ค์ •ํ•˜๊ธฐ ๋ณธ๋ฌธ

๐Ÿง‘‍๐Ÿ’ป Web/JavaScript

[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ ์ปจ๋ฒค์…˜] ESLint, Prettier, airbnb style guide๋กœ ์„ค์ •ํ•˜๊ธฐ

Lennon 2021. 12. 8. 21:22
728x90
๋ฐ˜์‘ํ˜•

ESLint

 

๊ฐ„๋‹จํžˆ ๋งํ•˜์ž๋ฉด ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•ด ์—๋Ÿฌ ๋ฐ ๋ฒ„๊ทธ๋ฅผ ๋ณด๊ณ ํ•ด์ฃผ๋Š” ๋„๊ตฌ์ด๋‹ค.

 

Prettier

 

๋ณธ๋ž˜ ์Šคํƒ€์ผ ๋Œ€์‹  ๊ทœ์น™์„ ์ •ํ•ด ๊ทœ์น™๋Œ€๋กœ ๋‹ค์‹œ ์ž‘์„ฑํ•ด ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

 

์ข‹์€ ์ฝ”๋“œ ์ปจ๋ฒค์…˜์„ ์ง€ํ‚ค๊ธฐ์œ„ํ•ด ํ•œ ๋ฒˆ ์ ์šฉํ•ด๋ณด์ž.

 

 

1. ์šฐ์„  vscode์— ํ™•์žฅ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ESLint, Prettier ๋‘ ๊ฐœ๋ฅผ ์„ค์น˜ํ•˜์ž.

 

 

 

2. ํ„ฐ๋ฏธ๋„์—์„œ ๋ณธ์ธ์˜ ํ”„๋กœ์ ํŠธ์— ๋“ค์–ด๊ฐ€ eslint์™€ prettier์„ ์„ค์น˜ํ•˜์ž.

 

2-1. eslint ์„ค์น˜

$ npm install eslint --save-dev

 

2-2. prettier ์„ค์น˜

$ npm install prettier --save-dev --save-exact

 

2-3. eslint์™€ prettier์˜ ์ถฉ๋Œ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์„ค์น˜

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

 

2-4. airbnb style ์„ค์น˜

$ npm install eslint-config-airbnb --save-dev

 

ํ”„๋กœ์ ํŠธ ํด๋”์˜ package.jsonํŒŒ์ผ์˜ "devDependencies"๋ฅผ ํ™•์ธํ•ด๋ณด์ž.

 

์ด๋ ‡๊ฒŒ ๋˜์–ด ์žˆ๋‹ค๋ฉด ์„ค์ •์„ ์ž˜ ๋งˆ๋ฌด๋ฆฌ ํ•œ ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ ์ ˆ์ฐจ๋ฅผ ๋ฐŸ์•„๋ณด์ž.

 

๋งŒ์•ฝ package.json์ด ์—†๋‹ค๋ฉด 

 

ํ„ฐ๋ฏธ๋„์— npm init์„ ์ž‘์„ฑํ•ด ํ”„๋กœ์ ํŠธ ํด๋”์— ์ƒ์„ฑํ•ด์ค€๋‹ค.

 

3. vscode ์„ธํŒ…

 

setting.json์„ ์ˆ˜์ •ํ•ด์•ผํ•œ๋‹ค. ํ•œ ๋ฒˆ ๋“ค์–ด๊ฐ€๋ณด์ž.

 

Windows: Ctrl + ,  ์„ธ ํ‚ค๋ฅผ ํ•จ๊ป˜ ๋ˆ„๋ฅธ๋‹ค.

Mac: Cmd + , ์„ธ ํ‚ค๋ฅผ ํ•จ๊ป˜ ๋ˆ„๋ฅธ๋‹ค.

 

๊ทธ๋Ÿฌ๋ฉด ํ•ด๋‹น ํ™”๋ฉด์ด ๋œฐ ๊ฒƒ์ด๋‹ค. ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ setting.json์— ์ ‘๊ทผํ•˜์ž.

 

setting.json์— ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์ž.

{
    // Set the default
    "editor.formatOnSave": false,
    // Enable per-language
    "[javascript]": {
      "editor.formatOnSave": true
    },
    "editor.codeActionsOnSave": {
      // For ESLint
      "source.fixAll.eslint": true
    }
  }

 

vscode ํ”„๋กœ์ ํŠธ ํด๋” ์ตœ์ƒ๋‹จ์— .eslintrc.json ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ํ›„ ํ•ด๋‹น ์ฝ”๋“œ์„ ์ž…๋ ฅํ•˜์ž

{
  "env": {
    "browser": true,
    "node": true,
    "useTabs": true
  },
  "plugins": ["prettier"],
  "extends": [
    "eslint:recommended",
    "plugin:prettier/recommended",
    "airbnb-base"
  ],
  "rules": {
    "prettier/prettier": "error"
  }
}

 

vscode ํ”„๋กœ์ ํŠธ ํด๋” ์ตœ์ƒ๋‹จ์— .prettierrc.json ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ํ›„ ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์ž.

{
    "trailingComma": "es5",
    "tabWidth": 2,
    "semi": true,
    "singleQuote": true
  }

 

 

์ด์ œ js๋ฅผ ์•„๋ฌด๋ ‡๊ฒŒ๋‚˜ ์ž…๋ ฅํ•œ์ฑ„ ์ €์žฅ์„ ํ•˜๋ฉด ์•Œ์•„์„œ ์ฝ”๋“œ ์ปจ๋ฒค์…˜์„ ์ง€์ผœ์ค„ ๊ฒƒ์ด๋‹ค!

 

์„ธ์„ธํ•œ ์„ค์ •์€ ์•„๋ž˜ ๋งํฌ์—์„œ ํ™•์ธํ•ด๋ณด๋ก ํ•˜์ž!

 

https://eslint.org/docs/rules/

 

List of available rules

โœ“no-unreachabledisallow unreachable code after `return`, `throw`, `continue`, and `break` statements

eslint.org

 

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