-
-
Notifications
You must be signed in to change notification settings - Fork 303
Support for flow types #48
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
Conversation
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at [email protected]. Thanks! |
Nice, thank you! TBH, I had something different in mind with flow type support. I thought we could run flow directly and query it to get type information (though I don't know how much information we can get). However, if we can do it this way and get equally good results, that's great! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
I tried flow itself before, but the ast it delivers is nearly the same as the one from babylon. The only current drawback with babylon is that imported/required types or types which are defined as libs for flow, cannot be resolved. But I also don't know how one would do that with flow. That might be a future addition if someone finds a way to do that. I'm nearly done with all the codechanges. I probably might refactor smaller pieces, but I think there wont be large changes anymore. I will now start updating the readme and add a section about flow, and what are the differences to propTypes. As flow offers more granular types, the results (except primitive types) are a little bit different. |
I was thinking that flow can return us the type information already parsed, but since I haven't worked with it yet, I might be wrong. Either way, I am really happy about this :) When you are done, I'd appreciate it if you could squash everything into a single commit. |
Yes squashing everything was the plan. :) |
Do you think that the flowHandler should be part of the default handlers? |
This is fantastic, very much looking forward to using it. Is there anything I can help with to get this landed sooner? |
Ok I am done from my side, as far as I can see. The only remaining question is if the flowTypeHandler should be part of the default handlers (currently it is)? This probably depends on how you want to release this feature: With this enabled by default I would think it is more a 3.0 version (as it could generate different output than before for people), whereas if it is not enabled by default, then it could be 2.7. But thats up to you @fkling . If you want me to change things pls tell me. |
Mmh, not sure, what would you prefer? Would it make sense to have a |
First of all flow- and propType-handlers don't conflict with eachother. Having both active is not a problem. The only "problem" that could arise is if people use both propTypes and flow in their components. In that case one of the handlers (depending on the order) would overwrite the other one. But even if they don't conflict, I like the idea of having a For the api people can build their desired set of handlers anyway on their own. If thats all okay, I add that? |
Right. I think (hope?) that this is not very common. What's more interesting is how often people use flow type annotation and However, I think it would fine to have this option first (no major update needed) and then see if others need a more flexible setup. |
Okay I added the cli option. |
Actually, here might be a better idea: Lets simply add the flow prop types to a new key, e.g. How does that sound? |
But then you end up having defaultValues under I was thinking about something similar: Having the flow type under |
Seems fine too.
Do you think people would add a description to both, the propTypes and the flow types in the same component? I think it's fine to assume that only one of them will be annotated. We could make it so that either the flow type description handler or the propType description handler doesn't set a description if there already is one set. |
That seems a reasonable approach. React-docgen could also warn if there are multiple descriptions found. |
Ok i changed the flowType from And both docgen handlers respect each other and don't update the description if already set, but trigger a warning. I removed the cli option again and added flowTypeHandler and flowTypeDocHandler to defaultHandlers. |
Two comments inside (somehow I'm always adding the comments so that GH doesn't show them inline, but whatever). If that's clarified I think we are good to go! Really awesome work, I'm sure lots of people will be happy about this, I certainly am :) Thank you! |
You were right about the type<->flowType thing in the readme, I did not regenerate it with the latest changes. Done now. |
And yes you are right, the required field is also set by the flowTypeHandler, although I think this is less critical than the type itself i would say. In case someone really uses both propTypes and flow, the type can be completely different, but the required field should be the same, And I think it would count as a bug in the components if you specify a prop as required with propTypes but not with flow or the other way around. |
I think its fine for now. If we encounter any problems, we can always update later. Everything looks really great! I'm only a little concerned regarding the warnings, since there is no way to disable them. This might be surprising if react-docgen is used as a module instead of the CLI. OTOH, I think it would be valuable if there was some information about unresolved types. But I also don't want to block this on implementing a proper error handling feature. Should we just omit the warnings for now? |
Thats true. There is also a note in the readme about this unresolvable types. |
Okay I removed them now. |
Thank you! I'm going to merge this now, and release a new version later today. Also thank you for your patience for my many questions and requests, and again, for implementing this feature! |
@fkling If there are bugs coming in, feel free to ping me. |
Looking forward to that ast-types PR :) |
I started working on adding support for flow types.
So far I only tried to get it working for classes, and it already works nicely for this usecase, but only for string type and declared types. The code although is far away from nice yet. It also needs a patched version of ast-types, which I will create a PR also soon.
I opened this PR already so that interested people can join and already have a look and maybe give feedback.
Will fix #31
This needs to be done:
support createClass()handle imported type definitions (not really sure if we can do anything about that)