On this page
deno lint
Deno 自带一个内置的 linter,用于分析您的代码中可能存在的错误、 漏洞以及风格问题。有关更广泛的概述,请参阅 Linting and Formatting。
基本用法 Jump to heading
检查当前目录中的所有 TypeScript 和 JavaScript 文件:
deno lint
检查特定文件或目录:
deno lint src/ main.ts
监视模式 Jump to heading
当文件发生变化时自动重新检查:
deno lint --watch
在 CI 中使用 Jump to heading
deno lint 在发现违规时会以非零状态码退出,因此适用于 CI 流水线:
deno lint
deno fmt --check
deno test
可用规则 Jump to heading
Deno 的 linter 包含 100 多条规则。查看所有可用规则:
deno lint --rules
如需带有文档的完整列表,请访问 lint rules 参考。
在 deno.json 中配置规则 Jump to heading
在您的 deno.json 中自定义启用哪些规则:
{
"lint": {
"rules": {
"tags": ["recommended"],
"include": ["ban-untagged-todo"],
"exclude": ["no-unused-vars"]
}
}
}
tags— 要启用的规则集。可用标签:recommended、freshinclude— 要启用的额外单条规则exclude— 即使被标签包含,也要禁用的规则
有关所有可用选项,请参阅 Configuration 页面。
包含和排除文件 Jump to heading
在 deno.json 中指定要检查的文件:
{
"lint": {
"include": ["src/"],
"exclude": ["src/testdata/", "src/generated/**/*.ts"]
}
}
您也可以从命令行排除文件:
deno lint --ignore=dist/,build/
Lint 插件 Jump to heading
您可以使用 lint plugins 通过自定义规则扩展 linter。
忽略指令 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,请务必将其作为
文件级别忽略指令添加。
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
--compactOutput lint result in compact format.
--fixFix any linting errors for rules that support it.
--ignore<ignore>Ignore linting particular source files.
--jsonOutput lint result in JSON format.
--rulesList available rules.
--rules-exclude<rules-exclude>Exclude lint rules.
--rules-include<rules-include>Include lint rules.
Options Jump to heading
--allow-import, -I<IP_OR_HOSTNAME>optionalAllow 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,raw.esm.sh:443,cdn.jsdelivr.net:443,raw.githubusercontent.com:443,gist.githubusercontent.com:443.
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.
--deny-import<IP_OR_HOSTNAME>optionalDeny importing from remote hosts. Optionally specify denied IP addresses and host names, with ports as necessary.
--ext<EXT>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-configDisable automatic loading of the configuration file.
--permit-no-filesDon't return an error code if no files were found.
File watching options Jump to heading
--no-clear-screenDo not clear terminal screen when under watch mode.
--watchWatch for file changes and restart process automatically. Only local files from entry point module graph are watched.
--watch-exclude<FILES>optionalExclude provided files/patterns from watch mode.