benjamn / benjamn/ast-types

Scope of a var in catch is catchclause

Open
#188 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
1.2k
Forks
194
Avg merge
22h 43m
Merged PRs (30d)
10

Description

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
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the supplied example with recast and ast-types, then trace the visitVariableDeclarator entry point and inspect path.scope for declarations inside the CatchClause. Done means the example reports b as declared by the FunctionDeclaration and its parent as Program, matching the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.