deno fmt
,程式碼格式化
Deno 內建程式碼格式化程式,它會自動格式化下列檔案
檔案類型 | 副檔名 |
---|---|
JavaScript | .js |
TypeScript | .ts |
JSX | .jsx |
TSX | .tsx |
Markdown | .md 、.markdown |
JSON | .json |
JSONC | .jsonc |
此外,deno fmt
可以格式化 Markdown 檔案中的程式碼片段。片段必須用三個反引號括起來,並有一個語言屬性。
# format all supported files in the current directory and subdirectories
deno fmt
# format specific files
deno fmt myfile1.ts myfile2.ts
# format all supported files in specified directory and subdirectories
deno fmt src/
# check if all the supported files in the current directory and subdirectories are formatted
deno fmt --check
# format stdin and write to stdout
cat file.ts | deno fmt -
忽略程式碼
在 TS/JS/JSONC 中,在程式碼前面加上 // deno-fmt-ignore
註解,即可忽略格式化程式碼
// deno-fmt-ignore
export const identity = [
1, 0, 0,
0, 1, 0,
0, 0, 1,
];
或者在檔案開頭加上 // deno-fmt-ignore-file
註解,即可忽略整個檔案。
在 Markdown 中,你可以使用 <!-- deno-fmt-ignore -->
註解,或使用 <!-- deno-fmt-ignore-file -->
註解忽略整個檔案。若要忽略 Markdown 的某個區段,請使用 <!-- deno-fmt-ignore-start -->
和 <!-- deno-fmt-ignore-end -->
註解將程式碼包起來。
設定
ℹ️ 建議使用預設選項。
從 Deno v1.14 開始,可以使用 設定檔 或下列 CLI 旗標自訂格式化程式
-
--use-tabs
- 是否使用 tab。預設為 false(使用空白)。 -
--line-width
- 印表機將嘗試保持在該行寬度以下。請注意,在某些情況下,印表機可能會超過此寬度。預設為 80。 -
--indent-width
- 縮排的字元數。預設為 2。 -
--no-semicolons
- 除了必要時,不使用分號。 -
--single-quote
- 是否使用單引號。預設為 false(使用雙引號)。 -
--prose-wrap={always,never,preserve}
- 定義 Markdown 檔案中散文應如何換行。預設為「always」。
注意:在 Deno 版本 < 1.31 中,您必須在這些標記前加上 options-
前綴(例如 --options-use-tabs
)