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
no-console rule should document how to enforce calls only via no-restricted-syntax #7806
Comments
I don't think it's worth changing the rule for this use case. Instead, you can use a disable comment to ignore the rule in this location. Here are two ways you can do this: // eslint-disable-next-line no-console
console.error = function (message) {
throw new Error(message);
};
// Or (equivalent)
console.error = function (message) { // eslint-disable-line no-console
throw new Error(message);
}; Of course, you could also just use the |
Yeah, right now we're just selectively disabling the rule. In this case these are warnings given by libraries such as React, though. |
I'm pretty sure I wrote this rule, and the intent really was to disallow calls to console.log and friends, not to eliminate using the console identifier altogether (the docs don't mention it). As such, I think this proposal makes sense if @mockdeep is willing to implement it. I'd suggest both changing the default and adding an option to re-enable the current behavior. I'll champion. |
@nzakas Is your proposal a breaking change? |
I am slightly concerned with changing the default behavior for this proposal because it would prevent code like this from being warned: Promise.resolve()
.then(foo)
.then(console.log) // I put this in for debugging, but I want the rule to warn it In other words, I would personally want the rule to warn whenever I access a property of the On the other hand, I suppose I could get around this by using rules:
no-restricted-properties: [error, {object: console}] |
I'm happy to have a stab at it. Can we handle my case by just checking for assignment to it? If that is what we do, then it shouldn't change for @not-an-aardvark's case. |
@kaicataldo Anything you want to add here since you had 👎'd the original post? Given your 👍 on my suggestion to use disable comments, may I assume that's your preferred alternative? |
@not-an-aardvark your use case wasn't intended to be supported and is not mentioned in the rule docs. You could still get that behavior with a new option. @platinumazure Reducing the number of warnings a rule produces is not a breaking change, so it should be fine. https://github.com/eslint/eslint/blob/master/README.md#semantic-versioning-policy @mockdeep I'd say we want a mode that operates only on CallExpressions for console.something, so basically a filter on the current default behavior. When the new option, lets call it checkAllReferences:true (false by default), it should just not apply that filter. |
This has 3 👍s and a champion, so I'm marking it as accepted. I added the "help wanted" label for now, but if anyone on the team wants to work on it, feel free to remove the label. |
Another use case: we do this kind of thing in tests like this one:
For now, just suppressing with |
I'm trying to help this issue, but I found it may not make sense. When we override var originalLog = console.log;
console.log = function (msg) {
// Custom logic for "msg"
// Then keep original behavior
originLog.call(this, msg);
}; Even we add a new option not to warning for the re-assignment, we still get a warning on the previous |
working on this. |
Removed my 👎. I'm not strongly opposed to this, and it sounds like the rest of the team is on board. |
Some questions were raised about whether ignoring assignment is the best way to handle this. Can I withdraw my 👍 (for now) and we can continue discussing whether there's anything we can sanely do for this rule? In particular, I'm wondering if |
Personally, I think ignoring assignments is fine here since it seems like that was the original intention of the rule. It's true that |
Adding TSC Summary: This issue proposes making |
In today's meeting, the TSC decided against implementing this as an option in the rule, preferring to keep the current behavior. We would, however, like to add a note to the Marking this as an accepted documentation change. |
Editing the title of this issue to reflect the TSC decision. |
What rule do you want to change?
The
no-console
rule.Does this change cause the rule to produce more or fewer warnings?
Fewer warnings.
How will the change be implemented? (New option, new default behavior, etc.)?
I'm thinking it would be default behavior, but could also be configured.
Please provide some example code that this change will affect:
In our development and test environments we have this snippet to suss out warnings in our Javascript:
What does the rule currently do for this code?
The rule currently reports
Unexpected console statement
.What will the rule do after it's changed?
It will not report any errors for assignment to console.
The text was updated successfully, but these errors were encountered: