跳至主要内容

deno lint,代码检查器

Deno 自带一个用于 JavaScript 和 TypeScript 的内置代码检查器。

# lint all JS/TS files in the current directory and subdirectories
deno lint
# lint specific files
deno lint myfile1.ts myfile2.ts
# lint all JS/TS files in specified directory and subdirectories
deno lint src/
# print result as JSON
deno lint --json
# read from stdin
cat file.ts | deno lint -

有关更多详细信息,请运行 deno lint --help

可用规则

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

忽略指令

文件

要忽略整个文件,// deno-lint-ignore-file 指令应放在文件顶部

// deno-lint-ignore-file

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

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

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

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

// Copyright 2020 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 <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 {
// ...
}

配置

从 Deno v1.14 开始,可以使用 配置文件 或以下 CLI 标志自定义 linter

  • --rules-tags - 将运行的标签名称列表。空列表将禁用所有标签,并且只会使用来自 include 的规则。默认为 "recommended"。

  • --rules-exclude - 将从配置的标签集中排除的规则名称列表。即使相同的规则在 include 中,它也会被排除;换句话说,--rules-exclude--rules-include 优先级更高。

  • --rules-include - 将运行的规则名称列表。如果相同的规则在 exclude 中,它将被排除。