deno.com
在当前页面

deno lint, linter

命令行用法

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

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

要忽略整个文件的检查,可以在文件顶部添加忽略注释:

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

不稳定选项 Jump to heading

--unstable Jump to heading

--unstable 标志已被弃用。请改用细粒度的 --unstable-* 标志.

--unstable-bare-node-builtins Jump to heading

启用不稳定的裸 Node 内置功能.

--unstable-detect-cjs Jump to heading

在更多情况下将模棱两可的 .js、.jsx、.ts、.tsx 文件视为 CommonJS 模块.

--unstable-sloppy-imports Jump to heading

启用不稳定的通过扩展探测、.js 到 .ts 的解析以及目录探测来解析模块.

检查选项 Jump to heading

--compact Jump to heading

以紧凑格式输出检查结果.

--fix Jump to heading

修复支持修复的规则中的任何检查错误.

--ignore Jump to heading

忽略检查特定的源文件.

--json Jump to heading

以 JSON 格式输出检查结果.

--rules Jump to heading

列出可用规则.

--rules-exclude Jump to heading

排除检查规则.

--rules-include Jump to heading

包含检查规则.

--rules-tags Jump to heading

使用带有标签的规则集.

Options Jump to heading

--allow-import Jump to heading

Short flag: -I

允许从远程主机导入。可选地指定允许的 IP 地址和主机名,必要时包括端口。默认值: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

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

--ext Jump to heading

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

--no-config Jump to heading

禁用自动加载配置文件.

文件监视选项 Jump to heading

--no-clear-screen Jump to heading

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

--watch Jump to heading

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

--watch-exclude Jump to heading

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

可用规则 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 {
  // ...
}

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

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

/**
 * 一些 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,请确保将其添加为文件级别的忽略指令。

你找到需要的内容了吗?

隐私政策