Skip to content

Commit 713e848

Browse files
committed
Fix flow errors. All tests pass.
1 parent 27c9ec2 commit 713e848

File tree

5 files changed

+32
-28
lines changed

5 files changed

+32
-28
lines changed

flow/react-docgen.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@
88
*
99
*/
1010

11+
import type Documentation from '../src/Documentation';
12+
1113
type PropTypeDescriptor = {
1214
name: string;
1315
value?: any;
1416
raw?: string;
17+
computed?: boolean;
18+
// These are only needed for shape types.
19+
// Consider consolidating PropTypeDescriptor and PropDescriptor
20+
description?: string;
21+
required?: boolean;
1522
};
1623

1724
type PropDescriptor = {
@@ -21,15 +28,6 @@ type PropDescriptor = {
2128
description?: string;
2229
};
2330

24-
declare class Documentation {
25-
addComposes(moduleName: string): void;
26-
get(key: string): any;
27-
set(key: string, value: any): void;
28-
getPropDescriptor(propName: string): PropDescriptor;
29-
toObject(): Object;
30-
}
31-
32-
3331
type Handler = (documentation: Documentation, path: NodePath) => void;
3432
type Resolver =
3533
(node: ASTNode, recast: Recast) => (?NodePath|?Array<NodePath>);

src/Documentation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class Documentation {
1414
_props: Object;
15-
_composes: Array<string>;
15+
_composes: Set<string>;
1616
_data: Object;
1717

1818
constructor() {
@@ -29,7 +29,7 @@ class Documentation {
2929
this._data.set(key, value);
3030
}
3131

32-
get(key: string) {
32+
get(key: string): any {
3333
return this._data.get(key);
3434
}
3535

src/parse.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,17 @@ export default function parse(
5252
): Array<Object>|Object {
5353
var ast = recast.parse(src, {esprima: babylon});
5454
var componentDefinitions = resolver(ast.program, recast);
55-
var isArray = Array.isArray(componentDefinitions);
5655

57-
if (!componentDefinitions || (isArray && componentDefinitions.length === 0)) {
58-
throw new Error(ERROR_MISSING_DEFINITION);
56+
if (Array.isArray(componentDefinitions)) {
57+
if (componentDefinitions.length === 0) {
58+
throw new Error(ERROR_MISSING_DEFINITION);
59+
}
60+
return executeHandlers(handlers, componentDefinitions);
61+
} else if (componentDefinitions) {
62+
return executeHandlers(handlers, [(componentDefinitions: NodePath)])[0];
5963
}
6064

61-
return isArray ?
62-
executeHandlers(handlers, componentDefinitions) :
63-
executeHandlers(handlers, [componentDefinitions])[0];
65+
throw new Error(ERROR_MISSING_DEFINITION);
6466
}
6567

6668
export {ERROR_MISSING_DEFINITION};

src/utils/getPropType.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function getEnumValues(path) {
3333
}
3434

3535
function getPropTypeOneOf(argumentPath) {
36-
var type = {name: 'enum'};
36+
var type: PropTypeDescriptor = {name: 'enum'};
3737
if (!types.ArrayExpression.check(argumentPath.node)) {
3838
type.computed = true;
3939
type.value = printValue(argumentPath);
@@ -44,7 +44,7 @@ function getPropTypeOneOf(argumentPath) {
4444
}
4545

4646
function getPropTypeOneOfType(argumentPath) {
47-
var type = {name: 'union'};
47+
var type: PropTypeDescriptor = {name: 'union'};
4848
if (!types.ArrayExpression.check(argumentPath.node)) {
4949
type.computed = true;
5050
type.value = printValue(argumentPath);
@@ -55,7 +55,7 @@ function getPropTypeOneOfType(argumentPath) {
5555
}
5656

5757
function getPropTypeArrayOf(argumentPath) {
58-
var type = {name: 'arrayOf'};
58+
var type: PropTypeDescriptor = {name: 'arrayOf'};
5959
var subType = getPropType(argumentPath);
6060

6161
if (subType.name === 'unknown') {
@@ -68,22 +68,24 @@ function getPropTypeArrayOf(argumentPath) {
6868
}
6969

7070
function getPropTypeShape(argumentPath) {
71-
var type: {name: string; value: any;} = {name: 'shape', value: 'unkown'};
71+
var type: PropTypeDescriptor = {name: 'shape', value: 'unkown'};
7272
if (!types.ObjectExpression.check(argumentPath.node)) {
7373
argumentPath = resolveToValue(argumentPath);
7474
}
7575

7676
if (types.ObjectExpression.check(argumentPath.node)) {
77-
type.value = {};
77+
var value = {};
7878
argumentPath.get('properties').each(function(propertyPath) {
79-
var descriptor = getPropType(propertyPath.get('value'));
79+
var descriptor: PropDescriptor | PropTypeDescriptor =
80+
getPropType(propertyPath.get('value'));
8081
var docs = getDocblock(propertyPath);
8182
if (docs) {
8283
descriptor.description = docs;
8384
}
8485
descriptor.required = isRequiredPropType(propertyPath.get('value'));
85-
type.value[getPropertyName(propertyPath)] = descriptor;
86+
value[getPropertyName(propertyPath)] = descriptor;
8687
});
88+
type.value = value;
8789
}
8890

8991
return type;

src/utils/isStatelessComponent.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,14 @@ function returnsJSXElementOrReactCreateElementCall(path) {
101101
}
102102
}
103103

104-
if (types.ObjectExpression.check(resolvedValue.node)) {
104+
if (resolvedValue && types.ObjectExpression.check(resolvedValue.node)) {
105105
var resolvedMemberExpression = namesToResolve
106106
.reduce((result, path) => { // eslint-disable-line no-shadow
107-
result = getPropertyValuePath(result, path.node.name);
108-
if (types.Identifier.check(result.node)) {
109-
return resolveToValue(result);
107+
if (result) {
108+
result = getPropertyValuePath(result, path.node.name);
109+
if (result && types.Identifier.check(result.node)) {
110+
return resolveToValue(result);
111+
}
110112
}
111113
return result;
112114
}, resolvedValue);

0 commit comments

Comments
 (0)