[D+45] ESlint ์ค์นํ๊ธฐ
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
์ด๋ฐ ์์ผ๋ก ๊ณต์๋ฌธ์๋ฅผ ํ์ฉํด ์ถฉ๋ถํ ์ฌ๋ฌ ๋ฌธ์ ๋ค์ ํด๊ฒฐํ ์ ์์ผ๋
๋๋ ์ด์ฌํ ๋ณต์ตํ๊ณ ์ฐ์ตํด๋ด์ผ๊ฒ ๋ค.