Skip to content

Commit

Permalink
Docs: show console call with no-restricted-syntax (fixes #7806) (#9376)
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorHom authored and not-an-aardvark committed Oct 6, 2017
1 parent 8da525f commit b4a9dbf
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/rules/no-console.md
Expand Up @@ -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)
Expand Down

0 comments on commit b4a9dbf

Please sign in to comment.