Skip to content

Correctly resolve values in Object.values #769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2023
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/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