Skip to content

๐Ÿงฉ Coding convention

์œ ๋™๊ท  edited this page Jul 27, 2023 · 11 revisions

์ฐธ๊ณ :
eslint-config-airbnb
Airbnb JavaScript Style Guide
Integrating Prettier + ESLint + Airbnb Style Guide in VSCode

์ฝ”๋”ฉ ์ปจ๋ฒค์…˜, ๊ฐ€๋…์„ฑ์ด ์ข‹๊ณ  ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์šฉ์ดํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋”ฉ ์Šคํƒ€์ผ ๊ทœ์•ฝ. ์ฝ”๋”ฉ ์ปจ๋ฒค์…˜์„ ์ค€์ˆ˜ํ•˜๋ฉด ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ์ฃผ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ์ž ์žฌ์  ์œ„ํ—˜ ์š”์†Œ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

1. ๋‹ค์–‘ํ•œ ๊ธฐ์—…์˜ ์ฝ”๋”ฉ ์ปจ๋ฒค์…˜

Airbnb Style Guide - ๋ณธ ํ”„๋กœ์ ํŠธ ์‚ฌ์šฉ ์ปจ๋ฒค์…˜
JavaScript Standard Style
Idiomatic JavaScript Style Guide
Google Style Guide

2. Install

  1. npm๊ณผ npx๋ฅผ ์„ค์น˜ํ•œ๋‹ค.
  2. VSCode ์ต์Šคํ…์…˜ ESLint, Prettier๋ฅผ ์„ค์น˜ํ•œ๋‹ค.
  3. ESLint์™€ Prettier ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ”„๋กœ์ ํŠธ์— ์„ค์น˜ํ•œ๋‹ค. ํ”„๋กœ์ ํŠธ์˜ ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
> npm install -D eslint prettier
  1. Airbnb config๋ฅผ ์„ค์น˜ํ•œ๋‹ค. npm 5+๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋ชจ๋“  ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
> npx install-peerdeps --dev eslint-config-airbnb
  1. eslint-config-prettier์™€ eslint-plugin-prettier๋ฅผ ์„ค์น˜ํ•œ๋‹ค.
