Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

no-console gives a false positive when there is a console local #3633

Closed
alfaproject opened this issue Jan 10, 2018 · 5 comments
Closed

no-console gives a false positive when there is a console local #3633

alfaproject opened this issue Jan 10, 2018 · 5 comments

Comments

@alfaproject
Copy link

Bug Report

  • TSLint version: 5.8.0
  • TypeScript version: 2.5.3
  • Running TSLint via: CLI

TypeScript code being linted

f(console) {
  console.log();
}

with tslint.json configuration:

{
  "rules": {
    "no-console": true
  }
}

Actual behavior

ERROR: 2:3 no-console Calls to 'console.log' are not allowed.

Expected behavior

No error

@johnwiseheart
Copy link
Contributor

This is kind of a bug I guess, but I think that it is not advisable to use a variable named console as it will shadow the global console variable. My recommendation is not to use console as a variable name.

@alfaproject
Copy link
Author

That's the whole point of it tho. I don't want people to use the global console, and therefore that name should then be available to use freely.

@johnwiseheart
Copy link
Contributor

johnwiseheart commented Jul 28, 2018

Whether you want people to use the global console or not, a variable named ‘console’ will still shadow the global unless you do something funky like unsetting the function. It’s a very specific use case and as such does not have a place in the main tslint ruleset, which is intended to be fairly specific and opinionated. That said, you could write your own custom rule to lint this use case.

@alfaproject
Copy link
Author

alfaproject commented Jul 28, 2018

Yes, I have done that for other globals like document, window, etc. But the console one is here in this repository, so I thought you guys would like to fix it. Guess it doesn't matter then.

Our use case is that we don't want people to use globals in a server-side rendering context, but global names like document and Event are too generic (as an example).

ESLint has a similar rule for what we need: https://eslint.org/docs/rules/no-restricted-globals
We implemented something like that, but it's not perfect, that's why I didn't do a PR.

@alfaproject
Copy link
Author

I guess we finally got that rule #3824

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants