no-console

禁止使用 console

💡 hasSuggestions

此规则报告的一些问题可通过编辑器建议手动修复

在设计为在浏览器中执行的 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" has an array of strings which are allowed methods of the console object

此规则的附加正确代码示例以及示例 { "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.

你可能不使用此规则的另一种情况是,如果你想强制执行控制台调用而不是控制台覆盖。例如:

¥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-lineeslint-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 中引入。

资源

ESLint 中文网
粤ICP备13048890号