본문 바로가기
경일/Block-chain

Solidity(솔리디티), smart contract(스마트 컨트랙트) prettier(프리티어) 적용하기 (prettier-plugin-solidity)

by dev_kong 2022. 7. 12.
728x90
728x90

0. 목차

1. 개요

2. 플러그인 설치

3. .prettierrc 작성

4. vscode 설정 변경

5. semicolon

1. 개요

최근 컨트랙트를 배우면서 당연하게도 solidity를 사용하게 되었는데,
기존에 있던 prettier 만으로는 solidity 코드에 formmater가 적용이 안된다.
매우.. 매우 불편하다.


그래서 찾다보니 플러그인을 이용해 할 수 있는 방법이 있어서 작성해보려 한다.

 

2. 플러그인 설치

 

플러그인을 사용해서 기존 prettier 설정에 추가적인 설정을 부가할 수 있다.
사용할 플러그인의 이름은
prettier-plugin-solidity이다.

 

GitHub

 

GitHub - prettier-solidity/prettier-plugin-solidity: A Prettier plugin for automatically formatting your Solidity code.

A Prettier plugin for automatically formatting your Solidity code. - GitHub - prettier-solidity/prettier-plugin-solidity: A Prettier plugin for automatically formatting your Solidity code.

github.com

 

npm i -d prettier-plugin-solidity

 

기존에 프리티어를 사용하고 있는 프로젝트이거나, 글로벌로 사용하고 있다면 플러그인만 추가하면 되지만,
그렇지 않다면 prettier 까지 같이 설치를 해줘야 한다.

 

3. .prettierrc 작성

 

설치를 마쳤다면 이제 .prettierrc 파일을 생성해서 안의 내용을 작성해주자.
기존에 작성되어 있는 내용이 있다면,
기존에 작성된 내용 아래쪽에 아래의 코드를 복붙 해주면 된다.

 

{
  // 기존에 작성된 내용
  "overrides": [
    {
      "files": "*.sol",
      "options": {
        "printWidth": 80,
        "tabWidth": 2,
        "useTabs": false,
        "singleQuote": false,
        "bracketSpacing": false,
        "explicitTypes": "always",
        "semi": true
      }
    }
  ]
}

 

4. vscode 설정 변경

한가지 설정을 더해줘야 한다.

루트 디렉토리에 

.vscode 라는 이름의 디렉토리를 만들고

해당 디렉토리 안에서 settings.json 을 만들어주자.

그리고
settings.json에서

 

{
  // 기존에 작성된 내용
  "editor.formatOnSave": true,
  "solidity.formatter": "prettier", // This is the default so it might be missing.
  "[solidity]": {
    "editor.defaultFormatter": "JuanBlanco.solidity"
  }
}

 

위 코드를 추가해주자.

 

저장을 잘해주고 켜져있는 모든 vscode를 다 끄고 실행하는게 좋다.
가끔 설정 변경이 바로바로 안먹히는 경우가 있다.

5. semicolon

모든 설정이 다 잘 먹힌다.
...semicolon 빼고..


깃헙에 들어가서 issue 검색을 해보니..

깃헙 이슈

 

GitHub - prettier-solidity/prettier-plugin-solidity: A Prettier plugin for automatically formatting your Solidity code.

A Prettier plugin for automatically formatting your Solidity code. - GitHub - prettier-solidity/prettier-plugin-solidity: A Prettier plugin for automatically formatting your Solidity code.

github.com

 

솔리디티는 세미콜론이 없으면 파싱 단계에서 에러가 나는데,
포매팅은 파싱 이후에 진행 되는 거라 세미콜론 자동 추가는 불가능 하다고 한다...
하핳...


사실 세미콜론 붙이는게 너무 힘들어서 찾아본건데 매우 곤란하지만.. 어쩔 수 없다..
오른쪽 새끼손가락 열일 시키자...

728x90
728x90

댓글