deno fmt
,代码格式化
命令行用法
deno fmt [OPTIONS] [files]...
自动格式化各种文件类型。
deno fmt myfile1.ts myfile2.ts
支持的文件类型包括
JavaScript, TypeScript, Markdown, JSON(C) and Jupyter Notebooks
受到相应不稳定标志支持的文件类型(参见格式化选项)
HTML, CSS, SCSS, SASS, LESS, YAML, Svelte, Vue, Astro and Angular
格式化标准输入并写入标准输出
cat file.ts | deno fmt -
检查文件是否已格式化
deno fmt --check
通过在代码前添加忽略注释来忽略格式化
// deno-fmt-ignore
通过在文件顶部添加忽略注释来忽略格式化整个文件
// deno-fmt-ignore-file
选项 Jump to heading
--config
Jump to heading
短标志:-c
配置 deno 的不同方面,包括 TypeScript、代码检查和代码格式化。通常配置文件名为 deno.json
或 deno.jsonc
并会自动检测;在这种情况下,此标志不是必需的。
--no-config
Jump to heading
禁用自动加载配置文件。
格式化选项 Jump to heading
--check
Jump to heading
检查源文件是否已格式化。
--ext
Jump to heading
设置所提供文件的内容类型。
--ignore
Jump to heading
忽略格式化特定的源文件。
--indent-width
Jump to heading
定义缩进宽度 [默认值: 2]
--line-width
Jump to heading
定义最大行宽 [默认值: 80]
--no-semicolons
Jump to heading
除非必要,否则不使用分号 [默认值: false]
--prose-wrap
Jump to heading
定义文本应如何换行 [默认值: always]
--single-quote
Jump to heading
使用单引号 [默认值: false]
--unstable-component
Jump to heading
启用格式化 Svelte、Vue、Astro 和 Angular 文件。
--unstable-sql
Jump to heading
启用格式化 SQL 文件。
--use-tabs
Jump to heading
使用制表符而不是空格进行缩进 [默认值: false]
文件监视选项 Jump to heading
--no-clear-screen
Jump to heading
在监视模式下不清除终端屏幕。
--watch
Jump to heading
监视文件更改并自动重启进程。仅监视入口点模块图中的本地文件。
--watch-exclude
Jump to heading
从监视模式中排除提供的文件/模式。
支持的文件类型 Jump to heading
Deno 自带一个内置的代码格式化工具,可以自动格式化以下文件
文件类型 | 扩展名 | 注释 |
---|---|---|
JavaScript | .js , .cjs , .mjs |
|
TypeScript | .ts , .mts , .cts |
|
JSX | .jsx |
|
TSX | .tsx |
|
Markdown | .md , .mkd , .mkdn , .mdwn , .mdown , .markdown |
|
JSON | .json |
|
JSONC | .jsonc |
|
CSS | .css |
|
HTML | .html |
|
Nunjucks | .njk |
|
Vento | .vto |
|
YAML | .yml , .yaml |
|
Sass | .sass |
|
SCSS | .scss |
|
LESS | .less |
|
Jupyter Notebook | .ipynb |
|
Astro | .astro |
需要 --unstable-component 标志或 "unstable": ["fmt-component"] 配置选项。 |
Svelte | .svelte |
需要 --unstable-component 标志或 "unstable": ["fmt-component"] 配置选项。 |
Vue | .vue |
需要 --unstable-component 标志或 "unstable": ["fmt-component"] 配置选项。 |
SQL | .sql |
需要 --unstable-sql 标志或 "unstable": ["fmt-sql"] 配置选项。 |
deno fmt
可以格式化 Markdown 文件中的代码片段。 代码片段必须用三个反引号括起来,并具有语言属性。
忽略代码 Jump to heading
JavaScript / TypeScript / JSONC Jump to heading
通过在代码前添加 // deno-fmt-ignore
注释来忽略格式化代码
// deno-fmt-ignore
export const identity = [
1, 0, 0,
0, 1, 0,
0, 0, 1,
];
或者通过在文件顶部添加 // deno-fmt-ignore-file
注释来忽略格式化整个文件。
Markdown / HTML / CSS Jump to heading
通过在下一个项目前添加 <!-- deno-fmt-ignore -->
注释来忽略格式化下一个项目
<html>
<body>
<p>
Hello there
<!-- deno-fmt-ignore -->
</p>
</body>
</html>
要忽略一段代码,请将代码用 <!-- deno-fmt-ignore-start -->
和 <!-- deno-fmt-ignore-end -->
注释括起来。
或者通过在文件顶部添加 <!-- deno-fmt-ignore-file -->
注释来忽略格式化整个文件。
YAML Jump to heading
通过在下一个项目前添加 # deno-fmt-ignore
注释来忽略格式化下一个项目
# deno-fmt-ignore aaaaaa: bbbbbbb