jashkenas/coffee-script — pull request #2483 by kevinjdolan (2012-08-08 19:56:18+00:00)
Title: Line-number Mappings (#558)

Regarding line-number mappings: 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.

The output map is a simple js->coffee hash which is serialized to JSON and written to the file specified using the `--map` command line option. The line counts are 1-based.

We use these maps internally for debugging our application, so this format works for us.

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.