Skip to content

Commit e08e08d

Browse files
committed
Correctly resolve values in Object.values
1 parent a684d82 commit e08e08d

File tree

4 files changed

+75
-3
lines changed

4 files changed

+75
-3
lines changed

Diff for: .changeset/witty-ligers-press.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-docgen': patch
3+
---
4+
5+
Correctly resolve the values in an `Object.values()` call

Diff for: packages/react-docgen/src/utils/__tests__/__snapshots__/getPropType-test.ts.snap

+32
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,22 @@ exports[`getPropType > resolve identifier to their values > handles unresolved n
250250
}
251251
`;
252252

253+
exports[`getPropType > resolve identifier to their values > handles unresolved value constants from imported objects 1`] = `
254+
{
255+
"name": "enum",
256+
"value": [
257+
{
258+
"computed": false,
259+
"value": "null",
260+
},
261+
{
262+
"computed": false,
263+
"value": "null",
264+
},
265+
],
266+
}
267+
`;
268+
253269
exports[`getPropType > resolve identifier to their values > resolves imported identifier to their initialization value in array 1`] = `
254270
{
255271
"name": "enum",
@@ -358,6 +374,22 @@ exports[`getPropType > resolve identifier to their values > resolves simple iden
358374
}
359375
`;
360376

377+
exports[`getPropType > resolve identifier to their values > resolves value constants from imported objects 1`] = `
378+
{
379+
"name": "enum",
380+
"value": [
381+
{
382+
"computed": false,
383+
"value": "\\"foo\\"",
384+
},
385+
{
386+
"computed": false,
387+
"value": "\\"bar\\"",
388+
},
389+
],
390+
}
391+
`;
392+
361393
exports[`getPropType > resolve identifier to their values > resolves values from imported Object.keys call 1`] = `
362394
{
363395
"name": "enum",

Diff for: packages/react-docgen/src/utils/__tests__/getPropType-test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,41 @@ describe('getPropType', () => {
436436
expect(getPropType(propTypeExpression)).toMatchSnapshot();
437437
});
438438

439+
test('resolves value constants from imported objects', () => {
440+
const propTypeExpression = parse
441+
.statement<ExpressionStatement>(
442+
`
443+
const values = {
444+
FOO: consts.FOO, BAR: consts.BAR
445+
}
446+
PropTypes.oneOf(Object.values(values));
447+
import consts from 'obj';
448+
`,
449+
mockImporter,
450+
1,
451+
)
452+
.get('expression');
453+
454+
expect(getPropType(propTypeExpression)).toMatchSnapshot();
455+
});
456+
457+
test('handles unresolved value constants from imported objects', () => {
458+
const propTypeExpression = parse
459+
.statement<ExpressionStatement>(
460+
`
461+
const values = {
462+
FOO: consts.FOO, BAR: consts.BAR
463+
}
464+
PropTypes.oneOf(Object.values(values));
465+
import consts from 'obj';
466+
`,
467+
1,
468+
)
469+
.get('expression');
470+
471+
expect(getPropType(propTypeExpression)).toMatchSnapshot();
472+
});
473+
439474
test('does not resolve external values without proper importer', () => {
440475
const propTypeExpression = parse
441476
.statement<ExpressionStatement>(

Diff for: packages/react-docgen/src/utils/resolveObjectValuesToArray.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ function resolveObjectToPropMap(object: NodePath): Map<string, string> | null {
5252
return;
5353
}
5454

55-
// Identifiers as values are not followed at all
56-
const valuePath = propPath.get('value');
55+
const valuePath = resolveToValue(propPath.get('value'));
5756
const value = valuePath.isStringLiteral()
5857
? `"${valuePath.node.value}"`
5958
: valuePath.isNumericLiteral()
6059
? `${valuePath.node.value}`
61-
: 'null';
60+
: // we return null here because there are a lot of cases and we don't know yet what we need to handle
61+
'null';
6262

6363
values.set(name, value);
6464
} else if (propPath.isSpreadElement()) {

0 commit comments

Comments
 (0)