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

Check for import declaration type when getting default values, resolves #63 #99

Merged
merged 1 commit into from
Aug 22, 2016

Conversation

nathanmarks
Copy link
Contributor

A common pattern in libraries is to set a default prop value as an imported component, for eg. a transition component for a dialog.
#63 has an example + a workaround (not the most elegant however, involves using a functional component to return the default component).

Right now, react-docgen throws an error on components implementing this pattern as ImportDefaultDeclaration is not a printable type.

This PR adds a conditional after the resolveToValue(path) call in getDefaultValue(path) to check if the node is an import declaration. If so, it returns the .name property of the original node (so the function/component name).

@brentschroeter
Copy link

+1: Code looks good. I've been including a similar handler manually in my application and would love to see Nathan's fix made standard.

@fkling
Copy link
Member

fkling commented Aug 2, 2016

yeah, this looks good to me. I was wondering if it makes sense to include some additional information about the origin of the value (e.g. that it is the default import of the ./ImportedComponent module), but I'm not sure where / how.

@nathanmarks
Copy link
Contributor Author

nathanmarks commented Aug 2, 2016

@fkling We can definitely provide some more information.

We know whether it was a default or a named import, and we know the require path.

Are you looking to provide information from the imported module itself? Or are you looking for something more specific about the import statement, ie: ImportedComponent - default export from module './ImportedComponent, NamedExport - export from module './ImportedComponent'?

@fkling
Copy link
Member

fkling commented Aug 2, 2016

Are you looking to provide information from the imported module itself?

No. This has come up a few times and maybe we are able to resolve and inspect dependencies at some point, but I was only thinking about the import statements itself.

In particular, if someone generates the docs, I can imagine they might want to link the source of the module or some other part of the page that provides more detailed information about ImportedComponent, and for that they probably need the path at least.

@nathanmarks
Copy link
Contributor Author

We can provide some additional information in the returned object.

Let me know exactly what you'd like to see, and I'll fix it up.

@levithomason
Copy link

@fkling any way we can get this merged for now? possibly add more info about the module later? It is currently breaking our doc build.

@fkling
Copy link
Member

fkling commented Aug 22, 2016

@levithomason, sure! I will try to release a new version today or tomorrow.

@fkling fkling merged commit b8681fb into reactjs:master Aug 22, 2016
@levithomason
Copy link

Super fast, thanks much!

@nathanmarks nathanmarks deleted the imported-default-prop-types branch August 22, 2016 21:27
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants