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-var fix isn't safe in the global scope #9520
Comments
Hey @Jamesernator Thanks for submitting an issue. Definitely a good point. With regards to the global variables, and how to ignore, ESLint has ignore lines (https://eslint.org/docs/user-guide/configuring#disabling-rules-with-inline-comments) which could solve the issue if there are only a few globals floating around. Would this work for you? The other part is whether there can be an option that takes in a list of var variable names to ignore those variables and not fix them. Is that correct? |
I think the best thing to do here would be to continue to report top-level global variables, but not autofix them. The fact that autofixing can cause code to break here seems like a bug to me. |
@VictorHom I'm not actually writing new The option I was suggesting was more a workaround so that you could tell eslint to ignore top level vars: /* eslint "no-var": [2, { ignoreGlobal: true }], "prefer-const": [2] */
var someGlobalVar = ...
function foo() {
var localVariable = 10
...
}
// Would become:
var someGlobalVar = ... // Not changed as it breaks
function foo() {
const localVariable = 10 // Changed as it's safe
...
} Now one thing that might be a bit annoying about removing the autofix for global vars is that people might be using it with CommonJS instead of the browser in which case it is safe to autofix global E.g. maybe something like: /* eslint "no-var": [2, { env: 'commonjs' }] */
var x = 10; // Will get fixed because it's safe to fix within commonjs
// ---------
/* eslint "no-var": [2, { env: 'browser' }] */
var x = 10; // Won't get fixed because it's not safe to fix in the browser context
// but it would be nice if it were still reported
// to be fixed manually |
I'm 👍 to disabling the autofix logic for global |
We could probably tighten that constraint so that we still autofix top-level |
I'll work on this. |
I'm not sure what the project's policy is about safety of fixes, but the
no-var
can produce unsafe transformations when the var is global which makes it unsuitable for fixing arbitrary files.This is a minimal example that isn't safe (and here's a demo page link to it):
When fixed by
eslint
it produces:However this isn't equivalent as in the global context
let
/const
statements have different semantics thanvar
, take this code for instance:Running that code will print 20 twice, but in contrast if we run the
eslint
"fixed" version:In this case it will print
10
followed byundefined
, in addition the second script will simply throw an error thatx
is already defined.Now I'm not planning on using globals in this way in new code, but a lot of files in the code base are many years old and I know I've seen
var someGlobalName
being used to add things aswindow.someGlobalName
so running theno-var
on the code base is very likely to break things. It would be nice at least to have an option tono-var
likeignore-global
or something like that to prevent breaking of such code.The text was updated successfully, but these errors were encountered: