Skip to content

Commit

Permalink
Correctly resolve values in Object.values
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Mar 26, 2023
1 parent a684d82 commit 510ec2c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-ligers-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-docgen': patch
---

Correctly resolve the values in an `Object.values()` call
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,22 @@ exports[`getPropType > resolve identifier to their values > handles unresolved n
}
`;

exports[`getPropType > resolve identifier to their values > handles unresolved value constants from imported objects 1`] = `
{
"name": "enum",
"value": [
{
"computed": false,
"value": "null",
},
{
"computed": false,
"value": "null",
},
],
}
`;

exports[`getPropType > resolve identifier to their values > resolves imported identifier to their initialization value in array 1`] = `
{
"name": "enum",
Expand Down Expand Up @@ -358,6 +374,22 @@ exports[`getPropType > resolve identifier to their values > resolves simple iden
}
`;

exports[`getPropType > resolve identifier to their values > resolves value constants from imported objects 1`] = `
{
"name": "enum",
"value": [
{
"computed": false,
"value": "\\"foo\\"",
},
{
"computed": false,
"value": "\\"bar\\"",
},
],
}
`;

exports[`getPropType > resolve identifier to their values > resolves values from imported Object.keys call 1`] = `
{
"name": "enum",
Expand Down
35 changes: 35 additions & 0 deletions packages/react-docgen/src/utils/__tests__/getPropType-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,41 @@ describe('getPropType', () => {
expect(getPropType(propTypeExpression)).toMatchSnapshot();
});

test('resolves value constants from imported objects', () => {
const propTypeExpression = parse
.statement<ExpressionStatement>(
`
const values = {
FOO: consts.FOO, BAR: consts.BAR
}
PropTypes.oneOf(Object.values(values));
import consts from 'obj';
`,
mockImporter,
1,
)
.get('expression');

expect(getPropType(propTypeExpression)).toMatchSnapshot();
});

test('handles unresolved value constants from imported objects', () => {
const propTypeExpression = parse
.statement<ExpressionStatement>(
`
const values = {
FOO: consts.FOO, BAR: consts.BAR
}
PropTypes.oneOf(Object.values(values));
import consts from 'obj';
`,
1,
)
.get('expression');

expect(getPropType(propTypeExpression)).toMatchSnapshot();
});

test('does not resolve external values without proper importer', () => {
const propTypeExpression = parse
.statement<ExpressionStatement>(
Expand Down
6 changes: 3 additions & 3 deletions packages/react-docgen/src/utils/resolveObjectValuesToArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ function resolveObjectToPropMap(object: NodePath): Map<string, string> | null {
return;
}

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

values.set(name, value);
} else if (propPath.isSpreadElement()) {
Expand Down

0 comments on commit 510ec2c

Please sign in to comment.