acornjs / acorn-jsx

Alternative, faster React.js JSX parser
MIT License
648 stars 74 forks source link

Add flow type support #11

Closed sebmck closed 9 years ago

sebmck commented 9 years ago

Most of this is a 1:1 mapping from esprima-fb. There are only two things that I haven't implemented due to not knowing the best way, this is types within arrow function parameters and the postfix for paranthesized primary types.

Apologies if the code is a bit janky, let me know if you want me to make any revisions.

sebmck commented 9 years ago

Ping @RReverser

RReverser commented 9 years ago

Why? I think it can be adapted to new tokenizer as well.

sebmck commented 9 years ago

Probably, the history is messed up so it'd be easier to just create another PR.

sebmck commented 9 years ago

Not sure what to do as the new JSX implementation breaks var identity: <T>(x: T, ...y:T[]) => T and many more, <T> is parsing as a JSX tag.

RReverser commented 9 years ago

You have to inject into the place where JSX expression is being started (https://github.com/RReverser/acorn-jsx/blob/master/acorn.js#L707) and change it to conditionally start either JSX expression or type generic based on previous token / parent context / whatever needed.

sebmck commented 9 years ago

@RReverser I see that _lt and _gt was changed to a general _relational, how do I determine which is which?

sebmck commented 9 years ago

Nevermind. tokVal, duh.

RReverser commented 9 years ago

It was relational initially, now it's just back to it in contexts where relational operator is allowed, or jsxTagStart / jsxTagEnd when should be tag boundary. This is the main benefit of new approach - any disambiguation is performed on tokenizer level, so 1) you can use it independently when needed (i.e. for highlighting) and 2) in parser funcs you get valid tokens for each given context and ready to be consumed.