Skip to content

copy comments when converting to class-properties #806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-camels-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-docgen': patch
---

Make docblocks for assigned methods be correctly detected.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function merge<
}
/**
* Extract info from the methods jsdoc blocks. Must be run after
* flowComponentMethodsHandler.
* componentMethodsHandler.
*/
const componentMethodsJsDocHandler: Handler = function (
documentation: Documentation,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { NodePath } from '@babel/traverse';
import type {
AssignmentExpression,
ClassDeclaration,
ClassMethod,
ClassPrivateMethod,
ClassProperty,
ExpressionStatement,
ObjectExpression,
ObjectMethod,
} from '@babel/types';
Expand Down Expand Up @@ -129,7 +131,7 @@ describe('getMethodDocumentation', () => {
});
});

test('extracts docblock on function assignment', () => {
test('extracts docblock on property assignment', () => {
const def = parse.statement<ClassDeclaration>(`
class Foo {
/**
Expand All @@ -148,6 +150,27 @@ describe('getMethodDocumentation', () => {
params: [],
});
});

test('extracts docblock on function assignment', () => {
const method = parse
.statementLast<ExpressionStatement>(
`const Foo = () => {}
/**
* Don't use this!
*/
Foo.foo = () => {}
`,
)
.get('expression') as NodePath<AssignmentExpression>;

expect(getMethodDocumentation(method)).toEqual({
name: 'foo',
docblock: "Don't use this!",
modifiers: ['static'],
returns: null,
params: [],
});
});
});

describe('parameters', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,20 @@ describe('normalizeClassDefinition', () => {

expect(classProperty).not.toBeDefined();
});

test('copies comments to class property', () => {
const classDefinition = parse.statement<ClassDeclaration>(`
class Foo {}

// my comment
Foo.propTypes = 42;
`);

normalizeClassDefinition(classDefinition);
const classProperty = classDefinition.node.body.body[0] as ClassProperty;

expect(classProperty).toBeDefined();
expect(classProperty.leadingComments?.length).toBe(1);
expect(classProperty.leadingComments?.[0].value).toBe(' my comment');
});
});
7 changes: 6 additions & 1 deletion packages/react-docgen/src/utils/normalizeClassDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { classProperty } from '@babel/types';
import { classProperty, inheritsComments } from '@babel/types';
import getMemberExpressionRoot from '../utils/getMemberExpressionRoot.js';
import getMembers from '../utils/getMembers.js';
import type { NodePath } from '@babel/traverse';
Expand Down Expand Up @@ -42,6 +42,11 @@ const explodedVisitors = visitors.explode<TraverseState>({
true,
);

inheritsComments(property, path.node);
if (path.parentPath.isExpressionStatement()) {
inheritsComments(property, path.parentPath.node);
}

state.classDefinition.get('body').unshiftContainer('body', property);
path.skip();
path.remove();
Expand Down