question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

no-var fix isn't safe in the global scope

See original GitHub issue

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):

var x = 10;

When fixed by eslint it produces:

let x = 10;

However this isn’t equivalent as in the global context let/const statements have different semantics than var, take this code for instance:

<script>
    var x = 10;
</script>
<script>
    var x = 20;
</script>
<script>
    console.log(x);
    console.log(window.x);
</script>

Running that code will print 20 twice, but in contrast if we run the eslint “fixed” version:

<script>
    let x = 10;
</script>
<script>
    let x = 20;
</script>
<script>
    console.log(x);
    console.log(window.x);
</script>

In this case it will print 10 followed by undefined, in addition the second script will simply throw an error that x 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 as window.someGlobalName so running the no-var on the code base is very likely to break things. It would be nice at least to have an option to no-var like ignore-global or something like that to prevent breaking of such code.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
not-an-aardvarkcommented, Oct 26, 2017

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.

0reactions
mysticateacommented, Oct 27, 2017

I’ll work on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why won't this variable reference to a non-local scope resolve?
In the sample code testNum is not global . Try using the key word nonlocal instead. This assumes that you are using python3....
Read more >
How to Idiomatically Use Global Variables in Rust - SitePoint
You've heard that you should avoid using global variables in Rust. But they can be useful for configuration or managing state in your...
Read more >
Variable Scope | Fix error: 'yourVariable' was not declared in ...
A variable with global scope, known as a global variable can be used anywhere in your program. Now, you might be tempted to...
Read more >
4. Variables - JavaScript: The Definitive Guide, 5th Edition [Book]
A global variable has global scope; it is defined everywhere in your JavaScript code. On the other hand, variables declared within a function...
Read more >
Documentation - Global .d.ts - TypeScript
Global Libraries. A global library is one that can be accessed from the global scope (i.e. without using any form of import )....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found