๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์‹œ๋„/Code-States

[D+45] ESlint ์„ค์น˜ํ•˜๊ธฐ

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2020. 10. 21.

 

D+45

-  ESlint ์„ค์น˜ํ•˜๊ธฐ -

(ESlinet ์„ค์น˜ํ•˜๊ธฐ)

 


 

 

ESlint ์„ค์น˜ํ•˜๊ธฐ

ESlint๋Š” ์ฝ”๋“œ์˜ ๋„์–ด์“ฐ๊ธฐ๋‚˜ ์ค„ ๋ฐ”๊ฟˆ ๋“ฑ์˜ ๊ทœ์น™๋“ค์„ ๋งž์ถ”๋Š” ๋ฐ์— ๋„์›€์ด ๋˜๋Š” Linter๋ผ๊ณ  ํ•˜๋ฉฐ,

์ข€ ๋” ์ •ํ™•ํ•˜๊ฒŒ๋Š” ECMAScript/JavaScript ์ฝ”๋“œ์—์„œ ๋ฐœ๊ฒฌ๋˜๋Š” ํŒจํ„ด์„ ์‹๋ณ„ํ•˜๊ณ  ๋ณด๊ณ ํ•˜๋Š” ๋„๊ตฌ๋กœ

์ฝ”๋“œ๋ฅผ ๋ณด๋‹ค ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ๋งŒ๋“ค๊ณ  ๋ฒ„๊ทธ๋ฅผ ํ”ผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋„๊ตฌ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

 

ESlint๋ฅผ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ๊ฒ ์ง€๋งŒ ๊ณต์‹๋ฌธ์„œ๋ฅผ ํ†ตํ•ด ์„ค์น˜๋ฅผ ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค๊ณ  ํ•˜๋‹ˆ

๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•ด ์„ค์น˜๋ฅผ ํ•ด๋ณด์•˜๋‹ค.

 

 

Getting Started with ESLint

Getting Started with ESLint ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs. In many ways, it is similar to JSLint and JSHint with a few exceptio

eslint.org

 

 

1 > ESLint ์„ค์น˜

npm install eslint --save-dev

ESLint๋Š” npm๊ณผ yarn์„ ํ†ตํ•ด์„œ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ ๋‚˜๋Š” npm์„ ์ด์šฉํ•ด ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด์•˜๋‹ค.

 

2 > ESLint ์„ค์ •

$ npx eslint --init

์„ค์น˜ ํ›„ ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์„ค์ •ํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด initํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด ์„ค์ •ํ•ด ์ฃผ๋ฉฐ,

์„ค์ • ๋ฐฉ๋ฒ•์€ ๋ณธ์ธ์ด ์›ํ•˜๋Š” ์„ค์ •์œผ๋กœ ๋งž์ถฐ์„œ ์ง„ํ–‰ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

 

๋‚ด๊ฐ€ ์„ค์ •ํ•œ ์„ ํƒ์ง€๋Š” ๊ทธ๋ƒฅ ์ฐธ๊ณ ๋งŒ ํ•ด์ค˜๋„ ์ข‹๋‹ค.

 

์งˆ๋ฌธ ์„ ํƒ
How would you like to use ESLint? To check syntax and find problems
What type of modules does your project use? CommonJS
Which framework does your project use? None of these
Does your project use TypeScript? No
Where does your code run? Node
What format do you want your config file to be in? ์›ํ•˜๋Š” ํ•ญ๋ชฉ ์„ ํƒ

 

$ npm run lint

๋งŒ์•ฝ ์ œ๋Œ€๋กœ ์„ค์ •์ด ๋˜์–ด lint ์—๋Ÿฌ๋ฅผ ์žก์•„๋‚ผ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด ํ•ด๋‹น ๋ช…๋ น์„ ์ž…๋ ฅํ•ด ์ฃผ๋ฉด

 

์ด์™€ ๊ฐ™์ด ์—๋Ÿฌ๋ฅผ ์ž˜ ์žก์•„๋‚ด๋Š” ๋ชจ์Šต์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

3 > Linting

ESLint์—์„œ ์ง€์ ํ•˜๋Š” ์—๋Ÿฌ๋ฅผ ์žก์•„๋‚ด๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ๋ณด์•˜๋‹ค.

 

1. mocha ๊ด€๋ จ ์—๋Ÿฌ ํ•ด๊ฒฐํ•˜๊ธฐ

   4:1  error  'describe' is not defined  no-undef
   6:3  error  'it' is not defined        no-undef

๋งŒ์•ฝ ๋‹น์‹ ์ด ์ด์™€ ๊ฐ™์€ ์—๋Ÿฌ๋ฅผ ๋ฐœ๊ฒฌํ•œ๋‹ค๋ฉด ๋จผ์ € ์—๋Ÿฌ์— ํ‘œ์‹œ๋˜์–ด ์žˆ๋Š” 'no-undef'์˜ ์˜๋ฏธ์— ๋Œ€ํ•ด์„œ ์•Œ์•„์•ผ ํ•˜๋Š”๋ฐ

์ด๋Š” ์ฝ”๋ฉ˜ํŠธ์— ์–ธ๊ธ‰๋˜์ง€ ์•Š๋Š” ํ•œ ๋ฐœํ‘œ๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜๋ฏธ๋กœ ์ด ์—๋Ÿฌ๋ฅผ ๋” ์ด์ƒ ๋ณด๊ณ  ์‹ถ์ง€ ์•Š๋‹ค๋ฉด

ESLint์—๊ฒŒ ํ˜„์žฌ mocha๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ์•Œ๋ ค์ค˜์•ผ ํ•ด๋‹น ์—๋Ÿฌ๋ฅผ ๋ณด์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

 

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์€. eslintrc.js์— 'env'ํ‚ค ์•ˆ์— "mocha": true๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

 

 

Configuring ESLint

Configuring ESLint ESLint is designed to be completely configurable, meaning you can turn off every rule and run only with basic syntax validation, or mix and match the bundled rules and your custom rules to make ESLint perfect for your project. There are

eslint.org

mocha ๋ง๊ณ ๋„ ๋‹ค๋ฅธ ๊ฒƒ๋“ค๋„ ํ•ด๋‹น ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ˆ ์ฐพ์•„๋ณด๊ธธ ๋ฐ”๋ž€๋‹ค.

 

 

2. ESLint ๊ฒ€์‚ฌ ์กฐ๊ฑด ์™„ํ™”ํ•˜๊ธฐ

 

์ด์™ธ์—๋„ ๋‹ค์–‘ํ•œ ์—๋Ÿฌ๊ฐ€ ์กด์žฌํ•˜๊ฒ ์ง€๋งŒ ๊ทธ์ค‘์—์„œ๋„ ๋‚ด๊ฐ€ ์ œ๋Œ€๋กœ ํ–ˆ์Œ์—๋„ lint์—์„œ ์—๋Ÿฌ๋ฅผ ์ง€์ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

 

์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ESLint์—์„œ ๊ฒ€์‚ฌ ์กฐ๊ฑด์„ ์™„ํ™”ํ•ด ์ฃผ๋ฉด ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋œ๋‹ค.

 

 

List of available rules

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

eslint.org

๋จผ์ € ์œ„์˜ ๋งํฌ๋ฅผ ๋“ค์–ด๊ฐ€ ๋ฐœ์ƒ๋˜๊ณ  ์žˆ๋Š” ์—๋Ÿฌ์˜ ๋‚ด์šฉ์„ ํ™•์ธํ•˜๊ณ 

ํ•ด๋‹น ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์—๋Ÿฌ๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ  ๋„˜๊ธฐ๊ณ  ์‹ถ๋‹ค๋ฉด rules๋ผ๋Š” ํ‚ค์— ํ•ด๋‹น ์—๋Ÿฌ๋ฅผ ์ž…๋ ฅ ํ›„ 0์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด

์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฌด์‹œํ•˜๊ฒŒ ๋œ๋‹ค.

 

 

Configuring ESLint

Configuring ESLint ESLint is designed to be completely configurable, meaning you can turn off every rule and run only with basic syntax validation, or mix and match the bundled rules and your custom rules to make ESLint perfect for your project. There are

eslint.org

 

์ด๋Ÿฐ ์‹์œผ๋กœ ๊ณต์‹๋ฌธ์„œ๋ฅผ ํ™œ์šฉํ•ด ์ถฉ๋ถ„ํžˆ ์—ฌ๋Ÿฌ ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ

๋‚˜๋„ ์—ด์‹ฌํžˆ ๋ณต์Šตํ•˜๊ณ  ์—ฐ์Šตํ•ด๋ด์•ผ๊ฒ ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€