Skip to content

Commit d27b6bc

Browse files
committed
ignore ObjectMethod
1 parent dc3acdf commit d27b6bc

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

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/getMemberExpressionValuePath.ts

Lines changed: 6 additions & 10 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() ||
@@ -63,16 +69,6 @@ function resolveName(path: NodePath): string | undefined {
6369
return;
6470
}
6571

66-
if (path.isObjectMethod()) {
67-
const key = path.get('key');
68-
69-
if (key.isIdentifier()) {
70-
return key.node.name;
71-
}
72-
73-
return;
74-
}
75-
7672
throw new TypeError(
7773
'Attempted to resolveName for an unsupported path. resolveName does not accept ' +
7874
path.node.type +

0 commit comments

Comments
 (0)