Skip to content

Commit b4968c4

Browse files
Merge pull request #58 from TrevorBurnham/trevorburnham/51-displayname-breaks-docgen
Use component expression name as identifier
2 parents 8465469 + a91043f commit b4968c4

5 files changed

Lines changed: 46 additions & 7 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as React from "react";
2+
3+
interface ButtonComponentProps {
4+
text: string;
5+
}
6+
7+
export const Button = (props: ButtonComponentProps) => (
8+
<button>{props.text}</button>
9+
);
10+
11+
Button.displayName = "MyButtonDisplayName";

src/__tests__/__snapshots__/generateDocgenCodeBlock.test.ts.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ try {
7676
catch (__react_docgen_typescript_loader_error) { }"
7777
`;
7878
79+
exports[`component fixture DisplayName.tsx has code block generated 1`] = `
80+
"import * as React from \\"react\\";
81+
82+
interface ButtonComponentProps {
83+
text: string;
84+
}
85+
86+
export const Button = (props: ButtonComponentProps) => (
87+
<button>{props.text}</button>
88+
);
89+
90+
Button.displayName = \\"MyButtonDisplayName\\";
91+
92+
try {
93+
// @ts-ignore
94+
MyButtonDisplayName.displayName = \\"MyButtonDisplayName\\";
95+
// @ts-ignore
96+
Button.__docgenInfo = { \\"description\\": \\"\\", \\"displayName\\": \\"MyButtonDisplayName\\", \\"props\\": { \\"text\\": { \\"defaultValue\\": null, \\"description\\": \\"\\", \\"name\\": \\"text\\", \\"required\\": true, \\"type\\": { \\"name\\": \\"string\\" } } } };
97+
}
98+
catch (__react_docgen_typescript_loader_error) { }"
99+
`;
100+
79101
exports[`component fixture HyphenatedPropName.tsx has code block generated 1`] = `
80102
"import * as React from \\"react\\";
81103

src/__tests__/generateDocgenCodeBlock.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {
66
GeneratorOptions,
77
} from "../generateDocgenCodeBlock";
88

9-
function getGeneratorOptions(parserOptions: ParserOptions = {}) {
9+
const defaultParserOptions = { shouldIncludeExpression: true };
10+
11+
function getGeneratorOptions(parserOptions: ParserOptions) {
1012
return (filename: string) => {
1113
const filePath = path.resolve(__dirname, "__fixtures__", filename);
1214

@@ -24,7 +26,7 @@ function getGeneratorOptions(parserOptions: ParserOptions = {}) {
2426
function loadFixtureTests(): GeneratorOptions[] {
2527
return fs
2628
.readdirSync(path.resolve(__dirname, "__fixtures__"))
27-
.map(getGeneratorOptions());
29+
.map(getGeneratorOptions(defaultParserOptions));
2830
}
2931

3032
const fixtureTests: GeneratorOptions[] = loadFixtureTests();
@@ -52,9 +54,10 @@ it("adds component to docgen collection", () => {
5254
it("generates value info for enums", () => {
5355
expect(
5456
generateDocgenCodeBlock(
55-
getGeneratorOptions({ shouldExtractLiteralValuesFromEnum: true })(
56-
"DefaultPropValue.tsx"
57-
)
57+
getGeneratorOptions({
58+
...defaultParserOptions,
59+
shouldExtractLiteralValuesFromEnum: true,
60+
})("DefaultPropValue.tsx")
5861
)
5962
).toMatchSnapshot();
6063
});

src/generateDocgenCodeBlock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ function setComponentDocGen(
305305
ts.factory.createBinaryExpression(
306306
// SimpleComponent.__docgenInfo
307307
ts.factory.createPropertyAccessExpression(
308-
ts.factory.createIdentifier(d.displayName),
308+
ts.factory.createIdentifier(d.expression?.getName() || d.displayName),
309309
ts.factory.createIdentifier("__docgenInfo")
310310
),
311311
ts.SyntaxKind.EqualsToken,

src/plugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,10 @@ export default class DocgenPlugin implements webpack.WebpackPluginInstance {
405405
}
406406

407407
return {
408-
docgenOptions,
408+
docgenOptions: {
409+
shouldIncludeExpression: true,
410+
...docgenOptions,
411+
},
409412
generateOptions: {
410413
docgenCollectionName:
411414
docgenCollectionName === undefined

0 commit comments

Comments
 (0)