From b4a9dbf4c3a2eb390e4227ad1d4b3b800734b7ed Mon Sep 17 00:00:00 2001 From: Victor Hom Date: Fri, 6 Oct 2017 00:24:48 -0400 Subject: [PATCH] Docs: show console call with no-restricted-syntax (fixes #7806) (#9376) --- docs/rules/no-console.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/rules/no-console.md b/docs/rules/no-console.md index ab58cb846a8..1c36a85f974 100644 --- a/docs/rules/no-console.md +++ b/docs/rules/no-console.md @@ -49,6 +49,46 @@ console.error("Log an error level message."); 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: + +```js +/*eslint no-console: ["error", { allow: ["warn"] }] */ +console.error = function (message) { + throw new Error(message); +}; +``` + +With the `no-console` rule in the above example, ESLint will report an error. For the above example, you can disable the rule: + +```js +// 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); +}; +``` + +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: + +```json +{ + "rules": { + "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" + } + ] + } +} +``` + ## Related Rules * [no-alert](no-alert.md)