You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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.
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:
Outputs this:
But I think it should output this:
The text was updated successfully, but these errors were encountered: