deno.com
本页内容

deno lint, 代码检查工具

命令行用法

deno lint [OPTIONS] [files]...

检查 JavaScript/TypeScript 源代码。

deno lint
deno lint myfile1.ts myfile2.js

以 JSON 格式打印结果

deno lint --json

从标准输入读取

cat file.ts | deno lint -
cat file.ts | deno lint --json -

列出可用规则

deno lint --rules

要忽略特定的诊断信息,你可以在前一行写一个忽略注释,并附带规则名称(或多个)。

// deno-lint-ignore no-explicit-any
// deno-lint-ignore require-await no-empty

要忽略整个文件的 linting,你可以在文件顶部添加一个忽略注释。

// deno-lint-ignore-file

Linting 选项 跳转到标题

--compact 跳转到标题

以紧凑格式输出 lint 结果。

--fix 跳转到标题

修复支持的规则中的任何 linting 错误。

--ignore 跳转到标题

忽略对特定源文件的 linting。

--json 跳转到标题

以 JSON 格式输出 lint 结果。

--rules 跳转到标题

列出可用规则。

--rules-exclude 跳转到标题

排除 lint 规则。

--rules-include 跳转到标题

包含 lint 规则。

--rules-tags 跳转到标题

使用带有标签的规则集。

选项 跳转到标题

--allow-import 跳转到标题

短标记:-I

允许从远程主机导入。可选地指定允许的 IP 地址和主机名,必要时包括端口。默认值:deno.land:443,jsr.io:443,esm.sh:443,cdn.jsdelivr.net:443,raw.githubusercontent.com:443,user.githubusercontent.com:443

--config 跳转到标题

短标记:-c

配置 Deno 的不同方面,包括 TypeScript、linting 和代码格式化。通常配置文件名为 deno.jsondeno.jsonc 并会自动检测;在这种情况下,不需要此标志。

--deny-import 跳转到标题

拒绝从远程主机导入。可选地指定拒绝的 IP 地址和主机名,必要时包括端口。

--ext 跳转到标题

从标准输入读取时,指定要进行 lint 的文件扩展名。例如,使用 jsx 对 JSX 文件进行 linting,或使用 tsx 对 TSX 文件进行 linting。此参数是必需的,因为标准输入无法自动推断文件类型。使用示例:cat file.jsx | deno lint - --ext=jsx

--no-config 跳转到标题

禁用自动加载配置文件。

--permit-no-files 跳转到标题

如果未找到文件,则不返回错误代码。

文件监视选项 跳转到标题

--no-clear-screen 跳转到标题

在监视模式下不清除终端屏幕。

--watch 跳转到标题

监视文件更改并自动重启进程。仅监视来自入口模块图的本地文件。

--watch-exclude 跳转到标题

从监视模式中排除提供的文件/模式。

可用规则 跳转到标题

有关支持规则的完整列表,请访问规则列表文档页面。

忽略指令 跳转到标题

文件级别 跳转到标题

要忽略整个文件,请在文件顶部使用 // deno-lint-ignore-file

// deno-lint-ignore-file

function foo(): any {
  // ...
}

你也可以指定忽略文件的原因。

// deno-lint-ignore-file -- reason for ignoring

function foo(): any {
  // ...
}

忽略指令必须放置在第一个语句或声明之前。

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

/**
 * Some JS doc
 */

// deno-lint-ignore-file

import { bar } from "./bar.js";

function foo(): any {
  // ...
}

你也可以在整个文件中忽略某些诊断信息。

// deno-lint-ignore-file no-explicit-any no-empty

function foo(): any {
  // ...
}

如果存在多个 // deno-lint-ignore-file 指令,则除第一个之外的所有指令都将被忽略。

// This is effective
// deno-lint-ignore-file no-explicit-any no-empty

// But this is NOT effective
// deno-lint-ignore-file no-debugger

function foo(): any {
  debugger; // not ignored!
}

行级别 跳转到标题

要忽略特定的诊断信息,请在有问题行的前一行使用 // deno-lint-ignore <codes...>

// deno-lint-ignore no-explicit-any
function foo(): any {
  // ...
}

// deno-lint-ignore no-explicit-any explicit-function-return-type
function bar(a: any) {
  // ...
}

你必须指定要忽略的规则名称。

你也可以指定忽略该诊断信息的原因。

// deno-lint-ignore no-explicit-any -- reason for ignoring
function foo(): any {
  // ...
}

忽略 ban-unused-ignore 自身 跳转到标题

deno lint 提供了 ban-unused-ignore 规则,它会检测那些从未抑制过特定诊断信息的忽略指令。当你重构代码后,想要发现不再需要的忽略指令时,这会很有用。

然而,在少数情况下,你可能希望忽略 ban-unused-ignore 规则本身。一个典型的例子是处理自动生成的文件;为某些规则添加文件级忽略指令是有意义的,并且在这种情况下几乎没有必要通过 ban-unused-ignore 来检测未使用的指令。

如果你想为整个文件抑制此规则,可以像往常一样使用 // deno-lint-ignore-file ban-unused-ignore

// deno-lint-ignore-file ban-unused-ignore no-explicit-any

// `no-explicit-any` isn't used but you'll get no diagnostics because of ignoring
// `ban-unused-ignore`
console.log(42);

请注意,忽略 ban-unused-ignore 本身仅通过文件级忽略指令起作用。这意味着每行指令,如 // deno-lint-ignore ban-unused-ignore,完全不起作用。如果你出于特殊原因需要忽略 ban-unused-ignore,请务必将其添加为文件级忽略指令。

更多关于 linting 和格式化 跳转到标题

有关 Deno 中 linting 和格式化以及这两种实用工具之间差异的更多信息,请访问我们基础部分中的Linting 和格式化页面。

您找到所需内容了吗?

隐私政策