๋…•ํ›„ํ‚ด

eslint์™€ prettier

0 views

๐Ÿ“Œ ESLint๋ž€?

Eslint๋Š” ES์™€ Lint๊ฐ€ ํ•ฉ์ณ์ง„ ๋‹จ์–ด์ž…๋‹ˆ๋‹ค. ES๋Š” ECMAScript๋กœ ์•„๋งˆ๋„ ์ž˜ ์•Œ๊ณ  ๊ณ„์‹œ์ง€๋งŒ, Lint๋Š” ์ฒ˜์Œ ๋ดค์„์ˆ˜๋„ ์žˆ๋Š”๋ฐ์š”. wikipedia์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

๋ฆฐํŠธ(lint) ๋˜๋Š”ย ๋ฆฐํ„ฐ(linter)๋Š”ย ์†Œ์Šค ์ฝ”๋“œ ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ ์˜ค๋ฅ˜,ย ๋ฒ„๊ทธ, ์Šคํƒ€์ผ ์˜ค๋ฅ˜, ์˜์‹ฌ์Šค๋Ÿฌ์šด ๊ตฌ์กฐ์ฒด์— ํ‘œ์‹œ(flag)๋ฅผ ๋‹ฌ์•„๋†“๊ธฐ ์œ„ํ•œ ๋„๊ตฌ๋“ค์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

๋Š๋‚Œ์ด ์˜ฌ๋“ฏ ๋ง๋“ฏโ€ฆ ๊ตฌ๊ธ€์— Lint๋ฅผ ๊ฒ€์ƒ‰ํ•ด๋ด…๋‹ˆ๋‹ค.

lint

์œ„์™€ ๊ฐ™์ด lint roller๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋ž˜๋œ ์Šค์›จํ„ฐ๋“ค์„ ๋ณด๋ฉด ์˜ท์— ์‚์ ธ๋‚˜์˜จ ๋ณดํ”„๋ผ๊ธฐ๋ฅผ ๋•Œ๋‚ผ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด์ฃ . ์–ด๋–ป๊ฒŒ ์ข€ ๋Š๋‚Œ์ด ์˜ค์‹œ๋‚˜์š”? Lint๋ฅผ ๊ฐ„๋‹จํžˆ ์ •์˜ํ•˜๋ฉด ์ฝ”๋“œ ๊ฐœ์„ ์„ ๋•๋Š” ๋„๊ตฌ ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ESLint๋Š” ์ •์  ์ฝ”๋“œ ๋ถ„์„๊ธฐ(Static Code Analyzer)๋กœ, ํŠน์ •ํ•œ ์ฝ”๋“œ ์Šคํƒ€์ผ์„ ๋”ฐ๋ฅด์ง€ ์•Š๋Š” ์ฝ”๋“œ์™€ ๋ฌธ๋ฒ•์ ์ธ ์—๋Ÿฌ๋ฅผ ์ฐพ์•„์ฃผ๊ณ , ๋ถ€๋ถ„์ ์œผ๋กœ ์ˆ˜์ •๊นŒ์ง€ ํ•ด์ฃผ๋Š” ๋„๊ตฌ ์ž…๋‹ˆ๋‹ค. ์ •์  ์ฝ”๋“œ ๋ถ„์„์ด๋ž€ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์—†์ด ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ๋ถ„์„ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ“Œ ์ฝ”๋“œ ํ˜•์‹ ๊ทœ์น™๊ณผ ์ฝ”๋“œ ํ’ˆ์งˆ ๊ทœ์น™

๋จผ์ € Linting(์ฝ”๋“œ ๊ฐœ์„ )์—๋Š” ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ๋ฒ”์ฃผ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” ์ฝ”๋“œ ํ˜•์‹ ๊ทœ์น™์ด๊ณ , ๋‘ ๋ฒˆ์งธ๋Š” ์ฝ”๋“œ ํ’ˆ์งˆ ๊ทœ์น™์ž…๋‹ˆ๋‹ค.

์ฝ”๋“œ ํ˜•์‹ ๊ทœ์น™์€ ์ฝ”๋“œ์˜ ํ˜•์‹์— ๊ด€ํ•œ ๊ทœ์น™์ž…๋‹ˆ๋‹ค. ๋“ค์—ฌ์“ฐ๊ธฐ์— tab์ด๋‚˜ space์˜ ํ˜ผ์šฉ์„ ๋ง‰๋Š” ESLint์˜ โ€˜no-mixed-spaces-and-tabsโ€™ ๊ทœ์น™์ด ๊ทธ ์˜ˆ์ž…๋‹ˆ๋‹ค.

์ฝ”๋“œ ํ’ˆ์งˆ ๊ทœ์น™์€ ์ฝ”๋“œ ํ’ˆ์งˆ์— ๊ด€ํ•œ ๊ทœ์น™์œผ๋กœ, ๋ฒ„๊ทธ๋ฅผ ์ฐพ๊ฑฐ๋‚˜ ์˜ˆ๋ฐฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ทœ์น™๋“ค์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ น ESLint์˜ โ€˜no-implict-globalsโ€™๋Š” ์ „์—ญ ์Šค์ฝ”ํ”„์˜ ๋ณ€์ˆ˜ ์„ ์–ธ์„ ๊ธˆ์ง€ํ•จ์œผ๋กœ์จ ๋ณ€์ˆ˜์˜ ์ถฉ๋Œ์„ ์˜ˆ๋ฐฉํ•˜๋Š” ์—ญํ• ์„ ํ•˜์ฃ .

ESLint๋Š” ์ฝ”๋“œ ํ˜•์‹ ๊ทœ์น™๊ณผ ์ฝ”๋“œ ํ’ˆ์งˆ ๊ทœ์น™ ๋ชจ๋‘ ๋‹ค๋ฃจ์ง€๋งŒ, Prettier๋Š” ์ฝ”๋“œ ํ˜•์‹ ๊ทœ์น™๋งŒ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ESLint๋งŒ ์“ฐ๋ฉด ๋˜์ง€ ์™œ Prettier๊นŒ์ง€ ๊ฐ™์ด ์“ฐ๋Š”๊ฑธ๊นŒ์š”?

์ด์œ ๋Š” Prettier๊ฐ€ ESLint๋ณด๋‹ค ์ฝ”๋“œ ํ˜•์‹ ๊ทœ์น™์„ ๋” ์ž˜ ์ ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์กฐ๊ธˆ ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜์—์„œ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค.


๐Ÿ“Œ ๋ฒˆ์™ธ๋กœ EditorConfig๋ž€?

์ฝ”๋“œ์˜ ์ผ๊ด€์„ฑ์„ ์œ„ํ•ด์„œ ESLint, Prettier ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ EditorConfig ์—ญ์‹œ๋„ ๋งŽ์ด ์“ฐ์ž…๋‹ˆ๋‹ค. EditorConfig๋Š” ์ฝ”๋“œ ํ˜•์‹ ๊ทœ์น™์ด๋‚˜ ์ฝ”๋“œ ํ’ˆ์งˆ ๊ทœ์น™์— ๊ด€์—ฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. EditorConfig๋Š” ํŒ€ ๋‚ด์— ์—ฌ๋Ÿฌ IDE ํˆด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋„ ์ฝ”๋“œ ์Šคํƒ€์ผ ํ†ต์ผ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค


๐Ÿ“Œ Prettier๊ฐ€ ESLint๋ณด๋‹ค ์ฝ”๋“œ ํ˜•์‹ ๊ทœ์น™์„ ์–ด๋–ป๊ฒŒ ๋” ์ž˜ ์ง€ํ‚ฌ๊นŒ?

