Skip to content

Commit c7e2bd5

Browse files
authored
copy comments when converting to class-properties (#806)
1 parent 64e521e commit c7e2bd5

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

Diff for: .changeset/eleven-camels-sleep.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-docgen': patch
3+
---
4+
5+
Make docblocks for assigned methods be correctly detected.

Diff for: packages/react-docgen/src/handlers/componentMethodsJsDocHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function merge<
2828
}
2929
/**
3030
* Extract info from the methods jsdoc blocks. Must be run after
31-
* flowComponentMethodsHandler.
31+
* componentMethodsHandler.
3232
*/
3333
const componentMethodsJsDocHandler: Handler = function (
3434
documentation: Documentation,

Diff for: packages/react-docgen/src/utils/__tests__/getMethodDocumentation-test.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import type { NodePath } from '@babel/traverse';
22
import type {
3+
AssignmentExpression,
34
ClassDeclaration,
45
ClassMethod,
56
ClassPrivateMethod,
67
ClassProperty,
8+
ExpressionStatement,
79
ObjectExpression,
810
ObjectMethod,
911
} from '@babel/types';
@@ -129,7 +131,7 @@ describe('getMethodDocumentation', () => {
129131
});
130132
});
131133

132-
test('extracts docblock on function assignment', () => {
134+
test('extracts docblock on property assignment', () => {
133135
const def = parse.statement<ClassDeclaration>(`
134136
class Foo {
135137
/**
@@ -148,6 +150,27 @@ describe('getMethodDocumentation', () => {
148150
params: [],
149151
});
150152
});
153+
154+
test('extracts docblock on function assignment', () => {
155+
const method = parse
156+
.statementLast<ExpressionStatement>(
157+
`const Foo = () => {}
158+
/**
159+
* Don't use this!
160+
*/
161+
Foo.foo = () => {}
162+
`,
163+
)
164+
.get('expression') as NodePath<AssignmentExpression>;
165+
166+
expect(getMethodDocumentation(method)).toEqual({
167+
name: 'foo',
168+
docblock: "Don't use this!",
169+
modifiers: ['static'],
170+
returns: null,
171+
params: [],
172+
});
173+
});
151174
});
152175

153176
describe('parameters', () => {

Diff for: packages/react-docgen/src/utils/__tests__/normalizeClassDefinition-test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,20 @@ describe('normalizeClassDefinition', () => {
109109

110110
expect(classProperty).not.toBeDefined();
111111
});
112+
113+
test('copies comments to class property', () => {
114+
const classDefinition = parse.statement<ClassDeclaration>(`
115+
class Foo {}
116+
117+
// my comment
118+
Foo.propTypes = 42;
119+
`);
120+
121+
normalizeClassDefinition(classDefinition);
122+
const classProperty = classDefinition.node.body.body[0] as ClassProperty;
123+
124+
expect(classProperty).toBeDefined();
125+
expect(classProperty.leadingComments?.length).toBe(1);
126+
expect(classProperty.leadingComments?.[0].value).toBe(' my comment');
127+
});
112128
});

Diff for: packages/react-docgen/src/utils/normalizeClassDefinition.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { classProperty } from '@babel/types';
1+
import { classProperty, inheritsComments } from '@babel/types';
22
import getMemberExpressionRoot from '../utils/getMemberExpressionRoot.js';
33
import getMembers from '../utils/getMembers.js';
44
import type { NodePath } from '@babel/traverse';
@@ -42,6 +42,11 @@ const explodedVisitors = visitors.explode<TraverseState>({
4242
true,
4343
);
4444

45+
inheritsComments(property, path.node);
46+
if (path.parentPath.isExpressionStatement()) {
47+
inheritsComments(property, path.parentPath.node);
48+
}
49+
4550
state.classDefinition.get('body').unshiftContainer('body', property);
4651
path.skip();
4752
path.remove();

0 commit comments

Comments
 (0)