Skip to content

Commit 6fcef77

Browse files
committed
Ignore non-ObjectExpression's in propTypeHandler.
If `propTypes` is a member expression, the handler used to throw the error TypeError: Cannot read property 'length' of undefined at NodePath.each (/react-docgen/node_modules/ast-types/lib/path.js:82:25) at amendPropTypes (/react-docgen/dist/handlers/propTypeHandler.js:69:26) at propTypeHandler (/react-docgen/dist/handlers/propTypeHandler.js:103:3)
1 parent 4e3f966 commit 6fcef77

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/handlers/__tests__/propTypeHandler-test.js

+15
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,20 @@ describe('propTypeHandler', () => {
202202
definition = statement('class Foo {}');
203203
expect(() => propTypeHandler(documentation, definition))
204204
.not.toThrow();
205+
206+
definition = statement('function Foo() {}');
207+
expect(() => propTypeHandler(documentation, definition))
208+
.not.toThrow();
209+
210+
definition = expression('() => {}');
211+
expect(() => propTypeHandler(documentation, definition))
212+
.not.toThrow();
213+
});
214+
215+
// This case is handled by propTypeCompositionHandler
216+
it('does not error if propTypes is a member expression', () => {
217+
var definition = expression('{propTypes: Foo.propTypes}');
218+
expect(() => propTypeHandler(documentation, definition))
219+
.not.toThrow();
205220
});
206221
});

src/handlers/propTypeHandler.js

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function isPropTypesExpression(path) {
3434
}
3535

3636
function amendPropTypes(documentation, path) {
37+
if (!types.ObjectExpression.check(path.node)) {
38+
return;
39+
}
40+
3741
path.get('properties').each(function(propertyPath) {
3842
switch (propertyPath.node.type) {
3943
case types.Property.name:

0 commit comments

Comments
 (0)