AST scopes/binding to names mapping.#624
Conversation
2a66879 to
f13350b
Compare
| // these positions, check what original name is listed there. To make things | ||
| // more interesting the same generated name can potentially be used for | ||
| // different original names. | ||
| let trackedBindings: { [string]: |
There was a problem hiding this comment.
This type should be defined above:
let trackedBindings: TrackedBindings
codehag
left a comment
There was a problem hiding this comment.
Hey yury, this looks super interesting. I made some comments but take them with a grain of salt for now -- i need to take another look at it to make sure that i got it right. The main improvement I could see here is readability and by extension, maintainability; When i revisit it tomorrow I will try to give better comments
| // these positions, check what original name is listed there. To make things | ||
| // more interesting the same generated name can potentially be used for | ||
| // different original names. | ||
| let trackedBindings: { [string]: |
There was a problem hiding this comment.
we should use const instead of let when the variable doesnt get reassigned. It makes it easier to read the code, otherwise I end up searching for where trackBindings is reassigned.
It looks like this can be changed for all instances of let in this block of code. There is no reassignment of the objects, only modification of their properties.
| !originalPositionTest || | ||
| originalPositionTest.name !== originalPosition.name || | ||
| originalPositionTest.column !== originalPosition.column || | ||
| originalPositionTest.line !== originalPosition.line |
There was a problem hiding this comment.
from line 231 - 241 -- we should factor this out into a function
function isExactPosition(originalPosition, position) {
const originalPositionTest = map.originalPositionFor({
line: position.line,
column: position.column,
bias: SourceMapConsumer.LEAST_UPPER_BOUND
});
return (
!originalPositionTest ||
originalPositionTest.name !== originalPosition.name ||
originalPositionTest.column !== originalPosition.column ||
originalPositionTest.line !== originalPosition.line
);
}
and do if (!isExactPosition(originalPosition, position) here
| if ( | ||
| !originalPosition || | ||
| !originalPosition.name || | ||
| originalPosition.name === name |
There was a problem hiding this comment.
it would be good to save this boolean in a variable with a descriptive name; or we can extract this into a function
| bestLocation: Location | ||
| } | ||
| } = Object.create(null); | ||
| Object.keys(scope.bindings).forEach(name => { |
There was a problem hiding this comment.
can you tell me a bit about what you put into this loop, and what should be the result when this loop is finished? I imagine at the moment that we have the following signature
Array<scopeBindingName> -> Map<name, validBestLocation>
and discarding cases of a scope name where there is no bestLocation
if this is the case, a reduce would fit better here; it would be more explicit what is happening
| } = Object.create(null); | ||
| Object.keys(scope.bindings).forEach(name => { | ||
| const positions = scope.bindings[name]; | ||
| positions.forEach(position => { |
There was a problem hiding this comment.
I suspect the same thing is happening here, where we have the following signature
Array<Position> -> Map<name, bestLocation>
And a reduce might be more explicit; we could have something like this:
const bestLocations = positions.reduce(findBestLocations(name))
and findBestLocation does (name) -> (bindings, Position) -> Map<name, bestLocation>
There was a problem hiding this comment.
positions.reduce might need the initial value positions.reduce(position => findBestLocations(position), {})
There was a problem hiding this comment.
find best location shall be performed across all generated names -- I reviewed summary comment before algorithm and provided example why things are done the way they are.
| }); | ||
|
|
||
| let bindings: { [string]: string } = Object.create(null); | ||
| Object.keys(trackedBindings).forEach(name => { |
There was a problem hiding this comment.
this should be a reduce as well
There was a problem hiding this comment.
fixed here and above.
f13350b to
b2ea826
Compare
jasonLaster
left a comment
There was a problem hiding this comment.
one style nit. @codehag is reviewing for accuracy :P
| return originalPosition.name; | ||
| } | ||
|
|
||
| async function getLocationScopes( |
There was a problem hiding this comment.
I think getLocationScope is sufficiently complex to justify its own module with some helper methods.
function appendTrackedBinding() {}
function appendBinding() {}
function getScopeBindings() {}
function getLocationScope() {
generatedSourceScopes.map(scope => {
// ...
const trackedBindings = bindings.reduce(appendTrackedBinding, {})
const bindings = trackedBindings.reduce(appendBinding, {});
return {
type: scope.type,
bindings,
};
})
}
module.exports = { getLocationScope}There was a problem hiding this comment.
appendTrackedBinding and appendBinding extraction is not possible due to dependency on location/currentPosition.
There was a problem hiding this comment.
getLocationScope also depends on _getSourceMap
There was a problem hiding this comment.
we might need to do a bit more splitting to share _getSourceMap between modules.
we also might need to pass in the closure state:
bindings.reduce(binding => appendTrackedBinding(binding, location, ...), {})If this feels like too much we can punt on it.
There was a problem hiding this comment.
Did not extracted _getSourceMap -- just exposed it from ./source-map
Moved scopes into separate file and refactored to have appendTrackedBinding.
99169f0 to
6c6bdb4
Compare
|
I think it would be nice to switch to using a fixtures:
Here is a commit where i setup uglify to make to do the minifying. |
| ); | ||
| } | ||
|
|
||
| async function getLocationScopes( |
There was a problem hiding this comment.
I'd like jsdoc for all new exported functions. Lack of documentation is one of the bigger problems in this entire module already.
codehag
left a comment
There was a problem hiding this comment.
this looks good from my side
6c6bdb4 to
4acef87
Compare
4acef87 to
d094523
Compare
| column: 1 | ||
| }, | ||
| name: "zero" | ||
| }); |
There was a problem hiding this comment.
we can clean this up after we add the parser code and can improve the test in one pass.
Associated Issue: firefox-devtools/debugger#3427
Summary of Changes
Test Plan
See firefox-devtools/debugger#3817