Skip to content

Commit 91b85f6

Browse files
committed
fix: Correctly handle ObjectProperties in isReactComponentMethod
1 parent 7dfb34e commit 91b85f6

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

packages/react-docgen/src/__tests__/__snapshots__/main-test.ts.snap

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,7 @@ exports[`main > React.createClass > parses with default resolver/handlers 1`] =
4444
{
4545
"description": "Example component description",
4646
"displayName": "ABC",
47-
"methods": [
48-
{
49-
"docblock": null,
50-
"modifiers": [],
51-
"name": "getDefaultProps",
52-
"params": [],
53-
"returns": null,
54-
},
55-
],
47+
"methods": [],
5648
"props": {
5749
"foo": {
5850
"defaultValue": {

packages/react-docgen/src/utils/__tests__/isReactComponentMethod-test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ describe('isReactComponentMethod', () => {
2525
expect(isReactComponentMethod(method)).toBe(true);
2626
});
2727

28+
test('returns true if the method is a component `createClass` object property with arrow function', () => {
29+
const def = parse.expression<ObjectExpression>('{ render: () => {}}');
30+
const method = def.get('properties')[0];
31+
32+
expect(isReactComponentMethod(method)).toBe(true);
33+
});
34+
35+
test('returns true if the method is a component `createClass` object property with function', () => {
36+
const def = parse.expression<ObjectExpression>('{ render: function () {}}');
37+
const method = def.get('properties')[0];
38+
39+
expect(isReactComponentMethod(method)).toBe(true);
40+
});
41+
2842
test('returns false if the method is not a component class method', () => {
2943
const def = parse.statement<ClassDeclaration>('class Foo { bar() {}}');
3044
const method = def.get('body').get('body')[0];

packages/react-docgen/src/utils/isReactComponentMethod.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ const componentMethods = [
2727
* Returns if the method path is a Component method.
2828
*/
2929
export default function (methodPath: NodePath): boolean {
30-
if (!methodPath.isClassMethod() && !methodPath.isObjectMethod()) {
30+
if (
31+
!methodPath.isClassMethod() &&
32+
!methodPath.isObjectMethod() &&
33+
!methodPath.isObjectProperty()
34+
) {
3135
return false;
3236
}
3337

0 commit comments

Comments
 (0)