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

Avoid lossy source map merging #1

Open
5 of 10 tasks
aleclarson opened this issue Jun 14, 2018 · 4 comments
Open
5 of 10 tasks

Avoid lossy source map merging #1

aleclarson opened this issue Jun 14, 2018 · 4 comments
Labels
enhancement New feature or request

Comments

@aleclarson
Copy link
Owner

aleclarson commented Jun 14, 2018

As described by @eventualbuddha in Rich-Harris#67 and @mariusGundersen in mozilla/source-map#216, merging 2+ source maps should never result in lost mappings. The current behavior of sorcery and Mozilla's source-map is to trace the line mappings of the root source map and toss out the rest of the tree, which results in a poor debugging experience. Below is my attempt to solve this issue.

Check out the tests here, and add a test (or file an issue) if you come across an edge case.

Most of the fix is contained in the blend function. It's well-documented, but questions are welcome. This function has one argument; a Node object that represents the "root" source map in a tree of source maps. The sources array of the given node is used to trace the root mappings array to its original source(s). Additionally, segments from any other nodes in the tree are interweaved into the root mappings array if they fit.

Since blend only mutates the given node, we need to start from the leaf nodes and work our way up to the root node so all source maps in the tree are blended together. This is done with the trace function.

I hope this solution works well for anyone who needs it. Please comment if you have any questions or find any bugs. Maybe this solution will even be adopted by Mozilla and @Rich-Harris. :)

Test plan

These are the tests I have so far. Passing tests have a checkmark. ✔︎

  • {indent,dedent} lines
  • shift columns {left,right} from middle of line
  • break line in two {while,without} removing characters
  • insert line break at {start,end} of file
  • remove first line
  • remove {middle,last} line
  • remove all line breaks
  • move first line to {middle,bottom}
  • move last line to middle
  • swap two lines

Test ideas are welcome. :)

@whaaaley
Copy link

How's the progress on this? Is this still something you're working on?

@aleclarson
Copy link
Owner Author

@whaaaley I'm taking the "fix it if I need it" approach due to time constraints. If you find any bugs, a repro would be handy. In its current state, it probably works in most situations. I'll be testing it on real applications in the near future.

@whaaaley
Copy link

Alrighty. I'll test it out for my css-in-js idea soon.

@aleclarson
Copy link
Owner Author

@whaaaley You can use @cush/sorcery to avoid cloning it yourself. 👍

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

No branches or pull requests

2 participants