Skip to content

Commit

Permalink
Fix prop-types oneOf not handling unresolved imports
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Mar 26, 2023
1 parent e86f680 commit a684d82
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 1 deletion.
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

0 comments on commit a684d82

Please sign in to comment.