파이널 프로젝트를 진행하며 제일 후회 했던 것 중 하나가 처음부터 Lint와 Formatter를 설정하지 않은 것이다! 컴포넌트 생성(화살표 함수 vs 일반 함수), quotation( ' ' vs " "), 그리고 사용하지도 않는 변수를 만들어 놓고 git에 push 하고, 테스트로 console.log() 찍어놓고 push하고.. 정말.. 어지러운 코드 덩어리가 만들어졌다. 토이 프로젝트 같은경우 끝나고 리팩토링을 하며 코드에 일관성을 주었는데, 파이널 프로젝트 같은 경우는.. 진짜 답이 없더라..! 내가 봐도 보기 힘든 코드인데, 제 3자는 얼마나 더 보기 힘들까..^^!...그러면서 궁금했던게.. 그럼 현업에서는 더 많은 사람들이 같이 코드를 짤텐데 어떻게 일관적인 코드를 유지하는지 궁금했다. 찾아보니 팀으로 작업을 할 때는 여러 작업자들의 코딩 스타일을 일치시키기 위한 Linter와 Code Formatter를 사용하는 것이 좋다고 한다.
npm install eslint --save-devnpm install prettier --save-devnpm install eslint-config-prettier --save-dev
// .eslintrc
{
"extends": ["react-app", "eslint:recommended", "prettier"],
"env": {
"es6": true
},
"rules": {
"no-var": "error", // var 금지
"no-multiple-empty-lines": "error", // 여러 줄 공백 금지
"no-console": ["error", { "allow": ["warn", "error", "info"] }], // console.log() 금지
"eqeqeq": "error", // 일치 연산자 사용 필수
"dot-notation": "error", // 가능하다면 dot notation 사용
"no-unused-vars": "error" // 사용하지 않는 변수 금지
}
}
참고자료 : rule Reference, configuration, Configure Plugins
module.exports = {
printWidth: 100, // printWidth default 80 => 100 으로 변경
singleQuote: true, // "" => ''
arrowParens: "avoid", // arrow function parameter가 하나일 경우 괄호 생략
};
참고자료: prettier options
npm install husky --save-dev
(처음 husky 세팅하는 사람만 실행 필요) npx husky install
npx husky install : husky에 등록된 hook을 실제 .git에 적용시키기 위한 스크립트husky install 이 될 수 있도록 하는 설정{
"scripts": {
"postinstall": "husky install"
},
}scripts 설정
Husky를 실행할때마다 매번 다 검사를 할 수는 없으니 cache 설정을 해주자!
// package.json
{
"scripts": {
"postinstall": "husky install",
"format": "prettier --cache --write .",
"lint": "eslint --cache .",
},
}
add pre-commit, pre-push hook
npx husky add .husky/pre-commit "npm run format"npx husky add .husky/pre-push "npm run lint"