-
Notifications
You must be signed in to change notification settings - Fork 95
Implements getScope parsing in the devtools-map-bindings #656
Conversation
jasonLaster
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good. A couple of style comments.
| const { sourceId } = location; | ||
| const visitor = createParseJSScopeVisitor(sourceId); | ||
| traverseAst(source, visitor.traverseVisitor); | ||
| const parsedScopes = visitor.toParsedScopes(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how you pull in the test traverseAst helper
| } | ||
| tree.params.forEach(param => { | ||
| parseDeclarator(param, scope, "param"); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
tree.params.forEach(param => parseDeclarator(param, scope, "param")))
| }); | ||
| } else if (isNode(declaratorId, "RestElement")) { | ||
| parseDeclarator(declaratorId.argument, targetScope, type); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we'll want some tests for these cases
| let zero = 0; | ||
| function fn(a, b) { | ||
| for (let i of [1, 2]) { | ||
| const i = a + i + zero; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it'd be nice if i were index so that we can have another minifier check
| // Find nearest outer scope with the specifed name and add reference. | ||
| for (let s = scope; s; s = s.parent) { | ||
| if (name in s.names) { | ||
| s.names[name].refs.push(loc.start); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be nice if this function avoided mutating scope and instead returned the found location:
| return; | ||
| } | ||
| if (path.isVariableDeclaration()) { | ||
| const hoistAt = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
neat. I imagine this might be one of the more interesting considerations because babel is free to add other hoisting rules in the future:
perhaps we want to try looking for the ref:
path.findParent((path) => path.scope.bindings.include(?));NOTE: just guessing based on reading the code, i could be mis-reading this
| if (hasLet) { | ||
| savedParents.set(path, parent); | ||
| parent = createTempScope("Block", parent, location); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whats this doing?
| return false; | ||
| } | ||
| return node.kind === "let" || node.kind === "const"; | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we move this function up top?
function hasLet(path) {
return path.node.body.some(node => {
if (!isNode(node, "VariableDeclaration")) {
return false;
}
return node.kind === "let" || node.kind === "const";
});
}a7c6702 to
3b35eb2
Compare
3b35eb2 to
d8bc810
Compare
jasonLaster
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Associated Issue: #624
Summary of Changes