-
-
Notifications
You must be signed in to change notification settings - Fork 258
Conversation
The accepted syntax for function declarations is extended to allow the following predicate declaration: FunctionReturnType := Type Predicate Type Predicate Predicate := %checks %checks ( ConditionalExpression )
Current coverage is 97.29% (diff: 100%)@@ master #103 diff @@
==========================================
Files 21 21
Lines 4001 4035 +34
Methods 485 491 +6
Messages 0 0
Branches 1179 1180 +1
==========================================
+ Hits 3892 3926 +34
Misses 49 49
Partials 60 60
|
this.expectContextual("checks"); | ||
// Force '%' and 'checks' to be adjacent | ||
if (moduloLoc.line !== checksLoc.line || moduloLoc.column !== checksLoc.column - 1) { | ||
this.raise(moduloPos, "Unexpected token"); |
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 line is not covered. Do you think we can have two tests that check if there are parse errors in these cases?
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.
You could also use this.unexpected(moduloPos);
here. It's a little bit shorter but otherwise isn't really important if not changed.
Thank you for your first PR to babylon. In order to get this working throughout the complete chain of babel, all the new node types (I think it is only I would wait with the merge till we have a PR in babel and the new flow version is released. Great work! |
Thanks @danez! I'll address your comments. |
@danez: This can still wait until Flow's upcoming release. |
|
||
pp.flowParseTypeAndPredicateInitialiser = function () { | ||
let oldInType = this.state.inType; | ||
this.state.inType = true; |
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.state.inType
seems to be only necessary for this.flowParseType()
, so maybe we could just do this two lines in the else branch? Then the reset only has to happen there either.
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.
It looks like this.state.inType
needs to be true
for this.expect(tt.colon)
to succeed (I tried to move it further down, but other tests failed). So, oldInType
needs to be recorded in the beginning in order to be restored before this.flowParsePredicate()
is called (note that flowParsePredicate
possibly parses expressions).
Bottom line is I wasn't able to perform the optimization you suggested.
this.expectContextual("checks"); | ||
// Force '%' and 'checks' to be adjacent | ||
if (moduloLoc.line !== checksLoc.line || moduloLoc.column !== checksLoc.column - 1) { | ||
this.unexpected(moduloPos); |
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.
Can you add a nice error message here? Maybe something like "Spaces between ´%´ and ´checks´ are not allowed here."
The second argument to this.unexpected
can be a custom message.
this.unexpected(moduloPos); | ||
} | ||
if (this.match(tt.parenL)) { | ||
this.next(); |
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.
Instead of
if (this.match(tt.parenL)) {
this.next();
you can do
if (this.eat(tt.parenL)) {
Which is doing the exact same thing but is shorter. :)
Thanks for the pointers, @danez! I will get to these, but I'm still waiting for support for function predicates to be fully merged in Flow master. |
I know flow supports this now, is this waiting on more changes, or has it just been forgotten? |
Sorry for taking so long to get back to this. AFAIK, there is still some function predicate code that hasn't been merged to Flow's master. Most of these changed involve the type checker, but there is also some parser work under way. For example I believe the notation of predicate refinement will be updated to something more elegant. Also, there has not been an official announcement yet. However, you are right that some support for function predicates (including I recently rebased my original PR, addressed @danez 's comments (including the ones in the respective babel PR) and added one more example. |
Any progress on this? I'd love to see this land to unblock early use |
The accepted syntax for function declarations is extended to allow
the specification of a function predicate (inferred or explicit). This
should be placed right after the function's return type (if existing,
otherwise the colon that would designate the start of the return
type). So the return type syntax becomes:
These changes are with regards to this commit in the Flow repo.