Skip to content

Commit a684d82

Browse files
committed
Fix prop-types oneOf not handling unresolved imports
1 parent e86f680 commit a684d82

File tree

6 files changed

+114
-1
lines changed

6 files changed

+114
-1
lines changed

Diff for: .changeset/nasty-onions-fly.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-docgen': patch
3+
---
4+
5+
Fix handling of `PropTypes.onOf` to handle unresolved imported values correctly

Diff for: packages/react-docgen/src/utils/__tests__/__snapshots__/getPropType-test.ts.snap

+32
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,38 @@ exports[`getPropType > resolve identifier to their values > does resolve object
218218
}
219219
`;
220220

221+
exports[`getPropType > resolve identifier to their values > handles unresolved imported identifier to their initialization value in array 1`] = `
222+
{
223+
"name": "enum",
224+
"value": [
225+
{
226+
"computed": true,
227+
"value": "FOO",
228+
},
229+
{
230+
"computed": true,
231+
"value": "BAR",
232+
},
233+
],
234+
}
235+
`;
236+
237+
exports[`getPropType > resolve identifier to their values > handles unresolved named imported identifier to their initialization value in array 1`] = `
238+
{
239+
"name": "enum",
240+
"value": [
241+
{
242+
"computed": true,
243+
"value": "FOO",
244+
},
245+
{
246+
"computed": true,
247+
"value": "BAR",
248+
},
249+
],
250+
}
251+
`;
252+
221253
exports[`getPropType > resolve identifier to their values > resolves imported identifier to their initialization value in array 1`] = `
222254
{
223255
"name": "enum",

Diff for: packages/react-docgen/src/utils/__tests__/getPropType-test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,34 @@ describe('getPropType', () => {
286286
expect(getPropType(identifierInsideArray)).toMatchSnapshot();
287287
});
288288

289+
test('handles unresolved imported identifier to their initialization value in array', () => {
290+
const identifierInsideArray = parse
291+
.statement<ExpressionStatement>(
292+
`
293+
PropTypes.oneOf([FOO, BAR]);
294+
import FOO from 'foo';
295+
import BAR from 'bar';
296+
`,
297+
)
298+
.get('expression');
299+
300+
expect(getPropType(identifierInsideArray)).toMatchSnapshot();
301+
});
302+
303+
test('handles unresolved named imported identifier to their initialization value in array', () => {
304+
const identifierInsideArray = parse
305+
.statement<ExpressionStatement>(
306+
`
307+
PropTypes.oneOf([FOO, BAR]);
308+
import { FOO } from 'foo';
309+
import { BAR } from 'bar';
310+
`,
311+
)
312+
.get('expression');
313+
314+
expect(getPropType(identifierInsideArray)).toMatchSnapshot();
315+
});
316+
289317
test('resolves memberExpressions', () => {
290318
const propTypeExpression = parse
291319
.statement<ExpressionStatement>(

Diff for: packages/react-docgen/src/utils/__tests__/resolveObjectKeysToArray-test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,23 @@ describe('resolveObjectKeysToArray', () => {
139139

140140
expect(resolveObjectKeysToArray(path)).toMatchSnapshot();
141141
});
142+
143+
test('can handle unresolved imported objects passed to Object.values', () => {
144+
const path = parse.expressionLast(
145+
`import foo from 'foo';
146+
Object.keys(foo);`,
147+
);
148+
149+
expect(resolveObjectKeysToArray(path)).toBe(null);
150+
});
151+
152+
test('can handle unresolve spreads from imported objects', () => {
153+
const path = parse.expressionLast(
154+
`import bar from 'bar';
155+
var abc = { foo: 'foo', baz: 'baz', ...bar };
156+
Object.keys(abc);`,
157+
);
158+
159+
expect(resolveObjectKeysToArray(path)).toBe(null);
160+
});
142161
});

Diff for: packages/react-docgen/src/utils/__tests__/resolveObjectValuesToArray-test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,33 @@ describe('resolveObjectValuesToArray', () => {
164164

165165
expect(resolveObjectValuesToArray(path)).toMatchSnapshot();
166166
});
167+
168+
test('can handle unresolved imported objects passed to Object.values', () => {
169+
const path = parse.expressionLast(
170+
`import foo from 'foo';
171+
Object.values(foo);`,
172+
);
173+
174+
expect(resolveObjectValuesToArray(path)).toBe(null);
175+
});
176+
177+
test('can handle unresolve spreads from imported objects', () => {
178+
const path = parse.expressionLast(
179+
`import bar from 'bar';
180+
var abc = { foo: 'foo', baz: 'baz', ...bar };
181+
Object.keys(abc);`,
182+
);
183+
184+
expect(resolveObjectValuesToArray(path)).toBe(null);
185+
});
186+
187+
test('can handle unresolve object value from imported objects', () => {
188+
const path = parse.expressionLast(
189+
`import bar from 'bar';
190+
var abc = { foo: 'foo', baz: bar };
191+
Object.keys(abc);`,
192+
);
193+
194+
expect(resolveObjectValuesToArray(path)).toBe(null);
195+
});
167196
});

Diff for: packages/react-docgen/src/utils/getPropType.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function getEnumValuesFromArrayExpression(
3737
const value = resolveToValue(elementPath as NodePath<Expression>);
3838

3939
return values.push({
40-
value: printValue(value),
40+
value: printValue(value.isImportDeclaration() ? elementPath : value),
4141
computed: !value.isLiteral(),
4242
});
4343
});

0 commit comments

Comments
 (0)