babel / babylon

PSA: moved into babel/babel as @babel/parser -->

Home Page:https://github.com/babel/babel/tree/master/packages/babel-parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Flow comment parsing

jamiebuilds opened this issue · comments

Flow supports this comment syntax. These are some pretty simple parser hacks.

Comment Includes

Input:

/*:: 
type Foo = {...};
*/

Parsed:

type Foo = {...};

/*:: is ignored and everything until the next */ is parsed as if the comment tokens weren't there.

Input:

ab/*::c*/de

Parsed:

abcde

Comment Annotations

Input

function foo(bar/*: number */)/*: string */ {...}

Parsed:

function foo(bar: number): string {...}

These are the same as includes, except /*: is parsed as a : token. The /* and `*/ are thrown out.


I think these would be pretty easy to add to Babylon to get an AST including the parts inside comments.

The only question I have is if this should be part of the Flow plugin or a separate plugin. I'm leaning towards separate plugin.

I can take this on unless someone else wants to. I'm happy to help anyone out if they want to take it on. Might be a good one for beginners

Note that comment annotations are not contextual at all, as long as the resulting syntax is parsable, it is valid:

Input:

const object = {
  foo/*: bar */
};

Parsed:

const object = {
  foo: bar
};

Interesting idea. I definitely hadn't considered trying to treat it like actual syntax instead of parsing the comments later. It does seem like that would address a lot of the current issues we have with the flow comment plugin.

I'd be open to it. I assume we'd want a similar flag for the code generator to output all this stuff?

My only comment is that your ab/*::c*/de to abde example seems off, since it would be treated as two separate identifiers in the parser normally, so it seems like it should still see the comment as a token separator.

current issues we have with the flow comment plugin

There's a Flow comment plugin already? Where/What?

since it would be treated as two separate identifiers in the parser normally

Oh I'm sorry, you're right the comment tokens aren't simply thrown out, they are replaced with whitespace ref

We've got https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-flow-comments, but it's always been buggy because leadingComments and trailingComments aren't very accurate.

Generally whenever people submit bugs for it, we recommend they use .js.flow files instead, like babel/babel#6142 (comment)

You can see there are at least a few other obvious ones https://github.com/babel/babel/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20flow-comments

Ooooh, thought you meant a Babylon plugin

Yeah sorry, could have been clearer on that :)

@thejameskyle @loganfsmyth I can work on this.

This issue has been moved to babel/babel#6668.