Skip to content

Commit 315f881

Browse files
authored
fix: Correctly read descriptions of local spread objects (#282)
1 parent 44a223e commit 315f881

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

src/__tests__/__snapshots__/main-test.js.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,3 +865,25 @@ Object {
865865
},
866866
}
867867
`;
868+
869+
exports[`main fixtures processes component "component_16.js" without errors 1`] = `
870+
Object {
871+
"description": "",
872+
"displayName": "Foo",
873+
"methods": Array [],
874+
"props": Object {
875+
"bar": Object {
876+
"defaultValue": Object {
877+
"computed": false,
878+
"value": "null",
879+
},
880+
"description": "This does something.",
881+
"required": false,
882+
"type": Object {
883+
"name": "custom",
884+
"raw": "PropTypes.node",
885+
},
886+
},
887+
},
888+
}
889+
`;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const propTypes = {
2+
/** This does something. */
3+
bar: PropTypes.node,
4+
};
5+
6+
export default function Foo(props) {
7+
return <div />;
8+
}
9+
10+
Foo.propTypes = {
11+
...propTypes
12+
};
13+
14+
Foo.defaultProps = {
15+
bar: null,
16+
};

src/handlers/propDocBlockHandler.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ const {
2121
types: { namedTypes: types },
2222
} = recast;
2323

24+
function resolveDocumentation(
25+
documentation: Documentation,
26+
path: NodePath,
27+
): void {
28+
if (!types.ObjectExpression.check(path.node)) {
29+
return;
30+
}
31+
32+
path.get('properties').each(propertyPath => {
33+
if (types.Property.check(propertyPath.node)) {
34+
setPropDescription(documentation, propertyPath);
35+
} else if (types.SpreadElement.check(propertyPath.node)) {
36+
const resolvedValuePath = resolveToValue(propertyPath.get('argument'));
37+
resolveDocumentation(documentation, resolvedValuePath);
38+
}
39+
});
40+
}
41+
2442
export default function propDocBlockHandler(
2543
documentation: Documentation,
2644
path: NodePath,
@@ -30,14 +48,9 @@ export default function propDocBlockHandler(
3048
return;
3149
}
3250
propTypesPath = resolveToValue(propTypesPath);
33-
if (!propTypesPath || !types.ObjectExpression.check(propTypesPath.node)) {
51+
if (!propTypesPath) {
3452
return;
3553
}
3654

37-
propTypesPath.get('properties').each(propertyPath => {
38-
// we only support documentation of actual properties, not spread
39-
if (types.Property.check(propertyPath.node)) {
40-
setPropDescription(documentation, propertyPath);
41-
}
42-
});
55+
resolveDocumentation(documentation, propTypesPath);
4356
}

0 commit comments

Comments
 (0)