deno.com
在当前页面

deno fmt, 代码格式化

命令行用法

o fmt [选项] [文件]...

自动格式化各种文件类型。

deno fmt myfile1.ts myfile2.ts

支持的文件类型有:

JavaScript, TypeScript, Markdown, JSON(C) 和 Jupyter Notebooks

支持的文件类型,但需要相应的不稳定标志(见格式化选项):

HTML, CSS, SCSS, SASS, LESS, YAML, Svelte, Vue, Astro 和 Angular

从标准输入格式化并写入标准输出:

cat file.ts | deno fmt -

检查文件是否已格式化:

deno fmt --check

通过在代码前添加忽略注释来忽略格式化:

// deno-fmt-ignore

通过在文件顶部添加忽略注释来忽略格式化文件:

// deno-fmt-ignore-file
了解更多: https://docs.deno.com/go/fmt

Options Jump to heading

--config Jump to heading

Short flag: -c

配置 deno 的不同方面,包括 TypeScript、代码检查和代码格式化 通常配置文件会被称为 deno.jsondeno.jsonc 并 自动检测;在这种情况下,此标志不是必需的。 文档:https://docs.deno.com/go/config.

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

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

你找到需要的内容了吗?

隐私政策