no-console

禁止使用 console

在设计为在浏览器中执行的 JavaScript 中,避免在 console 上使用方法被认为是最佳实践。此类消息被认为是出于调试目的,因此不适合发送给客户端。一般来说,使用 console 的调用应该在被推送到生产环境之前被剥离。

console.log("Made it here.");
console.error("That shouldn't have happened.");

规则详情

此规则不允许调用或分配 console 对象的方法。

此规则的错误代码示例:

/* 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();

此规则的正确代码示例:

/* eslint no-console: "error" */

// custom console
Console.log("Hello world!");

选项

iUBjM40SPBc84UHpTbhTXk6VVyhYwzeK7QgP40HJsg3t5Yxl/eCfaRMeqSPi0K1G

    hAjk05IEKh++biF/2hMM3Z8p4yA/izw59AptzujO3Vi6co0pizwUp8Zks0RCdZlUXGpMHkEpWEO0oCoVd3GPrNVk3dTMe/wWozDCe7jyUI6nkFgRp8ynK/3CFQMqxC4rNQtIP5yGAktGbutjX8O2K4CXlRaSwyD0tXu7beP287o=

PFxbUUAhGbZwFfOCdZNtHO82/099XzS21PJsBCLAZhv9S9H5zMdVtFLEi8ZuC1KeF6jzDcjwdBaHgJu1crOrhgAWDqu+FvgSjpjWeDdLaJyKwyzeScba3mP/g17Do5O5BDy82/RDllLGyHVzDGXxhQ==

/* eslint no-console: ["error", { allow: ["warn", "error"] }] */

console.warn("Log a warn level message.");
console.error("Log an error level message.");

何时不使用

tvDWiu/+lWlm7u8rpz5i8DYrZcAU34pZdODlZN9nt4cyXkMqu1ALTh90nQ13lY3IoPXUWCQAf8MbebOxwIbns5IJMkHDEdG9lubmgf4pVTyYytlDivpvC/rHgOAk9s9pcUhbkLwFG9WzWMQ6+R8596Wl5G3DDGy/sfTF8PYWjXVxLRjF5TdhANCBlrydJ2pLwdaDIrATBh8L5W70nDtFkUbttmLeSVu2q585AC9QfK9eJOk+DIemQt7L3PjQoiC4D0CSA8fuw1VInm2CUhGK/r5kPbek2i4eXSzy+J+gp9A=

1vIdgE6vSF7xdBz3wUtgnOXe+pLCV4Xb+tYXy46TI22PWWYh7JfV4kx337BtPjaxUckO02duKzP1TTPU3jdZEFZ9B0J/K7pXv1UeKok2XQyvO2BHgdhnYF751YgA2emL4cM223SLZZVx5QaUQhl7KpH7BlukgA0lgLi3bPCCZ9s=

/* eslint no-console: ["error", { allow: ["warn"] }] */
console.error = function (message) {
  throw new Error(message);
};

Po4KyJCD3QKQh/lTNnDkRtoUsb/z1OUFjABTnRHQ7cMLErM8y2QbWekmFhqzE3bVAhlbDS9/V2ybxsf18tQtGBb6Ye+0i93qLxUoPlwJSvY1ZdjttG1tM5dy4qt4/60kkZk39HWbu5UiuGb6zs5e+WQgb2GAOPWy3dPYYi8wVSY=

// 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);
};

iX4JlQata4Ewqd8msEwmhybP3+JdIqOs9cpDvN4vw6mlnLj0fs0Ob4lVtl2vC1NIczs0lhaHACbv2yfvExniDtT4JDSGhzVljYHDZOEeRhuxCSu+O+NI2PEpCSmCki8BwVL+B8KH/TyozqSu0pjn4z27Zr7at7SQ5zPibrJclOefrrPYA2mFZd9VvmhaTJvmUeqDGt8brrn38pep9JVxn3jU8MKXayh9uRlymTiGHqIgamaRUAjXX9dfKScmuJzjmRBAyaz/cA+yll2qnP1QnaqAawJbbJUs14RwyrChT6M=

{
    "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"
            }
        ]
    }
}