Skip to content

Commit 0aa6cdc

Browse files
committed
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
1 parent d91aba5 commit 0aa6cdc

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function resolveName(path: NodePath): string | undefined {
4242
path.isArrowFunctionExpression() ||
4343
path.isTaggedTemplateExpression() ||
4444
path.isCallExpression() ||
45+
path.isObjectMethod() ||
4546
isReactForwardRefCall(path)
4647
) {
4748
let currentPath: NodePath = path;

0 commit comments

Comments
 (0)