feat: Provide raw expression as part of parser output #398
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-typescript
to access the expression for that component.Use case
In Storybook, a component's prop table will break if you set the component's
displayName
to a value that differs from its identifier. For example, let's say that you define a component like this:Then
react-docgen-typescript-plugin
will append code to that module like this:That code will be a no-op, because
MyButtonDisplayName
is an invalid reference.Why does
react-docgen-typescript-plugin
behave this way? Because thereact-docgen-typescript
parser 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-plugin
could useexpression.getName()
instead ofdisplayName
to correctly handle cases wheredisplayName
is a different value.Note that I opted to return the
expression
object here, rather than just the result ofexpression.getName()
, because it might help to address another case: unnamed default exports.react-docgen-typescript-plugin
fails 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.