Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: show console call with no-restricted-syntax (fixes #7806) #9376

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/rules/no-console.md
Original file line number Diff line number Diff line change
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