Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: if LEAST_UPPER_BOUND returns null, try GREATEST_LOWER_BOUND #375

Merged
merged 4 commits into from
Apr 22, 2019

Conversation

bcoe
Copy link
Member

@bcoe bcoe commented Apr 21, 2019

I'm running into issues adding source-map support to c8, in which LEAST_UPPER_BOUND returns mapping.source === null.

If I loosen up our check so that it falls back to the default behavior of GREATEST_LOWER_BOUND in this edge-case, c8's remapping works like a charm, see:

Screen Shot 2019-04-20 at 11 46 44 PM


@loganfsmyth can you clarify for me why this change might address my issues? I understand that they underlying cause is most likely that V8's coverage ranges don't directly map to the mappings generated by TypeScript ... but why would LEAST_UPPER_BOUND be returning null?

Reading the documentation, shouldn't it simply return the next closest element that's a greater line number than the element I'm remapping -- I guess I don't fully understand the scenarios that result in LEAST_UPPER_BOUND or GREATEST_LOWER_BOUND returning null.

@bcoe bcoe requested a review from coreyfarrell April 21, 2019 06:52
@bcoe bcoe mentioned this pull request Apr 21, 2019
2 tasks
sourceMap,
generatedLocation.start.line,
generatedLocation.start.column
);
let end = originalEndPositionFor(sourceMap, generatedLocation.end);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe, in the case of my testing with c8, it was the GREATEST_LOWER_BOUND in the originalEndPositionFor that was resulting in null data; opting for LEAST_UPPER_BOUND works like a charm ... but I don't fully understand why both wouldn't return an element.

Copy link
Member

@coreyfarrell coreyfarrell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I haven't really had a chance to gain a reasonable understanding of source-map modules / API's. What I can say is that I've confirmed that this patch combined with istanbuljs/nyc#1080 resolve some issues which were raised starting at nyc@13.2.0.

bias: GREATEST_LOWER_BOUND
});
if (mapping.source === null) {
mapping = sourceMap.originalPositionFor({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: You could just return sourceMap.originalPositionFor({ here then mapping could be const.

@bcoe
Copy link
Member Author

bcoe commented Apr 22, 2019

I dug into the source-map codebase, and can speak better to why this logic is necessary:

If you have GREATEST_LOWER_BOUND configured, source-map returns -1 rather than the closest element, if there's no element smaller than the node we're searching for in the tree:

    // we are in termination case (3) or (2) and return the appropriate thing.
    if (aBias == exports.LEAST_UPPER_BOUND) {
      return mid;
    } else {
      return aLow < 0 ? -1 : aLow;
    }

inversely, if LEAST_UPPER_BOUND is configured, source-map returns -1 if there is no element greater than the element being searched for:

    if (aBias == exports.LEAST_UPPER_BOUND) {
      return aHigh < aHaystack.length ? aHigh : -1;
    } else {
      return mid;
    }

By trying a search for LEAST_UPPER_BOUND and GREATEST_LOWER_BOUND what we're essentially doing is ensuring that we get the mid point returned to us. This behavior seems more reasonable than completely falling over when generated ranges and source map ranges don't line up well -- as is the case with c8.

There's a similar conversation happening here about trying both approaches:

mozilla/source-map#376

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants