타입스크립트 보일러플레이트 바로가기
https://github.com/sangbeomheo/typescript-boilerplate
Prettier is an opinionated code formatter with support for:
JavaScript (including experimental features), JSX, Angular, Vue, Flow, TypeScript ,CSS, Less, and SCSS ...
opinionated(독선적인)
의 의미는 아마도 프리티어는 일관적인(지정한) 코드 스타일을 강제하는 것을 의미하는 것 같다.Prettier의 장점
은.prettierrc.json
를 생성한다.singleQuote
, arrowParens
두 가지 옵션만 별도로 지정했음npm i -D prettier
// .prettierrc.json
{
"singleQuote": true,
"arrowParens": "avoid"
}
https://prettier.io/docs/en/options.html
Use single quotes instead of double quotes.
single quotes
가 double quotes
보다 더 보이게 깔끔해보이기 이유로 사용. 기능에 영향이 전혀 없어서 부담 없이 설정했다. "singleQuote": true, // default: false
Include parentheses around a sole arrow function parameter.
https://prettier.io/docs/en/options.html#arrow-function-parentheses
always(default)
- Always include parens. Example: (x) => x
avoid
- Omit parens when possible. Example: x => x
v2.0.0
부터 default가 avoid
에서 always
로 변경됐다.{
"arrowParens": "avoid"
}
Print semicolons at the ends of statements.
true(default)
- Add a semicolon at the end of every statement.false
- Only add semicolons at the beginning of lines that may introduce ASI failures. https://prettier.io/docs/en/rationale.html#semicolons커뮤니티에서 자바스크립트 코드 작성 시 세미 콜론 사용 여부에 대한 논쟁을 쉽게 찾아볼 수 있다. 관련 내용이 정리된 블로그 글을 첨부한다.
"
1. 자바스크립트는 구문구분 시 세미콜론을 넣어야 한다
2. 세미콜론을 넣지 않을 때 인터프리터가 자동으로 넣어준다.
3. 자동으로 넣어줄 때 오류가 생길 예외사항에 대해 과거에 많은 사람이 세미콜론을 강제했다.
4. 인터프리터의 과정을 이해하여 코딩할 경우 세미콜론이 없어도 충분히 예외사항이 생기지 않는다.
5. 인터프리터의 과정에 따라 코딩스타일이 정립된다면 세미콜론을 강제할 필요가 없다.
6. 하지만 여전히 논쟁거리
"
https://okayoon.tistory.com/entry/자바스크립트-세미콜론-꼭-써야하나요 [Zzolab Blog :):티스토리]
ESLint
도 코드 코맷팅 기능을 지원한다. 그래서 Prettier
를 같이 쓰게되면 충돌을 할 수 있다. 그래서 ESLint
의 코드 코맷팅 기능을 꺼서 해당 기능은 오롯이 Prettier
가 담당하게 해야한다.ESLint
에서 플러그인을 사용해 lint 동작시 prettier도 같이 동작하게 통합할 수 있다.npm i -D eslint-config-prettier eslint-plugin-prettier
eslint-config-prettier
:eslint-plugin-prettier
: // .eslintrc.json
{
...
"extends": [
"airbnb-base",
"plugin:import/recommended",
"plugin:prettier/recommended" // 한 줄 축약형, eslint에서 prettier 관련 기능을 끄고, prettier 오류를 lint의 rule로 적용해 lint 오류로 보여줌
],
...
}
eslint
가 포맷팅 관련 오류를 잡는 중prettier
의 arrowParens
적용된 코드를 eslint
가 오류로 잡는다..prettierc.json
{
"singleQuote": true,
"arrowParens": "avoid"
}
.eslintrc.json
{
"root": true,
"env": {
"browser": true,
"es2021": true,
"node": true
},
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"sourceType": "module",
"ecmaVersion": "latest",
"project": "./tsconfig.json"
},
"extends": [
"airbnb-base",
"plugin:import/recommended",
"plugin:prettier/recommended"
],
"overrides": [
{
"files": "**/*.+(ts)",
"parser": "@typescript-eslint/parser",
"extends": [
"airbnb-typescript/base",
"plugin:import/typescript",
"plugin:@typescript-eslint/recommended"
]
}
],
"ignorePatterns": ["dist", "node_modules"]
}
https://prettier.io/docs/en/index.html
https://prettier.io/docs/en/options.html
https://github.com/prettier/eslint-config-prettier
https://www.npmjs.com/package/eslint-plugin-prettier