협업 프로젝트시에 코딩룰이 적용된 소스는 인체에 유익 하다는 전제 하에 강제하는 방법을 알아 본다.
MIT license를 가진 code formatter 모듈입니다. Code format이란 줄 바꿈, 줄간격, 괄호의 위치 등 여러 가지 Style을 의미하며, Prettier는 이런 code style을 자동적으로 정리해 줍니다.
javascript 정적 분석 도구
ESLint란? ECMAScript Lint의 줄임말로, javascript의 표준을 다루는 협회가 바로 ECMA입니다. Lint는 유닉스 시절에 정적코드 분석 프로그램인 'Linter"에서 따온 이름입니다.
full command
yarn add -D eslint prettier eslint-plugin-import eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y eslint-config-prettier
: 1. eslint: Javascript 정적 분석 도구
yarn add --dev eslint
: 2. prettier: auto code formatter module
yarn add --dev prettier
: 3. ES6의 import, export 구문 지원
yarn add --dev eslint-plugin-import
: 4. eslint와 prettier간 충돌방지 플러그인
yarn add eslint-plugin-prettier --dev
: 5. for react 구문
yarn add eslint-plugin-react --dev
yarn add eslint-plugin-react-hooks --dev
: 6. for jsx(xml) 정적 분석
yarn add eslint-plugin-jsx-a11y --dev
: 7. prettier 설정
yarn add --dev eslint-config-prettier
file: root/.prettierrc
{
"endOfLine": "auto",
"semi": true,
"singleQuote": false,
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"arrowParens": "avoid",
"bracketSpacing": false,
"htmlWhitespaceSensitivity": "css",
"jsxBracketSameLine": false,
"jsxSingleQuote": false,
"proseWrap": "preserve",
"quoteProps": "as-needed",
"trailingComma": "none",
"vueIndentScriptAndStyle": false,
"filepath": "",
"requirePragma": false,
"insertPragma": false,
"overrides": [
{
"files": "*.json",
"options": { "printWidth": 80, "trailingComma": "none" }
},
{
"files": "*.prettierrc",
"options": { "printWidth": 80, "trailingComma": "none" }
}
]
}
아래 사이트를 통해 룰 설정을 미리 체험 할 수 있다.
https://prettier.io/playground/
yarn eslint --init
yarn run v1.22.19
$ C:\Projects\Sources\react-components\node_modules\.bin\eslint --init
You can also run this command directly using 'npm init @eslint/config'.
√ How would you like to use ESLint? · problems
√ What type of modules does your project use? · esm
√ Which framework does your project use? · react
√ Does your project use TypeScript? · No / Yes
√ Where does your code run? · browser
√ What format do you want your config file to be in? · JSON
The config that you've selected requires the following dependencies:
eslint-plugin-react@latest
√ Would you like to install them now? · No / Yes
√ Which package manager do you want to use? · yarn
Installing eslint-plugin-react@latest
{
"env": {
"browser": true,
"es2021": true
},
"extends": ["eslint:recommended", "plugin:react/recommended"],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["react"],
"rules": {}
}
"devDependencies": {
"@craco/craco": "^7.1.0",
"eslint": "^8.53.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"prettier": "^3.0.3"
}
.eslintrc 추가
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"prettier",
"eslint:recommended",
"plugin:react/recommended",
"plugin:jsx-a11y/recommended",
"plugin:prettier/recommended",
"plugin:react-hooks/recommended"
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["react"],
"rules": {}
}
function App() {
const a = 10;
asdfasdfsafas;
...
[eslint]
src\App.js
Line 6:9: 'a' is assigned a value but never used no-unused-vars
Line 7:3: 'asdfasdfsafas' is not defined no-undef
Search for the keywords to learn more about each error.
ERROR in [eslint]
src\App.js
Line 6:9: 'a' is assigned a value but never used no-unused-vars
Line 7:3: 'asdfasdfsafas' is not defined no-undef
Search for the keywords to learn more about each error.
webpack compiled with 1 error
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
shift + alt + f를 누르면 아래와 같이 자동으로 잡아 준다.
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>