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

References to the explicitly declared variables in the global scope should be resolved as explicitlyResolved #42

Open
RReverser opened this issue Jan 9, 2015 · 9 comments

Comments

@RReverser
Copy link
Member

When I try to analyze smth as simple as

var localVar = globalObj.prop1 + globalObj.prop2;

I get localVar in globalScope.through and it's resolved property equals to null (while the var is obviously local and shouldn't be among unresolved references).

@RReverser
Copy link
Member Author

Simple wrapping code into a IIFE fixes the issue, but I believe it should be "resolved" even when not wrapped.

@Constellation
Copy link
Member

Thank you for your report.
Currently, we take quite conservative analysis. (This is because we use escope for minifier, it should not break the code)
Since it's a global scope, there's a lot of chance to define variables. e.g. window.variable = x.
We cannot determine that reference is absolutely resolved with this variable declaration.
So escope makes references to the global variables as unresolved.

While the minifier requires the very conservative assumption, the other tools (such as linters) requires moderate assumptions. For such cases, escope provides optimistic option.
You can use it like,

scopeManager = escope.analyze(ast, { optimistic: true });

In the above case, localVar should be found in the globalScope.references and it should be marked as resolved with the variable, while globalObj reference is stored in the through and it is noted as not resolved.

Is it good for your use cases?

@Constellation
Copy link
Member

NOTE:
If you specifies optimistic: true, it affects on the with and eval scopes.
If it is preferable to handle the global scope specially, please inform me.
I'll add / extend the escope to handle it :D

@RReverser
Copy link
Member Author

As far as I understand, window.variable can appear inside IIFE as well, so it doesn't really help if you avoid it only in the global scope. On the other hand, those vars that are explicitly defined, have absolutely same semantics in global and function scopes, and you can be always sure that it's defined and "resolved".

Where am I wrong?

@RReverser
Copy link
Member Author

In any case, ability to get them as resolved through flag would be definitely sufficient for my needs, everything else is just pure interest :)

@Constellation
Copy link
Member

As far as I understand, window.variable can appear inside IIFE as well, so it doesn't really help if you avoid it only in the global scope. On the other hand, those vars that are explicitly defined, have absolutely same semantics in global and function scopes, and you can be always sure that it's defined and "resolved".

Thank you! Strictly speaking, there's a difference.
If it's a function scope, basically (unless there's eval. eval is also treated conservatively in escope) all variables are defined in the provided code and we can know the status of the variables. (configurability)
However, the global scope is not the same. The global scope may have the other variables.

For example, we assume that our global object has testing property with {configurable: true}.
In this environment, the following script raises ReferenceError.

var testing = 42;
delete testing;    // We can delete this variable since it's pre-defined as `{configurable: true}`
// or delete globalObject.testing also has the same effect...
console.log(testing);  // raise ReferenceError

So for the minifier, we give up the analysis on the global object now...

But I also think it's a little bit too stict ;)
I think it's useful that analyzing the explicitly defined global variables and mark them (with some property, such as explicitlyResolved). What do you think of?

@RReverser
Copy link
Member Author

Thanks for a detailed explanation!

But I also think it's a little bit too stict ;)

Yeah, I also think that people don't use delete often enough to make those vars unavailable at all just for it.

I think it's useful that analyzing the explicitly defined global variables and mark them (with some property, such as explicitlyResolved). What do you think of?

This could definitely work, thanks!

@Constellation
Copy link
Member

This could definitely work, thanks!

OK, so I'll implement it :)

@Constellation Constellation changed the title Declared var has resolved: null References to the explicitly declared variables in the global scope should be resolved as explicitlyResolved Jan 9, 2015
@RReverser
Copy link
Member Author

Thanks!

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

No branches or pull requests

2 participants