Skip to content

Commit

Permalink
Only add react-docgen info in the file a component is defined
Browse files Browse the repository at this point in the history
This avoids errors when a component is being imported and re-exported under
a different name via a barrel file or similar.
  • Loading branch information
glenjamin committed Apr 28, 2024
1 parent 7e1ee32 commit cf562cc
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
7 changes: 4 additions & 3 deletions code/presets/react-webpack/src/loaders/react-docgen-loader.ts
Expand Up @@ -22,6 +22,7 @@ import {
const { getNameOrValue, isReactForwardRefCall } = utils;

const actualNameHandler: Handler = function actualNameHandler(documentation, componentDefinition) {
documentation.set('definedInFile', componentDefinition.hub.file.opts.filename);
if (
(componentDefinition.isClassDeclaration() || componentDefinition.isFunctionDeclaration()) &&
componentDefinition.has('id')
Expand Down Expand Up @@ -58,7 +59,7 @@ const actualNameHandler: Handler = function actualNameHandler(documentation, com
}
};

type DocObj = Documentation & { actualName: string };
type DocObj = Documentation & { actualName: string; definedInFile: string };

const defaultHandlers = Object.values(docgenHandlers).map((handler) => handler);
const defaultResolver = new docgenResolver.FindExportedDefinitionsResolver();
Expand Down Expand Up @@ -107,8 +108,8 @@ export default async function reactDocgenLoader(
const magicString = new MagicString(source);

docgenResults.forEach((info) => {
const { actualName, ...docgenInfo } = info;
if (actualName) {
const { actualName, definedInFile, ...docgenInfo } = info;
if (actualName && definedInFile == this.resourcePath) {
const docNode = JSON.stringify(docgenInfo);
magicString.append(`;${actualName}.__docgenInfo=${docNode}`);
}
Expand Down
1 change: 1 addition & 0 deletions code/renderers/react/src/docs/extractArgTypes.test.ts
Expand Up @@ -50,6 +50,7 @@ const annotateWithDocgen = (inputPath: string) => {
const skippedTests = [
'js-class-component',
'js-function-component',
'js-re-exported-component',
'js-function-component-inline-defaults',
'js-function-component-inline-defaults-no-propTypes',
'ts-function-component',
Expand Down
@@ -0,0 +1 @@
export { component } from '../js-function-component/input.jsx';
@@ -0,0 +1,3 @@
import { component } from '../js-function-component/input.jsx';

export { component };
5 changes: 5 additions & 0 deletions code/renderers/react/template/stories/js-argtypes.stories.jsx
Expand Up @@ -7,6 +7,7 @@ import { ThemeProvider, themes, convert } from '@storybook/theming';

import { component as JsClassComponentComponent } from './docgen-components/js-class-component/input.jsx';
import { component as JsFunctionComponentComponent } from './docgen-components/js-function-component/input.jsx';
import { component as JsRexportedComponentComponent } from './docgen-components/js-re-exported-component/input.jsx';
import { component as JsFunctionComponentInlineDefaultsComponent } from './docgen-components/js-function-component-inline-defaults/input.jsx';
import { component as JsFunctionComponentInlineDefaultsNoPropTypesComponent } from './docgen-components/js-function-component-inline-defaults-no-propTypes/input.jsx';
import { component as JsProptypesShapeComponent } from './docgen-components/9399-js-proptypes-shape/input.jsx';
Expand Down Expand Up @@ -60,6 +61,10 @@ export const JsClassComponent = { parameters: { component: JsClassComponentCompo

export const JsFunctionComponent = { parameters: { component: JsFunctionComponentComponent } };

export const JsRexportedComponent = {
parameters: { component: JsRexportedComponentComponent },
};

export const JsFunctionComponentInlineDefaults = {
parameters: { component: JsFunctionComponentInlineDefaultsComponent },
};
Expand Down

0 comments on commit cf562cc

Please sign in to comment.