简中文档
On this page

deno lint, 检查工具

Command line usage

deno lint [OPTIONS] [files]...

Lint JavaScript/TypeScript source code.

deno lint
deno lint myfile1.ts myfile2.js

Print result as JSON:

deno lint --json

Read from stdin:

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

List available rules:

deno lint --rules

To ignore specific diagnostics, you can write an ignore comment on the preceding line with a rule name (or multiple):

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

To ignore linting on an entire file, you can add an ignore comment at the top of the file:

// deno-lint-ignore-file

Linting options Jump to heading

--compact Jump to heading

Output lint result in compact format.

--fix Jump to heading

Fix any linting errors for rules that support it.

--ignore Jump to heading

Ignore linting particular source files.

--json Jump to heading

Output lint result in JSON format.

--rules Jump to heading

List available rules.

--rules-exclude Jump to heading

Exclude lint rules.

--rules-include Jump to heading

Include lint rules.

--rules-tags Jump to heading

Use set of rules with a tag.

Options Jump to heading

--allow-import Jump to heading

Short flag: -I

Allow importing from remote hosts. Optionally specify allowed IP addresses and host names, with ports as necessary. Default value: deno.land:443,jsr.io:443,esm.sh:443,cdn.jsdelivr.net:443,raw.githubusercontent.com:443,user.githubusercontent.com:443.

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

--ext Jump to heading

Specify the file extension to lint when reading from stdin.For example, use jsx to lint JSX files or tsx for TSX files.This argument is necessary because stdin input does not automatically infer the file type.Example usage: cat file.jsx | deno lint - --ext=jsx.

--no-config Jump to heading

Disable automatic loading of the configuration file.

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

要查看支持的规则的完整列表,请访问 规则列表 文档页面。

忽略指令 Jump to heading

文件级别 Jump to heading

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

// deno-lint-ignore-file

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

您还可以指定忽略该文件的原因:

// deno-lint-ignore-file -- 忽略的原因

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

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

// 版权所有 2018-2024 Deno 作者。保留所有权利。MIT 许可证.

/**
 * 一些 JS 文档
 */

// 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 指令,除了第一个以外的指令将被忽略:

// 这是有效的
// deno-lint-ignore-file no-explicit-any no-empty

// 但是这个无效
// deno-lint-ignore-file no-debugger

function foo(): any {
  debugger; // 未被忽略!
}

行级别 Jump to heading

要忽略特定的诊断,请在有问题的行前使用 // 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 -- 忽略的原因
function foo(): any {
  // ...
}

忽略 ban-unused-ignore 本身 Jump to heading

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` 没有使用,但由于忽略了 `ban-unused-ignore`,
// 所以不会返回任何诊断
console.log(42);

请注意,忽略 ban-unused-ignore 本身仅通过文件级忽略指令有效。这意味着像 // deno-lint-ignore ban-unused-ignore 这样的行级指令完全无效。如果您出于某些特殊原因想要忽略 ban-unused-ignore,请确保将其作为文件级忽略指令添加。

更多关于 linting 和格式化的信息 Jump to heading

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

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

编辑此页面
隐私政策