Skip to main content
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 中自定义格式化选项:

deno.json
{
  "fmt": {
    "useTabs": false,
    "lineWidth": 80,
    "indentWidth": 2,
    "semiColons": true,
    "singleQuote": false,
    "proseWrap": "preserve"
  }
}

有关所有可用选项,请参阅 Configuration 页面。

从 .editorconfig 继承设置 Jump to heading

deno fmt 还会读取 .editorconfig 文件,并 用它们来填充你尚未在其他地方设置的任何格式化选项。其优先级从高到低依次为:

  1. CLI 标志(--indent-width--use-tabs 等)
  2. deno.json 中的 fmt
  3. .editorconfig
  4. 内置默认值

因此,.editorconfig 只会提供你尚未通过标志或 deno.json 配置过的值。 诸如 indent_styleindent_sizemax_line_length 之类的属性会映射到相应的 deno fmt 选项。

包含和排除文件 Jump to heading

deno.json 中指定要格式化的文件:

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"] 配置项。

Note

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
<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 注释来忽略格式化:

HTML
# deno-fmt-ignore aaaaaa: bbbbbbb
Command line usage:
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-config
Jump to heading

Disable automatic loading of the configuration file.

--permit-no-files
Jump to heading

Don't return an error code if no files were found.

Formatting options Jump to heading

Check if the source files are formatted.

Set content type of the supplied file.

--fail-fast
Jump to heading

Stop checking files on first format error.

--ignore<ignore>
Jump to heading

Ignore formatting particular source files.

--indent-width<indent-width>
Jump to heading

Define indentation width [default: 2]

--line-width<line-width>
Jump to heading

Define maximum line width [default: 80]

--no-editorconfig
Jump to heading

Don't read .editorconfig files to infer formatting options [default: false]

--no-semicolons<no-semicolons>optional
Jump to heading

Don't use semicolons except where necessary [default: false]

--prose-wrap<prose-wrap>
Jump to heading

Define how prose should be wrapped [default: always]

--single-quote<single-quote>optional
Jump to heading

Use single quotes [default: false]

--unstable-component
Jump to heading

Enable formatting Svelte, Vue, Astro and Angular files.

--unstable-sql
Jump to heading

Enable formatting SQL files.

--use-tabs<use-tabs>optional
Jump to heading

Use tabs instead of spaces for indentation [default: false]

File watching options Jump to heading

--no-clear-screen
Jump to heading

Do not clear terminal screen when under watch mode.

Watch for file changes and restart process automatically. Only local files from entry point module graph are watched.

--watch-exclude<FILES>optional
Jump to heading

Exclude provided files/patterns from watch mode.

Last updated on

Did you find what you needed?

编辑此页面
Privacy policy