Skip to content

Commit eb0edaa

Browse files
committed
Fix argument detection for shapes
Currently, if a value is passed to `shape()` that cannot be resolved, such as in `shape(Child.propTypes)`, docgen returns "type": { "name": "shape", "value": "unkown" } Regardlessly of the typo, this behavior is not in line with the behavior of other types. Now, the following structure will be returned instead: "type": { "name": "shape", "value": "Child.propTypes", "computed": true } Fixes #87.
1 parent 86b167f commit eb0edaa

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,28 @@ Object {
5656
}
5757
}
5858
`;
59+
60+
exports[`main fixtures processes component "component_4.js" without errors 1`] = `
61+
Object {
62+
"description": "",
63+
"methods": Array [],
64+
"props": Object {
65+
"child": Object {
66+
"description": "",
67+
"required": true,
68+
"type": Object {
69+
"computed": true,
70+
"name": "shape",
71+
"value": "Child.propTypes"
72+
}
73+
},
74+
"something": Object {
75+
"description": "",
76+
"required": true,
77+
"type": Object {
78+
"name": "string"
79+
}
80+
}
81+
}
82+
}
83+
`;

src/__tests__/fixtures/component_4.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
*/
10+
11+
/**
12+
* Testing computed argument passed to shape()
13+
*/
14+
15+
import React from 'react';
16+
17+
var pt = React.PropTypes;
18+
19+
class Parent extends React.Component {
20+
static propTypes = {
21+
something: pt.string.isRequired,
22+
child: pt.shape(Child.propTypes).isRequired
23+
}
24+
}
25+
26+
export default Parent;

src/utils/__tests__/getPropType-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ describe('getPropType', () => {
125125
},
126126
},
127127
});
128+
129+
// computed
130+
expect(getPropType(expression('shape(Child.propTypes)'))).toEqual({
131+
name: 'shape',
132+
value: 'Child.propTypes',
133+
computed: true,
134+
});
128135
});
129136

130137
it('resolves variables to their values', () => {

src/utils/getPropType.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function getPropTypeObjectOf(argumentPath) {
8181
}
8282

8383
function getPropTypeShape(argumentPath) {
84-
var type: PropTypeDescriptor = {name: 'shape', value: 'unknown'};
84+
var type: PropTypeDescriptor = {name: 'shape'};
8585
if (!types.ObjectExpression.check(argumentPath.node)) {
8686
argumentPath = resolveToValue(argumentPath);
8787
}
@@ -101,6 +101,11 @@ function getPropTypeShape(argumentPath) {
101101
type.value = value;
102102
}
103103

104+
if (!type.value) {
105+
type.value = printValue(argumentPath);
106+
type.computed = true;
107+
}
108+
104109
return type;
105110
}
106111

0 commit comments

Comments
 (0)