Skip to content

Commit a91043f

Browse files
committed
Use component expression name as identifier
Instead of displayName. Fixes #51
1 parent 966f4db commit a91043f

4 files changed

Lines changed: 14 additions & 8 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ try {
9393
// @ts-ignore
9494
MyButtonDisplayName.displayName = \\"MyButtonDisplayName\\";
9595
// @ts-ignore
96-
MyButtonDisplayName.__docgenInfo = { \\"description\\": \\"\\", \\"displayName\\": \\"MyButtonDisplayName\\", \\"props\\": { \\"text\\": { \\"defaultValue\\": null, \\"description\\": \\"\\", \\"name\\": \\"text\\", \\"required\\": true, \\"type\\": { \\"name\\": \\"string\\" } } } };
96+
Button.__docgenInfo = { \\"description\\": \\"\\", \\"displayName\\": \\"MyButtonDisplayName\\", \\"props\\": { \\"text\\": { \\"defaultValue\\": null, \\"description\\": \\"\\", \\"name\\": \\"text\\", \\"required\\": true, \\"type\\": { \\"name\\": \\"string\\" } } } };
9797
}
9898
catch (__react_docgen_typescript_loader_error) { }"
9999
`;

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)