Skip to content
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/tired-ideas-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-docgen': patch
---

Do not fail when resolving inside ObjectMethod nodes
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { ObjectMethod, VariableDeclaration } from '@babel/types';
import { parse } from '../../../tests/utils';
import getMemberExpressionValuePath from '../getMemberExpressionValuePath.js';
import { describe, expect, test } from 'vitest';
import type { NodePath } from '@babel/traverse';

describe('getMemberExpressionValuePath', () => {
describe('MethodExpression', () => {
Expand Down Expand Up @@ -101,4 +103,23 @@ describe('getMemberExpressionValuePath', () => {
);
});
});
describe('ObjectMethod', () => {
test('ignores ObjectMethod', () => {
const def = parse.statement<VariableDeclaration>(`
const slice = createSlice({
example(state, action) {
},
});
`);

// path to `action.payload.id`
const path = def
.get('declarations')[0]
.get('init')
.get('arguments')[0]
.get('properties')[0] as NodePath<ObjectMethod>;

expect(getMemberExpressionValuePath(path, 'images')).toBe(null);
});
});
});
26 changes: 26 additions & 0 deletions packages/react-docgen/src/utils/__tests__/resolveToValue-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,30 @@ describe('resolveToValue', () => {
expect(resolveToValue(path)).toBe(path);
});
});

describe('ObjectMethod', () => {
test('does not throw', () => {
const def = parse.statement(
`const slice = createSlice({
example(state, action) {
state.images[action.payload.id] = action.payload.content;
},
});`,
);

// path to `action.payload.id`
const path = def
.get('declarations')[0]
.get('init')
.get('arguments')[0]
.get('properties')[0]
.get('body')
.get('body')[0]
.get('expression')
.get('left')
.get('property');

expect(() => resolveToValue(path)).not.toThrow();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ function resolveName(path: NodePath): string | undefined {
return;
}

// When we check ObjectMethod we simply ignore it as assigning
// to it is technicaly possible but rare
if (path.isObjectMethod()) {
return;
}

if (
path.isFunctionExpression() ||
path.isArrowFunctionExpression() ||
Expand Down
Loading