代碼自動格式化工具【Python——yapf, 前端——Prettier】

Python代碼格式化工具Yapf

Python將代碼格式規範加入語法當中,形成了可讀性很高且嚴格的代碼風格。yapf能使您的代碼自動格式化爲更爲標準和規範的代碼風格,在大型開發需要同步代碼時有重要作用,統一的代碼風格可以讓任何人更方便的讀懂他人代碼。

YAPF (Yet Another Python Formatter)是Google開源的一個用來格式化Python代碼的工具. 支持2種代碼規範
PEP8
Google style

yapf官方文檔:https://pypi.org/project/yapf/

  • 安裝:

sudo pip install yapf

安裝後可以作爲命令行工具直接使用
可以試着對某個代碼demo.py進行格式化:

yapf demo.py

yapf的結果會預覽在命令行窗戶中,但此時並不是真的修改了文件,僅僅做了格式化後的預覽

  • yapf 前後格式化對比:

yapf -d demo.py

  • 若要直接對文件進行自動格式化修改:

yapf -i demo.py

  • 導出配置文件:

yapf --style-help > style.cfg

  • 自定義配置文件並使用

此例爲將縮進由4個空格改爲2個空格

$ yapf --style-help > my_style.cfg $ sed -i
“s/indent_width=4/indent_width=2/” my_style.cfg $ yapf --style
my_style.cfg loops.py

  • 在代碼中控制是否使用yapf
    部分代碼不進行格式化

# yapf: disable
[code will not be formatted]
# yapf: enable

- 將一個文件夾中的Python代碼進行格式化:

yapf -i -r [文件夾名]

- 完整參數列表:

usage: yapf [-h] [-v] [-d | -i] [-r | -l START-END] [-e PATTERN]
            [--style STYLE] [--style-help] [--no-local-style] [-p]
            [-vv]
            [files [files ...]]

Formatter for Python code.

positional arguments:
  files

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show version number and exit
  -d, --diff            print the diff for the fixed source
  -i, --in-place        make changes to files in place
  -r, --recursive       run recursively over directories
  -l START-END, --lines START-END
                        range of lines to reformat, one-based
  -e PATTERN, --exclude PATTERN
                        patterns for files to exclude from formatting
  --style STYLE         specify formatting style: either a style name (for
                        example "pep8" or "google"), or the name of a file
                        with style settings. The default is pep8 unless a
                        .style.yapf or setup.cfg file located in the same
                        directory as the source or one of its parent
                        directories (for stdin, the current directory is
                        used).
  --style-help          show style settings and exit; this output can be saved
                        to .style.yapf to make your settings permanent
  --no-local-style      don't search for local style definition
  -p, --parallel        Run yapf in parallel when formatting multiple files.
                        Requires concurrent.futures in Python 2.X
  -vv, --verbose        Print out file names while processing

前端代碼格式工具——Prettier

官網:https://prettier.io/
Prettier的中文意思是“漂亮的、機靈的”,也是一個流行的代碼格式化工具的名稱,它能夠解析代碼,使用你自己設定的規則來重新打印出格式規範的代碼。
Prettier具有以下幾個有優點:

  1. 可配置化
  2. 支持多種語言
  3. 集成多數的編輯器
  4. 簡潔的配置項

使用Prettier在code review時不需要再討論代碼樣式,節省了時間與精力。下面使用官方的例子來簡單的瞭解下它的工作方式。
可以將JavaScript,HTML,CSS,Markdown等語言進行自動格式化,還可以配合ESlint使用校驗代碼格式。

官方示例

安裝
使用yarn

yarn add prettier --dev --exact
# or globally
yarn global add prettier

使用npm

npm install --save-dev --save-exact prettier
# or globally
npm install --global prettier

和ESLint一起使用
很多項目都會使用ESLint來提高代碼的質量,有兩種方式能夠集成Prettier和ESLint,你也可以單獨或同時使用它們。
使用ESLint運行Prettier
如果你已經在你的項目中使用ESLint並且想要只通過單獨一條命令來執行你的所有的代碼檢查的話,你可以使用ESLint來爲你運行Prettier。
只需要使用eslint-plugin-prettier來添加Prettier作爲ESLint的規則配置。
yarn add --dev prettier eslint-plugin-prettier

    .eslintrc.json
    {
      "plugins": ["prettier"],
      "rules": {
        "prettier/prettier": "error"
      }
    }

關閉ESLint的格式規則
你是否通過ESLint來運行Prettier,又或者是單獨運行兩個工具,那你大概只想要每個格式問題只出現一次,而且你特別不想要ESLint僅僅是和Prettier有簡單的不同和偏好而報出“問題”。
所以你大概想要禁用衝突的規則(當保留其他Prettier不關心的規則時)最簡單的方式是使用eslint-config-prettier。它可以添加到任何ESLint配置上面。

yarn add --dev eslint-config-prettier

    .eslintrc.json
    {
      "extends": ["prettier"]
    }

轉載:https://www.jianshu.com/p/d6a69eb08f07https://www.jianshu.com/p/22d7a97720b7

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章