benjamn / benjamn/ast-types

Missing catch binding breaks scope API

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

Description

Using `v0.16.1`.

After parsing the following:

```js
try {} catch { id; }
```

When walking the AST and then looking up the scope at `id` an exception is thrown:

```
null does not match type Pattern
at BaseType.assert (\node_modules\ast-types\lib\types.js:15:19)
at addPattern (\node_modules\ast-types\lib\scope.js:256:28)
at scanScope (\node_modules\ast-types\lib\scope.js:136:17)
at Sp.scan (\node_modules\ast-types\lib\scope.js:109:13)
at Sp.getBindings (\node_modules\ast-types\lib\scope.js:114:14)
at Context.visitIdentifier (\ast.js:30:32)
at Context.invokeVisitorMethod (\node_modules\ast-types\lib\path-visitor.js:283:51)
at PVp.visitWithoutReset (\node_modules\ast-types\lib\path-visitor.js:158:32)
at visitChildren (\node_modules\ast-types\lib\path-visitor.js:204:25)
at PVp.visitWithoutReset (\node_modules\ast-types\lib\path-visitor.js:167:20)
```

Steps to reproduce:

```js
const astTypes = require("ast-types");

const {builders: b, visit} = astTypes;

`try {} catch (e) { void x }`;

const main = b.file(b.program([
// try {}
b.tryStatement(b.blockStatement([]),
// catch
b.catchClause(
/* param: */ null, // b.identifier("e"), // <- adding a catch binding avoids the bug
/* guard: */ null,
// {
b.blockStatement([
// void x
b.expressionStatement(
b.unaryExpression("void", b.identifier("x"))
)
]))
// }
)
]));

visit(main, {
visitIdentifier(path) {
console.log(`at ${path.node.name}`); // 'at x'
console.log(path.scope.getBindings()); // <- throws
return false;
}
});

```

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the failure with the provided ast-types example using a catchClause whose parameter is null. Inspect node_modules/ast-types/lib/scope.js around addPattern and scanScope, then verify that path.scope.getBindings() no longer throws for a catch without a binding.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.