no-console
禁止使用 console
此规则报告的一些问题可通过编辑器 建议 手动修复
在设计为在浏览器中执行的 JavaScript 中,通常认为避免在 console 上使用方法是最佳实践。这类消息通常被认为是用于调试目的,因此不适合发送给客户端。一般来说,使用 console 的调用应该在推送到生产环境之前被去除。
🌐 In JavaScript that is designed to be executed in the browser, it’s considered a best practice to avoid using methods on console. Such messages are considered to be for debugging purposes and therefore not suitable to ship to the client. In general, calls using console should be stripped before being pushed to production.
console.log("Made it here.");
console.error("That shouldn't have happened.");
规则详情
🌐 Rule Details
此规则不允许对 console 对象的方法进行调用或赋值。
🌐 This rule disallows calls or assignments to methods of the console object.
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/* eslint no-console: "error" */
console.log("Log a debug level message.");
console.warn("Log a warn level message.");
console.error("Log an error level message.");
console.log = foo();
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/* eslint no-console: "error" */
// custom console
Console.log("Hello world!");
选项
🌐 Options
此规则有一个异常对象选项:
🌐 This rule has an object option for exceptions:
"allow"有一个字符串数组,这些字符串是console对象允许的方法
此规则的其他正确代码示例,带有示例 { "allow": ["warn", "error"] } 选项:
🌐 Examples of additional correct code for this rule with a sample { "allow": ["warn", "error"] } option:
/* eslint no-console: ["error", { allow: ["warn", "error"] }] */
console.warn("Log a warn level message.");
console.error("Log an error level message.");
何时不使用
🌐 When Not To Use It
然而,如果你使用的是 Node.js,console 用于向用户输出信息,因此并不严格用于调试目的。如果你在为 Node.js 开发,那么你很可能不希望启用此规则。
🌐 If you’re using Node.js, however, console is used to output information to the user and so is not strictly used for debugging purposes. If you are developing for Node.js then you most likely do not want this rule enabled.
另一个可能不使用此规则的情况是,如果你想强制执行 console 调用而不是 console 覆盖。例如:
🌐 Another case where you might not use this rule is if you want to enforce console calls and not console overwrites. For example:
/* eslint no-console: ["error", { allow: ["warn"] }] */
console.error = function (message) {
throw new Error(message);
};
在上述示例中使用 no-console 规则时,ESLint 会报告一个错误。对于上述示例,你可以禁用该规则:
🌐 With the no-console rule in the above example, ESLint will report an error. For the above example, you can disable the rule:
// eslint-disable-next-line no-console
console.error = function (message) {
throw new Error(message);
};
// or
console.error = function (message) { // eslint-disable-line no-console
throw new Error(message);
};
然而,你可能不想手动添加 eslint-disable-next-line 或 eslint-disable-line。你可以使用 no-restricted-syntax 规则实现仅在控制台调用时接收错误的效果:
🌐 However, you might not want to manually add eslint-disable-next-line or eslint-disable-line. You can achieve the effect of only receiving errors for console calls with the no-restricted-syntax rule:
{
"rules": {
"no-console": "off",
"no-restricted-syntax": [
"error",
{
"selector": "CallExpression[callee.object.name='console'][callee.property.name!=/^(log|warn|error|info|trace)$/]",
"message": "Unexpected property on console object was called"
}
]
}
}
相关规则
版本
此规则是在 ESLint v0.0.2 中引入。