@@ -11,6 +11,16 @@ export interface GeneratorOptions {
1111 typePropName : string ;
1212}
1313
14+ /**
15+ * Gets the identifier name for the component.
16+ *
17+ * If the component has a displayName that differs from its
18+ * identifier, this will return the identifier.
19+ */
20+ function getComponentIdentifier ( d : ComponentDoc ) : string {
21+ return d . expression ?. getName ( ) || d . displayName
22+ }
23+
1424/**
1525 * Inserts a ts-ignore comment above the supplied statement.
1626 *
@@ -42,12 +52,18 @@ function insertTsIgnoreBeforeStatement(statement: ts.Statement): ts.Statement {
4252 * SimpleComponent.displayName = "SimpleComponent";
4353 * ```
4454 */
45- function setDisplayName ( d : ComponentDoc ) : ts . Statement {
55+ function setDisplayName ( d : ComponentDoc ) : ts . Statement | null {
56+ // If the expression name doesn't match the display name,
57+ // then we know the component has already set a displayName
58+ if ( d . expression && d . expression . getName ( ) !== d . displayName ) {
59+ return null
60+ }
61+
4662 return insertTsIgnoreBeforeStatement (
4763 ts . factory . createExpressionStatement (
4864 ts . factory . createBinaryExpression (
4965 ts . factory . createPropertyAccessExpression (
50- ts . factory . createIdentifier ( d . displayName ) ,
66+ ts . factory . createIdentifier ( getComponentIdentifier ( d ) ) ,
5167 ts . factory . createIdentifier ( "displayName" )
5268 ) ,
5369 ts . SyntaxKind . EqualsToken ,
@@ -270,7 +286,7 @@ function insertDocgenIntoGlobalCollection(
270286 ts . factory . createPropertyAssignment (
271287 ts . factory . createIdentifier ( "docgenInfo" ) ,
272288 ts . factory . createPropertyAccessExpression (
273- ts . factory . createIdentifier ( d . displayName ) ,
289+ ts . factory . createIdentifier ( getComponentIdentifier ( d ) ) ,
274290 ts . factory . createIdentifier ( "__docgenInfo" )
275291 )
276292 ) ,
@@ -316,7 +332,7 @@ function setComponentDocGen(
316332 ts . factory . createBinaryExpression (
317333 // SimpleComponent.__docgenInfo
318334 ts . factory . createPropertyAccessExpression (
319- ts . factory . createIdentifier ( d . expression ?. getName ( ) || d . displayName ) ,
335+ ts . factory . createIdentifier ( getComponentIdentifier ( d ) ) ,
320336 ts . factory . createIdentifier ( "__docgenInfo" )
321337 ) ,
322338 ts . SyntaxKind . EqualsToken ,
0 commit comments