简中文档
On this page

deno fmt, 代码格式化

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

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

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.

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

支持的文件类型 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"] 配置选项。

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

关于代码检查和格式化的更多信息 Jump to heading

有关 Deno 中代码检查和格式化的更多信息,以及这两个工具之间的区别,请访问我们基础知识部分的 代码检查和格式化 页面。

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

编辑此页面
隐私政策