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

Scope of a var in catch is catchclause #188

Open
robz opened this issue Oct 19, 2016 · 2 comments
Open

Scope of a var in catch is catchclause #188

robz opened this issue Oct 19, 2016 · 2 comments

Comments

@robz
Copy link

robz commented Oct 19, 2016

A var that is declared in catch clauses should have the scope that the try/catch is in, but instead .scope returns a scope that is the catch clause itself.

For example, this code:

var recast = require("recast");
var types = require("ast-types");

var ast = recast.parse(`
  function f() {
    try {
      var a = 4;
    } catch (e) {
      var b = 3;
    }
  }
`);

types.visit(ast, {
  visitVariableDeclarator: function(path) {
    var name = path.node.id.name;
    var scope = path.scope;
    console.log(name, scope.node.type, scope.declares(name));
    scope = scope.parent;
    console.log(name, scope.node.type, scope.declares(name));
    return false;
  }
});

Outputs this:

a FunctionDeclaration true
a Program false
b CatchClause false
b FunctionDeclaration true

But I think it should output this:

a FunctionDeclaration true
a Program false
b FunctionDeclaration true
b Program false
@glmdgrielson
Copy link

Actually, I just tried

(function () {
	try {
		var a = 4;
	} catch (_) {
		var b = 3;
	}
	return typeof b !== "undefined";
})()

and Node is giving me false. So it should probably output:

a FunctionDeclaration true
a Program false
b CatchClause true
b FunctionDeclaration false

@bnjmnt4n
Copy link
Contributor

bnjmnt4n commented Jan 7, 2019

Seems to me that although the variable declaration for b is bound to the function's scope, b is not explicitly defined and hence will still be undefined. That is to say, I believe your original code should be the correct behaviour.

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

3 participants