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

Content of try block gets a different scope from parent #86

Closed
wolfgang42 opened this issue Nov 1, 2015 · 3 comments
Closed

Content of try block gets a different scope from parent #86

wolfgang42 opened this issue Nov 1, 2015 · 3 comments

Comments

@wolfgang42
Copy link

Given the following JavaScript:

var v;
try {
    v = 1;
} catch(e) {}

escope thinks that the v inside the try block is in the global scope:

var local_v;
try {
    global_v = 1;
} catch(e) {}

I discovered this when I tried to use escope to refactor a try...catch and found that the code broke because the code inside the try was still using the old variable name.

Here is a test case which does not pass:

expect = require('chai').expect
esprima = require 'esprima'
escope = require '..'

describe 'try', ->
    it 'finds a variable in the parent scope', ->
        ast = esprima.parse """
        var v;
        try {
            v = 1;
        } catch(e) {}
        """

        scopeManager = escope.analyze ast

        globalScope = scopeManager.scopes[0]
        expect(globalScope.type).to.be.equal 'global'
        expect(globalScope.variables).to.have.length 0
        expect(globalScope.references).to.have.length 0

I believe this is related to #57 but I'm not certain.

@wolfgang42
Copy link
Author

#22 also looks related as well.

@michaelficarra
Copy link
Member

I don't see anything wrong with this.

expect(globalScope.variables).to.have.length 1

@wolfgang42
Copy link
Author

The problem is that the identifier v is viewed as having two different scopes, one inside the try and one outside of it. However it's the same v in both places.

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