diff --git a/.changeset/nasty-onions-fly.md b/.changeset/nasty-onions-fly.md new file mode 100644 index 00000000000..cbb01d6bcfa --- /dev/null +++ b/.changeset/nasty-onions-fly.md @@ -0,0 +1,5 @@ +--- +'react-docgen': patch +--- + +Fix handling of `PropTypes.onOf` to handle unresolved imported values correctly diff --git a/packages/react-docgen/src/utils/__tests__/__snapshots__/getPropType-test.ts.snap b/packages/react-docgen/src/utils/__tests__/__snapshots__/getPropType-test.ts.snap index 13fdfae356c..c70b3d38f0b 100644 --- a/packages/react-docgen/src/utils/__tests__/__snapshots__/getPropType-test.ts.snap +++ b/packages/react-docgen/src/utils/__tests__/__snapshots__/getPropType-test.ts.snap @@ -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", diff --git a/packages/react-docgen/src/utils/__tests__/getPropType-test.ts b/packages/react-docgen/src/utils/__tests__/getPropType-test.ts index ed86d2d5c92..bb999613058 100644 --- a/packages/react-docgen/src/utils/__tests__/getPropType-test.ts +++ b/packages/react-docgen/src/utils/__tests__/getPropType-test.ts @@ -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( + ` + 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( + ` + 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( diff --git a/packages/react-docgen/src/utils/__tests__/resolveObjectKeysToArray-test.ts b/packages/react-docgen/src/utils/__tests__/resolveObjectKeysToArray-test.ts index 0c06830d979..ee06409a02f 100644 --- a/packages/react-docgen/src/utils/__tests__/resolveObjectKeysToArray-test.ts +++ b/packages/react-docgen/src/utils/__tests__/resolveObjectKeysToArray-test.ts @@ -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); + }); }); diff --git a/packages/react-docgen/src/utils/__tests__/resolveObjectValuesToArray-test.ts b/packages/react-docgen/src/utils/__tests__/resolveObjectValuesToArray-test.ts index 9872a1274b5..797c6507cb6 100644 --- a/packages/react-docgen/src/utils/__tests__/resolveObjectValuesToArray-test.ts +++ b/packages/react-docgen/src/utils/__tests__/resolveObjectValuesToArray-test.ts @@ -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); + }); }); diff --git a/packages/react-docgen/src/utils/getPropType.ts b/packages/react-docgen/src/utils/getPropType.ts index 942f14120af..5096dde6ed4 100644 --- a/packages/react-docgen/src/utils/getPropType.ts +++ b/packages/react-docgen/src/utils/getPropType.ts @@ -37,7 +37,7 @@ function getEnumValuesFromArrayExpression( const value = resolveToValue(elementPath as NodePath); return values.push({ - value: printValue(value), + value: printValue(value.isImportDeclaration() ? elementPath : value), computed: !value.isLiteral(), }); });