-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add variance node type and generate property variance annotations #4697
Conversation
babel/babylon#161 adds parsing support for property variance annotations. This PR adds the necessary node type for the new Variance node and generate support for all the positions where variance can now appear.
this.token("+"); | ||
} else if (node.variance === "minus") { | ||
this.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.
Hmm, I'm concerned that this will be a breaking change. Thought?
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're right, it is a breaking change. I can roll back that part of the change, so the TypeParameter node is unchanged, if you think that's best.
Flow 0.34, which introduces property variance, will also make the same breaking change to the TypeParameter AST.
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.
We'll need to think through how to approach it. I just don't want to end up in a state where people get broken output or thrown exceptions.
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.
For the generator specifically we'd probably be able to do
if (node.variance && typeof node.variance === 'object'){
this.print(node.variance, node);
} else if (node.variance === "plus"){ // ...
my concern is more related to my other comment about the visitor
list, because if we add variance
to the visitor
list for TypeParameter
, people using older versions of Babylon and getting older ASTs will start getting errors because traversal will start traversing down into the string value expecting it to be an AST node.
@@ -60,6 +60,7 @@ export function ClassProperty(node: Object) { | |||
this.print(node.key, node); | |||
this.token("]"); | |||
} else { | |||
this.print(node.variance, node); |
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.
Because this has added this new variance
property to all of these nodes, variance
needs to be added to the visitor
list in https://github.com/babel/babel/blob/master/packages/babel-types/src/definitions/flow.js#L46 and all of the other node types that now have a .variance
property. Otherwise, strip-flow-types
will miss them and not strip them.
Thank you for the thoughtful review, Logan. In retrospect, the breaking change I've made here is gratuitous. We don't need a position for the variance symbol, because it's always a single char at the very beginning of the enclosing node. I will roll back the new AST node in the Flow AST as well and just use |
This diff also adds tests to class properties and to the flow-strip-types transform.
OK, the latest update removes the breaking change and should resolve the issues raised here. Thanks again! |
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.
Looks good to me. Should we make an issue to change back to variance being a Node as soon as we major bump?
Yeah, an issue for that sounds good. I'm happy to do the work when the time comes :) Thanks! |
@samwgoldman I realize we'll also need to add support for this to the What would be the expected output for the comment-based representation of the type annotations? |
@loganfsmyth Interesting. I'm not sure I understand what the purpose of nulling out the property is there. I definitely added tests to The expected output for comment-based annotations on classes would use the
|
I think what @loganfsmyth is referring to is the case when we mix flow with class-properties: class C2 {
+p: T = e;
} This would probably not remove the variance. flow-strip-types removes the whole property if class-properties not supported, so that's why it does not appear in the test cases. Testcase would be probably So it might be good in all cases to |
Current coverage is 88.82% (diff: 100%)@@ master #4697 diff @@
==========================================
Files 196 196
Lines 13868 13874 +6
Methods 1430 1431 +1
Messages 0 0
Branches 3198 3198
==========================================
+ Hits 12317 12323 +6
Misses 1551 1551
Partials 0 0
|
Sorry I didn't get time to reply yesterday. @danez is totally correct. |
Oooh, that makes sense—a very important use case! Thanks for catching my various missteps on this PR! |
Also it would helpful if you could writeup the changelog entry description for flow prs 😄 |
* master: (38 commits) chore(package): update browserify to version 13.1.1 (babel#4762) Increase test coverage (babel#4742) Make getBinding ignore labels; add Scope#getLabel, Scope#registerLabel (babel#4758) Add variance node type and generate property variance annotations (babel#4697) Add make command to delete node_modules (babel#4748) fixes [skip ci] Support ObjectExpression in static path evaluation (babel#4746) Fix replacing for-of if inside label (babel#4736) Replace `path-exists` with `fs.existsSync` (babel#4731) Avoid unnecessary +0 in transform-es2015-parameters (babel#4738) [import()] Initial support for dynamic-import (babel#4699) Fix line endings on checkout Automatically generate missing expected.js fixtures (babel#4735) Fix few typos in issue/pr templates (babel#4739) [skip ci] contributing updates [skip ci] increase git depth [skip ci] Change usage of "suite"/"test" in unit-tests to "describe"/"it" (babel#4734) Run ESLint on test files, and fix lint errors in test files (babel#4732) Add .gitattributes forcing LF line endings (babel#4730) Update tests for changed error messages in Babylon (babel#4727) ...
…bel#4697) * Add variance node type and generate property variance annotations babel/babylon#161 adds parsing support for property variance annotations. This PR adds the necessary node type for the new Variance node and generate support for all the positions where variance can now appear. * Variance is no longer a separate node type This diff also adds tests to class properties and to the flow-strip-types transform. * Add test + fix for edge case with variance and class proeprties
babel/babylon#161 adds parsing support for property variance
annotations. This PR adds the complementary support to babel-generator and tests.