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

Sub-option to allow console.warn and friends in no-console #2621

Closed
hgl opened this issue May 27, 2015 · 14 comments
Closed

Sub-option to allow console.warn and friends in no-console #2621

hgl opened this issue May 27, 2015 · 14 comments
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion triage An ESLint team member will look at this issue soon

Comments

@hgl
Copy link

hgl commented May 27, 2015

console.warn and console.error are pretty useful to help users report errors even in production code.

I wonder if it makes sense to add a sub-option to only report error on using console.log, which is mainly used for development debugging?

@ilyavolodin
Copy link
Member

The rule was create to guard any output to console in production code, as it's considered a bad practice. There is no real difference between console.warn and console.log from my perspective. I think if you want to output to console, you should just disable this rule.

@ilyavolodin ilyavolodin added the triage An ESLint team member will look at this issue soon label May 27, 2015
@gyandeeps
Copy link
Member

Agree with @ilyavolodin .
If its an exception just once then you can actually disable the rule for that particular line

console.error("hi") // eslint-disable-line no-console

@hgl
Copy link
Author

hgl commented May 27, 2015

The problem is that my project uses console.warn and console.error in production code, but I do want to check for console.log in case I forget to remove them after debugging.

Having to to use comment every time I use console.warn and console.error doesn't sound very optimal.

It's pretty common for a browser extension to dump indexDB error to the console to help users report bugs for example.

@xjamundx
Copy link
Contributor

My thought would be to create a custom rule for your project to do this. I don't think it would be something that we'd want to have globally, but it would be really easy to add this exception for your project. Do you want some help setting it up?

@hgl
Copy link
Author

hgl commented May 27, 2015

Do you want some help setting it up?

That would be great. Thanks. Does the doc cover it?

@xjamundx
Copy link
Contributor

Take a look at the existing no-console rule.

You can add the following in order to make it only warn on console.log statements you need to update the if statement on line 18 to look like this:

if (node.object.name === "console" && node.property.name === "log") {
  1. Save that modified file into a rules/ directory inside your project as no-console-log.js.
  2. Open up your .eslintrc file and then add no-console-log: 2 to the rules section to enable the rule.
  3. When you run eslint make sure to use the --rulesdir flag to specify that it should look in the rules/ directory

More info here:
http://eslint.org/docs/user-guide/command-line-interface#--rulesdir

More advanced docs:
https://gist.github.com/jareware/7179093

Good luck!

@ilyavolodin
Copy link
Member

We have a whole section in the documentation dedicated to rule creation here: http://eslint.org/docs/developer-guide/working-with-rules
Let us know if you run into some problems.

@xjamundx
Copy link
Contributor

@hgl feel free to join the chat room at https://gitter.im/eslint/eslint if you get stuck

@hgl
Copy link
Author

hgl commented May 28, 2015

Thanks guys. And thank you @xjamundx for taking the time writing the step by step instructions.

I will try it out.

@Globegitter
Copy link

I agree with @hgl that there should be options that you can allow warn and/or err. For example we allow console.warn in our production code but not console.log or console.error. Also other third-party libraries such as ReactJS or pixi.js make use of console.warn in some way or another. So I do think there is enough of a real-world use-case to reconsider the current form of this rule.

Of course I also do respect the choice of the main contributors if that is set in stone.

@joeybaker
Copy link
Contributor

For what it's worth, I published a plugin that just checks for console.log, using @xjamundx's advice above. https://github.com/joeybaker/eslint-plugin-no-console-log

@IanVS
Copy link
Member

IanVS commented Sep 10, 2015

👍 Thanks for reporting back!

@Diokuz
Copy link

Diokuz commented Sep 30, 2015

+1 for no-console-log

because, console.log cannot be used in production - its almost always developer, who forger remove debugger code. But console.error and .warn can be used, and it used in production.

@allenmanning
Copy link

+1 for configuring which ones fail

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 7, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion triage An ESLint team member will look at this issue soon
Projects
None yet
Development

No branches or pull requests

9 participants