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 页面 了解所有可用选项。

包含和排除文件 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
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"] 配置选项。

Note

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
<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

Options Jump to heading

--config Jump to heading

Short flag: -c

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 Jump to heading

Check if the source files are formatted.

--ext Jump to heading

Set content type of the supplied file.

--fail-fast Jump to heading

Stop checking files on first format error.

--ignore Jump to heading

Ignore formatting particular source files.

--indent-width Jump to heading

Define indentation width [default: 2]

--line-width Jump to heading

Define maximum line width [default: 80]

--no-semicolons Jump to heading

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

--prose-wrap Jump to heading

Define how prose should be wrapped [default: always]

--single-quote 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 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 Jump to heading

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

--watch-exclude Jump to heading

Exclude provided files/patterns from watch mode.

Last updated on

你找到了你需要的东西吗?

编辑此页面
隐私政策