> npm install -D eslint-config-prettier eslint-plugin-prettier
  1. ํ”„๋กœ์ ํŠธ์˜ ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์— .eslintrc.json ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ๋‹ค์Œ ๋‚ด์šฉ์„ ์ž‘์„ฑํ•œ๋‹ค.
{
  "extends": ["airbnb", "prettier"], //์ด ์„ค์ • ํŒŒ์ผ์€ airbnb ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ์™€ prettier์˜ ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ๋ฅผ ํ™•์žฅํ•œ๋‹ค.
  "plugins": ["prettier"], //prettier ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์‚ฌ์šฉํ•œ๋‹ค.
  "rules": {
    "prettier/prettier": ["error"], //prettier์—์„œ ๋ฐœ์ƒํ•œ ๋ชจ๋“  ์Šคํƒ€์ผ ๊ฒฝ๊ณ ๋ฅผ ์—๋Ÿฌ๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค.
    "no-unused-vars": "warn", //์„ ์–ธํ–ˆ์œผ๋‚˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ฒฝ๊ณ ๋กœ ํ‘œ์‹œํ•œ๋‹ค.
    "no-undef": "off" //์•Œ ์ˆ˜ ์—†๋Š” ๊ฐ’์— ๋Œ€ํ•ด ๊ฒฝ๊ณ ๋ฅผ ๋ˆ๋‹ค.
  },
  "env": {
    "browser": true, //๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ ์ž‘๋™ํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ์„ค์ •ํ•œ๋‹ค.
    "node": true, //Node ํ™˜๊ฒฝ์—์„œ ์ž‘๋™ํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ์„ค์ •ํ•œ๋‹ค.
    "es2021": true //ECMAScript 2021 ํ‘œ์ค€์„ ์ง€์›ํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ์„ค์ •ํ•œ๋‹ค.
  },
  "overrides": [
    {
      "env": {
        "node": true //Node ํ™˜๊ฒฝ์—์„œ ์ž‘๋™ํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ์„ค์ •ํ•œ๋‹ค.
      },
      "files": [".eslintrc.{js,cjs}"], //์ด ์„ค์ •์ด ์ ์šฉ๋˜๋Š” ํŒŒ์ผ๋“ค์˜ ํŒจํ„ด์„ ๋ช…์‹œํ•œ๋‹ค.
      "parserOptions": {
        "sourceType": "script" //์„ค์ • ํŒŒ์ผ์˜ ์†Œ์Šค ํƒ€์ž…์„ 'script'๋กœ ๋ช…์‹œํ•œ๋‹ค.
      }
    }
  ],
  "parserOptions": {
    "ecmaVersion": "latest", //๋งˆ์ง€๋ง‰ ECMASclima์˜ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•ด ํŒŒ์‹ฑํ•œ๋‹ค.
    "sourceType": "module" //์ „์ฒด ํ”„๋กœ์ ํŠธ์˜ ์†Œ์Šค ํƒ€์ž…์„ 'module'๋กœ ๋ช…์‹œํ•œ๋‹ค.
  }
}
  1. ํ”„๋กœ์ ํŠธ์˜ ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์— .prettierrc ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  Prettier config file์„ ์ฐธ๊ณ ํ•˜์—ฌ ์›ํ•˜๋Š” ํฌ๋งทํŒ… ์˜ต์…˜์„ ์„ค์ •ํ•œ๋‹ค.
{
  "arrowParens": "avoid", // ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๊ด„ํ˜ธ ์‚ฌ์šฉ ๋ฐฉ์‹
  "bracketSpacing": false, // ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์—์„œ ๊ด„ํ˜ธ์— ๊ณต๋ฐฑ ์‚ฝ์ž… ์—ฌ๋ถ€ 
  "endOfLine": "auto", // EoF ๋ฐฉ์‹, OS๋ณ„๋กœ ์ฒ˜๋ฆฌ ๋ฐฉ์‹์ด ๋‹ค๋ฆ„ 
  "htmlWhitespaceSensitivity": "css", // HTML ๊ณต๋ฐฑ ๊ฐ๋„ ์„ค์ •
  "jsxBracketSameLine": false, // JSX์˜ ๋งˆ์ง€๋ง‰ `>`๋ฅผ ๋‹ค์Œ ์ค„๋กœ ๋‚ด๋ฆด์ง€ ์—ฌ๋ถ€ 
  "jsxSingleQuote": true, // JSX์— singe ์ฟผํ…Œ์ด์…˜ ์‚ฌ์šฉ ์—ฌ๋ถ€
  "printWidth": 100, //  ์ค„ ๋ฐ”๊ฟˆ ํ•  ํญ ๊ธธ์ด
  "proseWrap": "preserve", // markdown ํ…์ŠคํŠธ์˜ ์ค„๋ฐ”๊ฟˆ ๋ฐฉ์‹ (v1.8.2)
  "quoteProps": "as-needed", // ๊ฐ์ฒด ์†์„ฑ์— ์ฟผํ…Œ์ด์…˜ ์ ์šฉ ๋ฐฉ์‹
  "semi": true, // ์„ธ๋ฏธ์ฝœ๋ก  ์‚ฌ์šฉ ์—ฌ๋ถ€
  "singleQuote": true, // single ์ฟผํ…Œ์ด์…˜ ์‚ฌ์šฉ ์—ฌ๋ถ€
  "tabWidth": 2, // ํƒญ ๋„ˆ๋น„ 
  "trailingComma": "all", // ์—ฌ๋Ÿฌ ์ค„์„ ์‚ฌ์šฉํ•  ๋•Œ, ํ›„ํ–‰ ์ฝค๋งˆ ์‚ฌ์šฉ ๋ฐฉ์‹
  "useTabs": false, // ํƒญ ์‚ฌ์šฉ ์—ฌ๋ถ€
  "rangeStart": 0, // ํฌ๋งทํŒ…์„ ๋ถ€๋ถ„ ์ ์šฉํ•  ํŒŒ์ผ์˜ ์‹œ์ž‘ ๋ผ์ธ ์ง€์ •
  "requirePragma": false, // ํŒŒ์ผ ์ƒ๋‹จ์— ๋ฏธ๋ฆฌ ์ •์˜๋œ ์ฃผ์„์„ ์ž‘์„ฑํ•˜๊ณ  Pragma๋กœ ํฌ๋งทํŒ… ์‚ฌ์šฉ ์—ฌ๋ถ€ ์ง€์ • (v1.8.0)
  "insertPragma": false, // ๋ฏธ๋ฆฌ ์ •์˜๋œ @format marker์˜ ์‚ฌ์šฉ ์—ฌ๋ถ€ (v1.8.0)
  "overrides": [ 
    {
      "files": "*.json",
      "options": {
        "printWidth": 200
      }
    }
  ], // ํŠน์ • ํŒŒ์ผ๋ณ„๋กœ ์˜ต์…˜์„ ๋‹ค๋ฅด๊ฒŒ ์ง€์ •ํ•จ, ESLint ๋ฐฉ์‹ ์‚ฌ์šฉ
}
  1. VSCode์˜ User Setting ํŒŒ์ผ(setting.json)์— "editor.formatOnSave": true๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.

Clone this wiki locally