Line-number Mappings (#558)

Regarding line-number mappings (#558): at Rentenna, we use this simple naive implementation -- thought it a good idea to share our solution.

We monkey-patch the parser to attach yylineno to any nodes created, and then once parsing is completed, we enforce the constraint that any node's line number can be at most the minimum of any of its children.

We find this to produce highly accurate source line-mappings.

Once we have attached line numbers to nodes, we insert text of the form "{{=LINE_NUMBER=}}" into the source js and then pull them out to produce the map. This is certainly a hack, but we found it to be a minimally invasive approach. Ideally, we could attach some metadata to the strings returned by compile, but it would probably require a major rewrite of the compile methods.

There were some problems with certain nodes where behavior depended on the compiled value. We disabled lineno printing for Value, Literal and Comment to avoid this issue.

The output map is a simple list of 0-based offsets. The length of the list is equal to the length of the generated coffeescript file, and the value of each element is the 0-based line in the original source.

We use these maps internally to generate sourcemaps for our application after combining several files, so this format works for us, but I would expect it would be preferable to generate sourcemaps directly in coffeescript.

In any case, I'm sure this misses some corner cases and the {{==}} hack does not seem ideal, but just thought we'd share our work. Thanks for creating an amazing tool.

Example stacktrace from our testing application: http://imgur.com/pBS0M