On this page
deno fmt
Deno 附带一个基于 dprint 的内置代码格式化器,能够自动将你的代码格式化为一致的风格。有关更全面的概述,请参阅 Linting and Formatting。
基本用法 Jump to heading
格式化当前目录中所有受支持的文件:
deno fmt
格式化特定文件或目录:
deno fmt main.ts src/
监视模式 Jump to heading
在文件发生更改时自动重新格式化文件:
deno fmt --watch
在 CI 中检查格式 Jump to heading
使用 --check 验证文件是否已格式化,而不修改它们。若有任何文件未格式化,该命令
会以非零状态码退出:
deno fmt --check
添加 --fail-fast 可在遇到第一个未格式化文件时停止,而不是报告全部
文件,这在大型代码库中很有用:
deno fmt --check --fail-fast
格式化 stdin Jump to heading
格式化通过 stdin 传入的代码——这对编辑器集成很有用:
cat main.ts | deno fmt -
配置格式化器 Jump to heading
在你的 deno.json 中自定义格式化选项:
{
"fmt": {
"useTabs": false,
"lineWidth": 80,
"indentWidth": 2,
"semiColons": true,
"singleQuote": false,
"proseWrap": "preserve"
}
}
有关所有可用选项,请参阅 Configuration 页面。
从 .editorconfig 继承设置 Jump to heading
deno fmt 还会读取 .editorconfig 文件,并
用它们来填充你尚未在其他地方设置的任何格式化选项。其优先级从高到低依次为:
- CLI 标志(
--indent-width、--use-tabs等) deno.json中的fmt块.editorconfig- 内置默认值
因此,.editorconfig 只会提供你尚未通过标志或 deno.json 配置过的值。
诸如 indent_style、indent_size 和 max_line_length 之类的属性会映射到相应的 deno fmt 选项。
包含和排除文件 Jump to heading
在 deno.json 中指定要格式化的文件:
{
"fmt": {
"include": ["src/"],
"exclude": ["src/testdata/", "src/generated/**/*.ts"]
}
}
你也可以在命令行中排除文件:
deno fmt --ignore=dist/,build/
支持的文件类型 Jump to heading
| 文件类型 | 扩展名 | 备注 |
|---|---|---|
| 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 |
|
| XML | .xml |
|
| SVG | .svg |
|
| Nunjucks | .njk |
|
| Vento | .vto |
|
| YAML | .yml, .yaml |
|
| 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 文件中的代码块。 代码块必须用三重反引号括起来,并具有语言属性。
标记格式化器(HTML、XML、SVG 和 --unstable-component 格式)
以及样式格式化器(CSS、SCSS 和 Less)已重写为仅调整
空白字符。它们不会重新排序或重写标记,而是将未知语法
(厂商扩展、未来的 at-rule、模板表达式,甚至损坏的标记)
原样透传而不是报错,因此在
真实世界的文件上格式化更加稳健。
忽略代码 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>
你好
<!-- 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
deno fmt [OPTIONS] [files]...Auto-format various file types.
deno fmt myfile1.ts myfile2.ts
Supported file types are:
JavaScript, TypeScript, Markdown, JSON(C) and Jupyter Notebooks
Supported file types which are behind corresponding unstable flags (see formatting options):
HTML, CSS, SCSS, LESS, YAML, Svelte, Vue, Astro and Angular
Format stdin and write to stdout:
cat file.ts | deno fmt -
Check if the files are formatted:
deno fmt --check
Ignore formatting code by preceding it with an ignore comment:
// deno-fmt-ignore
Ignore formatting a file by adding an ignore comment at the top of the file:
// deno-fmt-ignore-file
Options Jump to heading
Configure different aspects of deno including TypeScript, linting, and code formatting.
Typically the configuration file will be called deno.json or deno.jsonc and
automatically detected; in that case this flag is not necessary.
--no-configDisable automatic loading of the configuration file.
--permit-no-filesDon't return an error code if no files were found.
Formatting options Jump to heading
--checkCheck if the source files are formatted.
--ext<ext>Set content type of the supplied file.
--fail-fastStop checking files on first format error.
--ignore<ignore>Ignore formatting particular source files.
--indent-width<indent-width>Define indentation width [default: 2]
--line-width<line-width>Define maximum line width [default: 80]
--no-editorconfigDon't read .editorconfig files to infer formatting options [default: false]
--no-semicolons<no-semicolons>optionalDon't use semicolons except where necessary [default: false]
--prose-wrap<prose-wrap>Define how prose should be wrapped [default: always]
--single-quote<single-quote>optionalUse single quotes [default: false]
--unstable-componentEnable formatting Svelte, Vue, Astro and Angular files.
--unstable-sqlEnable formatting SQL files.
--use-tabs<use-tabs>optionalUse tabs instead of spaces for indentation [default: false]
File watching options Jump to heading
--no-clear-screenDo not clear terminal screen when under watch mode.
--watch<FILES>optionalWatch for file changes and restart process automatically. Local files from entry point module graph are watched by default. Additional paths might be watched by passing them as arguments to this flag.
--watch-exclude<FILES>optionalExclude provided files/patterns from watch mode.
Last updated on