eslint/no-console Restriction
作用
禁止使用 console。
为什么不好?
在旨在浏览器中执行的 JavaScript 中,避免使用 console 上的方法被视为最佳实践。此类消息被认为是用于调试目的,因此不适合发布给客户端。通常,在使用 console 调用之前应该将其剥离,然后再推送到生产环境。
示例
此规则的 错误 代码示例:
javascript
console.log("记录一条调试级别消息。");
console.warn("记录一条警告级别消息。");
console.error("记录一条错误级别消息。");
console.log = foo();此规则的 正确 代码示例:
javascript
// 自定义 console
Console.log("你好,世界!");配置
此规则接受一个具有以下属性的配置对象:
allow
类型:string[]
默认值:[]
allow 选项允许将给定的 console 方法列表作为此规则的例外使用。
假设选项配置为 { "allow": ["info"] },则规则的行为如下:
此选项的 错误 代码示例:
javascript
console.log("foo");此选项的 正确 代码示例:
javascript
console.info("foo");如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"no-console": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"no-console": "error",
},
});bash
oxlint --deny no-console版本
此规则在 v0.0.13 中新增。
