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

Scopes: Add 'file' scope #84

Closed
wants to merge 2 commits into from
Closed

Scopes: Add 'file' scope #84

wants to merge 2 commits into from

Conversation

szuend
Copy link
Collaborator

@szuend szuend commented May 21, 2024

This is straw-man PR to discuss whether we want to distinguish "global" and "file" ("script" or "module") scope.

An example of this may be the following:

// a.js
var foo = 'a';
const bar=  'b';

Both Firefox DevTools and Chrome DevTools would show different scopes for a and b. Specifically a would be in the "global" scope as it gets hoisted to globalThis/window. Whereas b would be only in the "script" scope.

This could simplify handling of the "global" scope across multiple authored files. To expand on the initial example let's add another file:

// b.js
var fooB = 'foo';
const barB = 'bar';

debugger;

And the combined bundle:

// bundle.js
var foo = 'a';
const bar=  'b';
var fooB = 'foo';
const barB = 'bar';

debugger;

If we pause on the debugger statement then the expected scope chain is:

  • script
    • barB: 'bar'
  • global
    • foo: 'a'
    • fooB: 'foo'

Since the global scope is combined. Generators can achieve this by generating the following "generated ranges" for the bundle:

// bundle.js
var foo = 'a';      | (1)  | (2)  | (3)  
const bar=  'b';    |      |      |
var fooB = 'foo';   |      |             | (4)
const barB = 'bar'; |      |             |
                    |      |             |
debugger;           |      |             |  

(1) and (2) are the respective "global" scopes. (3) is the file scope of a.js and (4) is the file scope of b.js.

@szuend szuend requested a review from hbenl May 21, 2024 08:18
Copy link
Collaborator

@hbenl hbenl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whether we want to distinguish "global" and "script" scope

Yes, I think we should do so. The proposed solution makes sense to me.

@jkup jkup self-requested a review May 30, 2024 16:41
@jkup
Copy link
Collaborator

jkup commented May 30, 2024

Change looks good. @nicolo-ribaudo mentioned in the monthly meeting we should consider if we need both script and module or can get away with just global and file

@szuend szuend changed the title Scopes: Add 'script' and 'module' scope Scopes: Add 'file' scope Jun 4, 2024
@szuend
Copy link
Collaborator Author

szuend commented Jun 4, 2024

As discussed in the meeting, consolidated the script / module scope into a single file scope.

@jkup
Copy link
Collaborator

jkup commented Jun 24, 2024

Closing in favor of replacing ScopeKind with an index into the names array. That way JS can store "module", "function" and other languages can make their own.

@jkup jkup closed this Jun 24, 2024
@szuend szuend deleted the scope-kind branch November 5, 2024 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants