feat: Provide raw expression as part of parser output#398
Merged
pvasek merged 2 commits intostyleguidist:masterfrom Dec 1, 2021
Merged
Conversation
Collaborator
|
I think this can be really good for a lot of different use cases that would be hard to support in any other way. I see the following possible issues there:
What do you think about it? Shouldn't we at least include some kind of opt-in switch for that in |
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is re: hipstersmoothie/react-docgen-typescript-plugin#51 and storybookjs/storybook#15401 cc @hipstersmoothie @leepowelldev
tl;dr This PR would allow consumers who parse components with
react-docgen-typescriptto access the expression for that component.Use case
In Storybook, a component's prop table will break if you set the component's
displayNameto a value that differs from its identifier. For example, let's say that you define a component like this:Then
react-docgen-typescript-pluginwill append code to that module like this:That code will be a no-op, because
MyButtonDisplayNameis an invalid reference.Why does
react-docgen-typescript-pluginbehave this way? Because thereact-docgen-typescriptparser doesn't return any way of obtaining the component's identifier other thandisplayName.If the parser returned the component
expression, then consumers likereact-docgen-typescript-plugincould useexpression.getName()instead ofdisplayNameto correctly handle cases wheredisplayNameis a different value.Note that I opted to return the
expressionobject here, rather than just the result ofexpression.getName(), because it might help to address another case: unnamed default exports.react-docgen-typescript-pluginfails for component declarations of the formexport default function() { ... }, since there's no identifier to reference the component in that case. But using the expression, it might be able to inject one—that is, it could convert the component declaration to one of the formexport default function SomeGeneratedName() { ... }, making it referencable.