์•„๋ž˜์™€ ๊ฐ™์ด ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜ ์ค€๋น„ํ–ˆ์Šต๋‹ˆ๋‹ค.

function printUser(firstName, lastName, number, street, code, city, country) {
  console.log(
    `${firstName} ${lastName} lives at ${number}, ${street}, ${code} in ${city}, ${country}`,
  )
}

printUser(
  'John',
  'Doe',
  48,
  '998 Primrose Lane',
  53718,
  'Madison',
  'United States of America',
)

ESLint๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

{
  "extends": ["eslint:recommended"],
  "env": {
    "es6": true,
    "node": true
  },
  "rules": {
    "max-len": ["error", {"code": 80}],
    "indent": ["error", 2]
  }
}

์œ„ ์ฝ”๋“œ์— ์ ์šฉ๋œ ๊ทœ์น™๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. console ๋ฌธ ํ—ˆ์šฉ ๊ธˆ์ง€ ( eslint์—์„œ ์ถ”์ฒœํ•˜๋Š” ๊ทœ์น™๋“ค์— ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. = eslint:recommended )
  2. ์ฝ”๋“œ ์ตœ๋Œ€ ๋ฌธ์ž์—ด ๊ธธ์ด 80
  3. ๋“ค์—ฌ์“ฐ๊ธฐ๋Š” 2์นธ

๊ทธ๋ฆฌ๊ณ  ๋งจ ์ฒ˜์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

error1

์ฝ”๋“œ ์ตœ๋Œ€ ๋ฌธ์ž์—ด ๊ธธ์ด๊ฐ€ 80์„ ๋„˜์—ˆ๊ณ , console ๋ฌธ์ด ์กด์žฌํ•˜๋ฉฐ, ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ€ ์ œ๋Œ€๋กœ ์•ˆ๋˜์–ด์žˆ๋‹ค๊ณ  error๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

ESLint์—์„œ ์ œ๊ณตํ•˜๋Š” ์—๋Ÿฌ ์ˆ˜์ • ํ”Œ๋ž˜๊ทธ(โ€“fix)์™€ ํ•จ๊ป˜ ESLint๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

error2

ESLint๊ฐ€ max-len๊ณผ console๋ฌธ ์—๋Ÿฌ๋Š” ์ˆ˜์ •ํ•˜์ง€ ๋ชปํ–ˆ์ง€๋งŒ, ๋“ค์—ฌ์“ฐ๊ธฐ ์—๋Ÿฌ๋Š” ๋ถ€๋ถ„์ ์œผ๋กœ ์ˆ˜์ •ํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋“ค์—ฌ์“ฐ๊ธฐ 2์นธ, ์ฝ”๋“œ ์ตœ๋Œ€ ๋ฌธ์ž์—ด ๊ธธ์ด 80์˜ ๊ทœ์น™์„ ์„ค์ •ํ•œ Prettier๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๊ฐ€ ์ž๋™ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

function printUser(firstName, lastName, number, street, code, city, country) {
  console.log(
    `${firstName} ${lastName} lives at ${number}, ${street}, ${code} in ${city}, ${country}`,
  )
}

printUser(
  'John',
  'Doe',
  48,
  '998 Primrose Lane',
  53718,
  'Madison',
  'United States of America',
)

ESLint๋Š” ํ•˜์ง€ ๋ชปํ•˜๋Š” max-len ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•ด์ง€๋Š” ๊ฒƒ์ด์ฃ . ํ•˜์ง€๋งŒ Prettier๋Š” ESLint์ฒ˜๋Ÿผ ์ฝ”๋“œ ํ’ˆ์งˆ์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋“ค(console.log)์— ๋Œ€ํ•ด์„œ ์–ด๋– ํ•œ ๊ฒฝ๊ณ ๋„ ๋ณด์—ฌ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ ํ˜•์‹๊ณผ ์ฝ”๋“œ ํ’ˆ์งˆ ๋‘˜ ๋‹ค ์žก๋Š” ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์€ ESLint์™€ Prettier๋ฅผ ๋™์‹œ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ ESLint์™€ Prettier, EditorConfig ์ค‘ ํ•˜๋‚˜๋งŒ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ์ด๊ฒƒ์€ ์ „์ ์œผ๋กœ ์‚ฌ์šฉ์ž์˜ ์„ ํƒ์— ๋‹ฌ๋ ค์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ช…์‹ฌํ•˜์„ธ์š”. ์•ž์—์„œ ๋ณด์•˜๋“ฏ์ด Prettier๋Š” ์ฝ”๋“œ ํ˜•์‹ ๊ทœ์น™๋งŒ์„ ์ง€ํ‚ฌ ๋ฟ, ํ’ˆ์งˆ ๊ทœ์น™์€ ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— Prettier๋ฅผ ๋จผ์ € ๊ณ ๋ คํ•˜๊ธฐ ๋ณด๋‹ค๋Š”, ESLint๋ฅผ ๋จผ์ € ๊ณ ๋ คํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœ ๋“œ๋ฆฝ๋‹ˆ๋‹ค


๐Ÿ“Œ ESLint์™€ Prettier์˜ ์ถฉ๋Œ

ESLint์™€ Prettier์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ทœ์น™์ด ์ถฉ๋Œํ•˜๋Š” ๋ถ€๋ถ„์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

conflict1

์šฐ๋ฆฌ๋Š” ์ด ์ถฉ๋Œ์„ ๋ง‰์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ €์žฅํ•  ๋•Œ๋งˆ๋‹ค issue์— ์˜ฌ๋ผ์˜จ ๋ฌดํ•œ ๋ฅดํ”„์— ๋น ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ Prettier๋Š” ์ฝ”๋“œ ํ˜•์‹ ๊ทœ์น™๋งŒ์„, ESlint๋Š” ์ฝ”๋“œ ํ’ˆ์งˆ ๊ทœ์น™๋งŒ์„ ๋‹ค๋ฃจ๊ฒŒ ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๊ฒน์น˜๋Š” ๊ฒƒ ์ค‘์— ์–ด๋Š ํ•œ์ชฝ์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์• ๋งคํ•œ ๊ฒƒ๋“ค๋„ ์กด์žฌํ•˜์ง€๋งŒ, ๋„ˆ๋ฌด ์„ธ์„ธํ•œ ๊ฒƒ ๊นŒ์ง€๋Š” ๊ณ ๋ คํ•˜์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๊ด€์‹ฌ์‚ฌ๋Š” ์˜ค์ง Prettier์™€ ESLint๊ฐ€ ์ถฉ๋Œ ์—†์ด ํ•˜๋‚˜์˜ ๊ทœ์น™๋งŒ ๋‹ค๋ฃจ๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋ง์ด์ฃ .

conflictresolve


๐Ÿ“Œ ESLint์™€ Prettier์˜ ์ถฉ๋Œ์„ ๋ง‰๊ธฐ์œ„ํ•œ ๋ฐฉ๋ฒ•

๊ฒฐ๋ก ๋ถ€ํ„ฐ ๋งํ•˜๋ฉด, ESLint์™€ Prettier ์ด์™ธ์— ๋‹ค์Œ ๋‘ ๊ฐ€์ง€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋” ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

  1. eslint-config-prettier
  2. eslint-plugin-prettier

ESLint์™€ Prettier๊ฐ€ ๊ณต์กดํ•˜๋ ค๋ฉด, ESLint์—์„œ Prettier์™€ ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜๋Š” ๊ทœ์น™๋“ค์„ ๋ชจ๋‘ ๋ฌด๋ ฅํ™” ์‹œํ‚ค๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ์—ญํ• ์„ 1๋ฒˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์ฃ .

eslint-config-prettier ์„ค์น˜ ํ›„ ESLint๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

{
  "extends": ["eslint:recommended", "prettier"],
  "env": {
    "es6": true,
    "node": true
  }
}

์ค‘์š”ํ•œ ๊ฒƒ์€ extends ๋ฐฐ์—ด์˜ ๋‚˜์ค‘ ์š”์†Œ๊ฐ€, ์™ผ์ชฝ ์š”์†Œ์˜ ์„ค์ • ๋‚ด์šฉ ์ค‘ ๊ณ‚์น˜๋Š” ๋ถ€๋ถ„์„ ๋ฎ์–ด์“ฐ๊ธฐ ๋•Œ๋ฌธ์—, prettier์—๊ฒŒ ์ฝ”๋“œ ํ˜•์‹ ๊ทœ์น™ ์ ์šฉ์„ 100% ์œ„์ž„ํ•˜๋ ค๋ฉด, ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ํ•ญ๋ชฉ์— prettier๋ฅผ ๊ธฐ์ž…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 1๋ฒˆ์— ๋Œ€ํ•œ ์„ค์ •์€ ์—ฌ๊ธฐ๊นŒ์ง€์ž…๋‹ˆ๋‹ค.

์ถ”๊ฐ€์ ์œผ๋กœ, ์ฝ”๋“œ ํ˜•์‹ ๊ทœ์น™ ์ ์šฉ ๋ฐ ์ฝ”๋“œ ํ’ˆ์งˆ ๊ทœ์น™ ์ ์šฉ์„ ์œ„ํ•ด ESLint์™€ Prettier๋ฅผ ๊ฐ๊ฐ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์€ ๋น„ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ์ด๋Š” 2๋ฒˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด์„œ ํ•œ ๋ฒˆ์˜ ์‹คํ–‰์œผ๋กœ ESLint์™€ Prettier๊ฐ€ ์ ์šฉ๋˜๊ฒŒ ์„ค์ •์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

eslint-plugin-prettier ์„ค์น˜ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ”๊ฐ€์ ์œผ๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

{
  "extends": ["eslint:recommended", "prettier"],
  "env": {
    "es6": true,
    "node": true
  },
  "rules": {
    "prettier/prettier": "error"
  },
  "plugins": [
    "prettier"
  ]
}

๐Ÿ“Œ ESLint์™€ Prettier์—์„œ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ทœ์น™์€ ๋ฌด์—‡์ด ์žˆ์„๊นŒ?

ESLint์™€ Prettier์˜ ์ฐจ์ด์ ์— ๋Œ€ํ•ด์„œ ์ง€๊ธˆ๊นŒ์ง€ ์„ค๋ช…ํ•ด์™”๋Š”๋ฐ, ๊ทธ๋Ÿฌ๋ฉด ์‹ค์ œ ๊ฐ๊ฐ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ ์šฉ ๊ฐ€๋Šฅํ•œ ๊ทœ์น™์ด ๋ฌด์—‡์ด ์žˆ๋Š”์ง€ ๋Œ€๋žต์ ์œผ๋กœ ์•Œ์•„๋ด…์‹œ๋‹ค.

ESLint์˜ ๊ฒฝ์šฐ, ESLint ๊ณต์‹ ๋ฌธ์„œ์—์„œ ์ถ”์ฒœํ•˜๋Š” ์„ค์ • ๊ทœ์น™๋“ค์„ ๋ช‡ ๊ฐœ ๋ณด์ž๋ฉด,

// comma-dangle: [2, "never"] ์„ค์ •์‹œ
// Error๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ,

var foo = {
  bar: 'baz',
  qux: 'quux', /*error Unexpected trailing comma.*/
}

var arr = [1, 2], /*error Unexpected trailing comma.*/

foo({
  bar: 'baz',
  qux: 'quux', /*error Unexpected trailing comma.*/
})

// Error๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ,

var foo = {
  bar: 'baz',
  qux: 'quux'
}

