benjamn / benjamn/ast-types

Support for immutable AST modification

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

Description

Right now you can use `types.visit` which will traverse over the AST, and then you can mutate the AST in whatever way you want.

This works fine, and most of the time it's what you want, but sometimes you want to return a *new* AST which is exactly like an existing AST but with certain things changed.

So I propose a new `types.map` function, which has a similar API as `types.visit`, except that in addition to visiting the existing AST, it also returns a fresh new AST:

```
var newAST = types.map(oldAST, {
visitIdentifier: function (path) {
this.traverse(path);

return {
type: "Identifier",
name: path.node.name.toUpperCase(),
loc: path.node.loc
};
}
});
```

In the above example, the `types.map` function will traverse over `oldAST`, and when it encounters an `Identifier` it will call the `visitIdentifier` method, which returns a new `Identifier`.

Therefore the `newAST` variable contains an AST which is exactly the same as `oldAST` except that all `Identifier`s have been replaced by the return value of the `visitIdentifier` method.

This does not modify `oldAST`, instead it creates a fresh AST. And it should work no matter how deeply nested the `Identifier` is in `oldAST`.

This makes it quite easy to non-destructively update an AST, even if the modification is buried deep in the AST.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading the existing types.visit entry point and its traversal API. The work is complete when types.map returns a fresh AST, replaces deeply nested Identifiers as shown, preserves other structure, and leaves oldAST unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.