-
-
Notifications
You must be signed in to change notification settings - Fork 258
[Flow] Arrow function type parameter declarations #54
Conversation
This fixes https://phabricator.babeljs.io/T7330 |
instance.extend("parseMaybeAssign", function (inner) { | ||
return function (...args) { | ||
let jsxError = null; | ||
let state = this.state.clone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't super obvious but cloning state is actually super expensive, for example there's a tokens
array on there that gets cloned and this can sometimes number in the tens of thousands for really large files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh great, just noticed that state
is only used within the JSX if
, you can just move it into there. It should resolve the more pathological cases but the perf issue will still be there. We should really use babylon to use immutable data structures so this state cloning is really cheap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll move it in there. It will still be hit for every JSX expression, I'm afraid, but that's better than every expression :)
Thanks @gabelevi! I'll publish a new release now. |
We aren't running the changes on babel in travis yet right? |
@hzoo Nope. |
I pushed a new release just now, I'll run |
Ok cool - it does suck that it would make the tests run a lot longer but worth doing. (installing it now locally too, seeing no issues) |
In Flow, you can declare type parameters for arrow functions. The syntax looks like this:
This is a little tricky to parse, especially with JSX. This PR attempts to solve the problem as follows
<
, then parse a type parameter declaration. Then parse an assignment expression. If that expression turns out to an ArrowFunctionExpression, then attach those type params to that expression. Otherwise, fail. On failure throw the JSX error, if it exists, or a new error.<
then just parse an assignment expression as usual.This was further complicated by the need to reinterpret tokens and mess with context. But at least it seems to work :)