Skip to content

Commit a122bff

Browse files
DiogoDoretodanez
andauthored
fix(react-docgen): add ObjectMethod support to resolveName (#1036)
* fix(react-docgen): add ObjectMethod support to resolveName Extend the resolveName function in getMemberExpressionValuePath.ts to handle ObjectMethod nodes, preventing the error below from being thrown during AST resolution of member expressions within object methods. TypeError: Attempted to resolveName for an unsupported path. resolveName does not accept ObjectMethod. Fix #902 * refactor: extract ObjectMethod handling to a dedicated condition * ignore ObjectMethod --------- Co-authored-by: Daniel Tschinder <[email protected]>
1 parent c1e7ddd commit a122bff

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

.changeset/tired-ideas-invent.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-docgen': patch
3+
---
4+
5+
Do not fail when resolving inside ObjectMethod nodes

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import type { ObjectMethod, VariableDeclaration } from '@babel/types';
12
import { parse } from '../../../tests/utils';
23
import getMemberExpressionValuePath from '../getMemberExpressionValuePath.js';
34
import { describe, expect, test } from 'vitest';
5+
import type { NodePath } from '@babel/traverse';
46

57
describe('getMemberExpressionValuePath', () => {
68
describe('MethodExpression', () => {
@@ -101,4 +103,23 @@ describe('getMemberExpressionValuePath', () => {
101103
);
102104
});
103105
});
106+
describe('ObjectMethod', () => {
107+
test('ignores ObjectMethod', () => {
108+
const def = parse.statement<VariableDeclaration>(`
109+
const slice = createSlice({
110+
example(state, action) {
111+
},
112+
});
113+
`);
114+
115+
// path to `action.payload.id`
116+
const path = def
117+
.get('declarations')[0]
118+
.get('init')
119+
.get('arguments')[0]
120+
.get('properties')[0] as NodePath<ObjectMethod>;
121+
122+
expect(getMemberExpressionValuePath(path, 'images')).toBe(null);
123+
});
124+
});
104125
});

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,30 @@ describe('resolveToValue', () => {
269269
expect(resolveToValue(path)).toBe(path);
270270
});
271271
});
272+
273+
describe('ObjectMethod', () => {
274+
test('does not throw', () => {
275+
const def = parse.statement(
276+
`const slice = createSlice({
277+
example(state, action) {
278+
state.images[action.payload.id] = action.payload.content;
279+
},
280+
});`,
281+
);
282+
283+
// path to `action.payload.id`
284+
const path = def
285+
.get('declarations')[0]
286+
.get('init')
287+
.get('arguments')[0]
288+
.get('properties')[0]
289+
.get('body')
290+
.get('body')[0]
291+
.get('expression')
292+
.get('left')
293+
.get('property');
294+
295+
expect(() => resolveToValue(path)).not.toThrow();
296+
});
297+
});
272298
});

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ function resolveName(path: NodePath): string | undefined {
3737
return;
3838
}
3939

40+
// When we check ObjectMethod we simply ignore it as assigning
41+
// to it is technicaly possible but rare
42+
if (path.isObjectMethod()) {
43+
return;
44+
}
45+
4046
if (
4147
path.isFunctionExpression() ||
4248
path.isArrowFunctionExpression() ||

0 commit comments

Comments
 (0)