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.

Question: how come function references on the global scope are not closed

See original GitHub issue

https://github.com/eslint/eslint-scope/blob/dbddf14d5771b21b5da704213e4508c660ca1c64/tests/references.js#L211-L263

I’m working on trying to understand this codebase better. But I can’t understand why it works this way:

function a() {}
a();

To me it seems clear that the reference that’s created as part of the call should resolve directly to the function declaration, but it’s not - it remains as an unresolved reference that’s added to the global scope’s through list.

This would cause the no-unused-vars ESLint rule to error on the function declaration, were it not for this code in the linter.

It looks like it works seemingly by chance? ESLint augments the global scope with more variables, and then forcefully resolves any through references against the global variable set, which includes the function declaration as well as the augmented variables.

Is anyone able to explain why this works this way?

Or a better question - when closing the global scope, why doesn’t it attempt to resolve all through references against the variables defined in the global scope?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mysticateacommented, May 15, 2020

Thank you for your question.

…but, I’m not sure why it’s so. I guess that it’s as-is from the original package escope.

Maybe… it couldn’t assume declarations in global scope make variables, because browsers may ignore declarations.

var top = function() {}
top() // ERROR: 'top' is not a function

data:text/html,<script>var top = function(){}; top();</script>

1reaction
mysticateacommented, May 16, 2020

Yes, data:text/html,<script type="module">var top = function(){}; top();</script> works fine.

Also, new knownGlobals: string[] option may be good to make variables and resolve references.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When I make a reference to a global variable, why does ...
A closure is simply a function that uses local variables from a surrounding scope. foo is a closure because it uses open ;...
Read more >
Variable scope, closure
When it wants to access phrase , then there is no phrase locally, so it follows the reference to the outer Lexical Environment...
Read more >
Closures - JavaScript - MDN Web Docs
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).
Read more >
The Problems with Global Variables
The standard states that global variables within a single source file are created and initialized in the order in which they appear in...
Read more >
Python Scope & the LEGB Rule: Resolving Names in Your ...
In this step-by-step tutorial, you'll learn what scopes are, how they work, and how to use them effectively to avoid name collisions in...
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