Skip to content
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

Merged
merged 4 commits into from Oct 21, 2016

Conversation

samwgoldman
Copy link
Contributor

@samwgoldman samwgoldman commented Oct 8, 2016

Q A
Bug fix? no
Breaking change? no
New feature? yes
Deprecations? no
Spec compliancy? no
Tests added/pass? yes
Fixed tickets
License MIT
Doc PR

babel/babylon#161 adds parsing support for property variance
annotations. This PR adds the complementary support to babel-generator and tests.

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.
@danez danez added PR: New Feature 🚀 A type of pull request used for our changelog categories pkg: parser labels Oct 8, 2016
this.token("+");
} else if (node.variance === "minus") {
this.token("-");
}
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Member

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);
Copy link
Member

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.

@samwgoldman
Copy link
Contributor Author

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 "plus" | "minus" | null for properties as well, then update the PRs here and for babylon.

This diff also adds tests to class properties and to the
flow-strip-types transform.
@samwgoldman
Copy link
Contributor Author

OK, the latest update removes the breaking change and should resolve the issues raised here. Thanks again!

Copy link
Member

@danez danez left a 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?

@samwgoldman
Copy link
Contributor Author

Yeah, an issue for that sounds good. I'm happy to do the work when the time comes :) Thanks!

@loganfsmyth
Copy link
Member

loganfsmyth commented Oct 19, 2016

@samwgoldman I realize we'll also need to add support for this to the flow-strip-types plugin at https://github.com/babel/babel/blob/master/packages/babel-plugin-transform-flow-strip-types/src/index.js#L25 to null our variance.

What would be the expected output for the comment-based representation of the type annotations?

@samwgoldman
Copy link
Contributor Author

@loganfsmyth Interesting. I'm not sure I understand what the purpose of nulling out the property is there. I definitely added tests to flow-strip-types, which ensures the variance annotations are removed. Can you explain a bit more about why a change there is required?

The expected output for comment-based annotations on classes would use the /*:: */ syntax, like so:

class Base {
  /*::+*/p: ?string;
}
class Derived extends Base {
  p: string;
}

@danez
Copy link
Member

danez commented Oct 21, 2016

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 flow-strip-types + syntax-class-properties and the example above.

So it might be good in all cases to null it in flow-strip-types

@codecov-io
Copy link

codecov-io commented Oct 21, 2016

Current coverage is 88.82% (diff: 100%)

Merging #4697 into master will increase coverage by <.01%

@@             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          

Powered by Codecov. Last update 7443f9e...c3d90e0

@loganfsmyth
Copy link
Member

Sorry I didn't get time to reply yesterday. @danez is totally correct.

@samwgoldman
Copy link
Contributor Author

Oooh, that makes sense—a very important use case! Thanks for catching my various missteps on this PR!

@hzoo hzoo merged commit 7bb430a into babel:master Oct 21, 2016
@hzoo
Copy link
Member

hzoo commented Oct 24, 2016

Also it would helpful if you could writeup the changelog entry description for flow prs 😄

mstade added a commit to zambezi/babel that referenced this pull request Oct 28, 2016
* 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)
  ...
panagosg7 pushed a commit to panagosg7/babel that referenced this pull request Jan 17, 2017
…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
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 7, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: New Feature 🚀 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants