jashkenas/coffee-script — pull request #3383 by eventualbuddha (2014-02-21 17:49:06+00:00)
Title: [RFC] Optionally emit comments from the lexer and the parser.

The lexer emits comment tokens inline since they don't interfere semantically. In the generated AST they are added to the root node as a "comments" property because they cannot (easily) be put directly into the nodes where valid comments might appear. This is similar to how Esprima handles parsed comments in the generated AST:

```js
> parse('1 + // foo\n1', { comment: true })
{ type: 'Program',
body:
[ { type: 'ExpressionStatement',
expression: [Object] } ],
comments: [ { type: 'Line', value: ' foo' } ] }
```

I tried searching for something similar to this in the coffee-script issues but never found one. There were a bunch of discussions about how hard it would be to support attaching comment nodes to their nearest node, and I agree that is hard.

#### Goals

- allow extracting the single-line comments from CoffeeScript source, for example to facilitate a [recast](https://www.npmjs.org/package/recast)-style re-writer for CoffeeScript

#### Non-Goals

- emit single-line comments in the generated JavaScript
- include comments in the generated AST or token list by default

I probably did something wrong in this PR and am mostly looking for comments to improve the approach I've taken. I don't especially care that this is merged, though it would make the project I'm working on (the single example from "Goals" above) easier to realize as an open source project rather than an internal tool at my company.