Skip to content
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

Fix prop-types oneOf not handling unresolved imports #767

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/nasty-onions-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-docgen': patch
---

Fix handling of `PropTypes.onOf` to handle unresolved imported values correctly
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,38 @@ exports[`getPropType > resolve identifier to their values > does resolve object
}
`;

exports[`getPropType > resolve identifier to their values > handles unresolved imported identifier to their initialization value in array 1`] = `
{
"name": "enum",
"value": [
{
"computed": true,
"value": "FOO",
},
{
"computed": true,
"value": "BAR",
},
],
}
`;

exports[`getPropType > resolve identifier to their values > handles unresolved named imported identifier to their initialization value in array 1`] = `
{
"name": "enum",
"value": [
{
"computed": true,
"value": "FOO",
},
{
"computed": true,
"value": "BAR",
},
],
}
`;

exports[`getPropType > resolve identifier to their values > resolves imported identifier to their initialization value in array 1`] = `
{
"name": "enum",
Expand Down
28 changes: 28 additions & 0 deletions packages/react-docgen/src/utils/__tests__/getPropType-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,34 @@ describe('getPropType', () => {
expect(getPropType(identifierInsideArray)).toMatchSnapshot();
});

test('handles unresolved imported identifier to their initialization value in array', () => {
const identifierInsideArray = parse
.statement<ExpressionStatement>(
`
PropTypes.oneOf([FOO, BAR]);
import FOO from 'foo';
import BAR from 'bar';
`,
)
.get('expression');

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

test('handles unresolved named imported identifier to their initialization value in array', () => {
const identifierInsideArray = parse
.statement<ExpressionStatement>(
`
PropTypes.oneOf([FOO, BAR]);
import { FOO } from 'foo';
import { BAR } from 'bar';
`,
)
.get('expression');

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

test('resolves memberExpressions', () => {
const propTypeExpression = parse
.statement<ExpressionStatement>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,23 @@ describe('resolveObjectKeysToArray', () => {

expect(resolveObjectKeysToArray(path)).toMatchSnapshot();
});

test('can handle unresolved imported objects passed to Object.values', () => {
const path = parse.expressionLast(
`import foo from 'foo';
Object.keys(foo);`,
);

expect(resolveObjectKeysToArray(path)).toBe(null);
});

test('can handle unresolve spreads from imported objects', () => {
const path = parse.expressionLast(
`import bar from 'bar';
var abc = { foo: 'foo', baz: 'baz', ...bar };
Object.keys(abc);`,
);

expect(resolveObjectKeysToArray(path)).toBe(null);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,33 @@ describe('resolveObjectValuesToArray', () => {

expect(resolveObjectValuesToArray(path)).toMatchSnapshot();
});

test('can handle unresolved imported objects passed to Object.values', () => {
const path = parse.expressionLast(
`import foo from 'foo';
Object.values(foo);`,
);

expect(resolveObjectValuesToArray(path)).toBe(null);
});

test('can handle unresolve spreads from imported objects', () => {
const path = parse.expressionLast(
`import bar from 'bar';
var abc = { foo: 'foo', baz: 'baz', ...bar };
Object.keys(abc);`,
);

expect(resolveObjectValuesToArray(path)).toBe(null);
});

test('can handle unresolve object value from imported objects', () => {
const path = parse.expressionLast(
`import bar from 'bar';
var abc = { foo: 'foo', baz: bar };
Object.keys(abc);`,
);

expect(resolveObjectValuesToArray(path)).toBe(null);
});
});
2 changes: 1 addition & 1 deletion packages/react-docgen/src/utils/getPropType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function getEnumValuesFromArrayExpression(
const value = resolveToValue(elementPath as NodePath<Expression>);

return values.push({
value: printValue(value),
value: printValue(value.isImportDeclaration() ? elementPath : value),
computed: !value.isLiteral(),
});
});
Expand Down