var arr = [1, 2]

foo({
  bar: 'baz',
  qux: 'quux'
})
// eslint no-dupe-args: 2 ์„ค์ •์‹œ

function foo(a, b, a) {
  /*error Duplicate param 'a'.*/
  console.log('which a is it?', a)
}
// eslint no-extra-semi: 2 ์„ค์ •์‹œ
// Error๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ

var x = 5 /*error Unnecessary semicolon.*/

function foo() {
  // code
} /*error Unnecessary semicolon.*/

// Error๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

var x = 5

var foo = function () {
  // code
}
module.exports = {
  trailingComma: 'all',
  // trailingComma๋Š” ํ›„ํ–‰์‰ผํ‘œ๋ผ๊ณ  ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค.
  // all์„ ํ•˜๋Š” ๊ฒฝ์šฐ, ๊ฐ์ฒด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ ๋’ค์— comma๋ฅผ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.

  // const obj = {
  // 								a:1,
  // 								b:2,
  // 						 }

  // none์„ ํ•˜๋Š” ๊ฒฝ์šฐ, comma๊ฐ€ ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค.
  // ํ›„ํ–‰์‰ผํ‘œ์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ž˜์—์„œ ์ถ”๊ฐ€์ ์œผ๋กœ ์„ค๋ช…ํ•  ๋‚ด์šฉ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  bracketSpacing: true,
  // true์ธ ๊ฒฝ์šฐ, ์ค‘๊ด„ํ˜ธ ์‚ฌ์ด์— ์ŠคํŽ˜์ด์Šค๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค.
  // { foo: bar }

  // false์ธ ๊ฒฝ์šฐ, ์ค‘๊ด„ํ˜ธ ์‚ฌ์ด์— ์ŠคํŽ˜์ด์Šค๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  // {for: bar}

  arrowParens: 'always',
  // 'always'์ธ ๊ฒฝ์šฐ, ํ•ญ์ƒ parenthesis๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
  // (x) => x;

  // 'avoid'์ธ ๊ฒฝ์šฐ, ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด parenthesis๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  // x => x;
}

โ€ป trailingComma์— ๋Œ€ํ•ด์„œ (Trailing comma after last line in object)

๋ฒ„์ „ ๊ด€๋ฆฌ ํˆด์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋Š” ์ฝ”๋“œ(version controlled code)๋ผ๋ฉด, trailingComma๋ฅผ ๊ฐ€๊ธ‰์  ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐ€์งœ ๋ณ€๊ฒฝ์ (spurious difference)์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ์ธ๋ฐ์š”. ๋งŒ์•ฝ trailingComma: โ€˜noneโ€™์ธ ์ƒํƒœ์—์„œ ์œ„ obj์— ์ƒˆ๋กœ์šด ํ”„๋กœํผํ‹ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ, ๋‘๊ฐœ์˜ ๋ผ์ธ์ด ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

// ์ƒˆ๋กœ์šด ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€ ์ „
const obj = {
  a: 1,
  b: 2,
}

// ์ƒˆ๋กœ์šด ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€ ํ›„
const obj = {
  a: 1,
  b: 2, // ๋ณ€๊ฒฝ๋œ Line 1
  c: 3, // ๋ณ€๊ฒฝ๋œ Line 2
}

๐Ÿ“š ์ฐธ๊ณ ๋ฌธํ—Œ

๋ฆฐํŠธ(ESLint)์™€ ํ”„๋ฆฌํ‹ฐ์–ด(Prettier)๋กœ ํ˜‘์—… ํ™˜๊ฒฝ ์„ธํŒ…ํ•˜๊ธฐ

Why You Should Use ESLint, Prettier & EditorConfig

What Is a Linter? Hereโ€™s a Definition and Quick-Start Guide

Set up ESlint, Prettier & EditorConfig without conflicts

It this the correct way of extending eslint rules?