I have added an option to my fork of streamlinejs for preserving line
numbers in the translated code. You can check it out here:
https://github.com/willconant/streamlinejs
Right now, you'll only get this behavior if you call
streamline.translate(source, {preserveLineNumbers: true}), or if you
require a file that has the !!STREAMLINE!! tag in it. In all other
cases, I stick to the pretty-printed output.
The primary strength of this approach is that the top of your stack
traces will contain the correct source line number without any extra
processing. The biggest weakness that I've seen so far is that the
rest of the stack trace tends to have a lot of useless/confusing
entries where control is resumed by constructs that only exist in the
translated code. For example, every "branch neutralizer" adds an entry
to the stack trace with a line number that roughly corresponds to the
location in the source where the branch ends. That being said, I think
this is a really good start on making streamlined code easier to
debug.
Here's how I got it to work:
1) I mark the original tree of narcissus nodes with the property
_isSourceNode before the tree is modified.
2) After the tree is modified, if preserveLineNumbers is true, I use a
modified version of Narcissus.decompiler.pp that generates a map of
output line numbers to input nodes along with the pretty-printed
source.
3) I rearrange the lines of the pretty-printed source to correspond
with the line numbers found on the nodes in the map, being careful to
only use nodes that were marked with _isSourceNode.
I could reduce this to two steps, simply having
Narcissus.decompiler.pp only add newlines such that the output line
numbers would match the input line numbers, but I think there may be a
useful hybrid approach where the transformed code is pretty-printed,
but the line number map is made available through the API for
debugging purposes. Another possibility is simply adding comments to
the ends of lines in the output source which indicate the
corresponding lines in the input source.
I haven't tested this extensively, so it very well may be horribly
broken, but I thought it was worth sharing early so I could get some
feedback before spending any more time.
Cheers!
--
Will Conant