Implement Scope.prototype.rename(oldName, newName)
- Dominant language
- TypeScript
- Stars
- 1.2k
- Forks
- 194
- Avg merge
- 22h 43m
- Merged PRs (30d)
- 10
Description
@OliverJAsh asked on https://gitter.im/benjamn/recast if it was possible to determine what declaration(s) were responsible for a given variable reference.
Here's the code I came up with at first:
``` js
types.visit(ast, {
visitIdentifier(path) {
var node = path.node;
if (node.name === "x") {
var xScope = path.scope.lookup("x");
if (xScope) {
// Iterate over all the Identifiers used to declare x and rename them to y.
// TODO Might need to be careful not to do this more than once?
xScope.getBindings().x.forEach(function (idPath) {
idPath.node.name = "y";
});
node.name = "y"; // Rename the identifier too!
}
}
this.traverse(path);
}
});
```
As I was writing it out, I realized the common case (renaming) could be accomplished with a much nicer API:
``` js
types.visit(ast, {
visitIdentifier(path) {
var node = path.node;
if (node.name === "x") {
var xScope = path.scope.lookup("x");
if (xScope) {
xScope.rename("x", "y");
}
}
this.traverse(path);
}
});
```
Open question: what should `Scope.prototype.rename` return? Perhaps a list of paths to references, since scanning for references in nested scopes is potentially expensive?
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the Scope.prototype.rename entry point and compare the existing path.scope.lookup and getBindings behavior used in the example. Trace renaming through nested scopes and determine the return contract, including whether reference paths are returned. Done means declarations and references are renamed consistently without duplicate work and the chosen API behavior is verified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers, devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100