Skip to content

Commit 3768744

Browse files
committed
fix: Output qualified type identifiers correctly
Also improves flow types
1 parent 46d418f commit 3768744

File tree

11 files changed

+207
-110
lines changed

11 files changed

+207
-110
lines changed

.eslintrc.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ module.exports = {
2020
},
2121
globals: {
2222
ASTNode: true,
23-
FlowTypeDescriptor: true,
24-
Handler: true,
2523
NodePath: true,
26-
PropDescriptor: true,
27-
PropTypeDescriptor: true,
28-
Resolver: true
24+
Recast: true
2925
}
3026
}

flow-typed/react-docgen.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"main": "dist/main.js",
2222
"scripts": {
23-
"build": "rimraf dist/ && babel src/ --out-dir dist/ --ignore __tests__,__mocks__",
23+
"build": "rimraf dist/ && babel src/ --out-dir dist/ --ignore __tests__,__mocks__,src/types.js",
2424
"lint": "eslint . --report-unused-disable-directives",
2525
"fix": "eslint . --fix --report-unused-disable-directives",
2626
"prepublish": "yarn build",

src/Documentation.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@
1010
*
1111
*/
1212

13+
import type { PropDescriptor } from './types';
14+
1315
export type DocumentationObject = {
14-
props?: Object,
15-
context?: Object,
16-
childContext?: Object,
16+
props?: { [string]: PropDescriptor },
17+
context?: { [string]: PropDescriptor },
18+
childContext?: { [string]: PropDescriptor },
1719
composes?: Array<string>,
1820
};
1921

2022
class Documentation {
21-
_props: Object;
22-
_context: Object;
23-
_childContext: Object;
23+
_props: Map<string, PropDescriptor>;
24+
_context: Map<string, PropDescriptor>;
25+
_childContext: Map<string, PropDescriptor>;
2426
_composes: Set<string>;
25-
_data: Object;
27+
_data: Map<string, any>;
2628

2729
constructor() {
2830
this._props = new Map();

src/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as AllResolver from './resolver';
1616
import * as utils from './utils';
1717
import type { Options } from './babelParser';
1818
import type { DocumentationObject } from './Documentation';
19+
import type { Handler, Resolver } from './types';
1920

2021
const defaultResolver = AllResolver.findExportedComponentDefinition;
2122
const defaultHandlers = [

src/parse.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import postProcessDocumentation from './utils/postProcessDocumentation';
1414

1515
import buildParser, { type Options } from './babelParser';
1616
import recast from 'recast';
17+
import type { Handler, Resolver } from './types';
1718

1819
const ERROR_MISSING_DEFINITION = 'No suitable component definition found.';
1920

src/types.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
import type Documentation from './Documentation';
12+
13+
export type PropTypeDescriptor = {
14+
name:
15+
| 'arrayOf'
16+
| 'custom'
17+
| 'enum'
18+
| 'array'
19+
| 'bool'
20+
| 'func'
21+
| 'number'
22+
| 'object'
23+
| 'string'
24+
| 'any'
25+
| 'element'
26+
| 'node'
27+
| 'symbol'
28+
| 'objectOf'
29+
| 'shape'
30+
| 'union',
31+
value?: any,
32+
raw?: string,
33+
computed?: boolean,
34+
// These are only needed for shape types.
35+
// Consider consolidating PropTypeDescriptor and PropDescriptor
36+
description?: string,
37+
required?: boolean,
38+
};
39+
40+
export type FlowBaseType = {
41+
required?: boolean,
42+
nullable?: boolean,
43+
alias?: string,
44+
};
45+
46+
export type FlowSimpleType = FlowBaseType & {|
47+
name: string,
48+
raw?: string,
49+
|};
50+
51+
export type FlowLiteralType = FlowBaseType & {
52+
name: 'literal',
53+
value: string,
54+
};
55+
56+
export type FlowElementsType = FlowBaseType & {
57+
name: string,
58+
raw: string,
59+
elements: Array<FlowTypeDescriptor>,
60+
};
61+
62+
export type FlowFunctionSignatureType = FlowBaseType & {
63+
name: 'signature',
64+
type: 'function',
65+
raw: string,
66+
signature: {
67+
arguments: Array<{ name: string, type: FlowTypeDescriptor }>,
68+
return: FlowTypeDescriptor,
69+
},
70+
};
71+
72+
export type FlowObjectSignatureType = FlowBaseType & {
73+
name: 'signature',
74+
type: 'object',
75+
raw: string,
76+
signature: {
77+
properties: Array<{
78+
key: string | FlowTypeDescriptor,
79+
value: FlowTypeDescriptor,
80+
}>,
81+
constructor?: FlowTypeDescriptor,
82+
},
83+
};
84+
85+
export type FlowTypeDescriptor =
86+
| FlowSimpleType
87+
| FlowLiteralType
88+
| FlowElementsType
89+
| FlowFunctionSignatureType
90+
| FlowObjectSignatureType;
91+
92+
export type PropDescriptor = {
93+
type?: PropTypeDescriptor,
94+
flowType?: FlowTypeDescriptor,
95+
required?: boolean,
96+
defaultValue?: any,
97+
description?: string,
98+
};
99+
100+
export type Handler = (documentation: Documentation, path: NodePath) => void;
101+
export type Resolver = (
102+
node: ASTNode,
103+
recast: Recast,
104+
) => ?NodePath | ?Array<NodePath>;

src/utils/__tests__/getFlowType-test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,44 @@ describe('getFlowType', () => {
399399
});
400400
});
401401

402+
it('handles qualified type identifiers', () => {
403+
const typePath = statement(`
404+
var x: MyType.x = {};
405+
406+
type MyType = { a: string, b: ?xyz };
407+
`)
408+
.get('declarations', 0)
409+
.get('id')
410+
.get('typeAnnotation')
411+
.get('typeAnnotation');
412+
413+
expect(getFlowType(typePath)).toEqual({
414+
name: 'MyType.x',
415+
});
416+
});
417+
418+
it('handles qualified type identifiers with params', () => {
419+
const typePath = statement(`
420+
var x: MyType.x<any> = {};
421+
422+
type MyType = { a: string, b: ?xyz };
423+
`)
424+
.get('declarations', 0)
425+
.get('id')
426+
.get('typeAnnotation')
427+
.get('typeAnnotation');
428+
429+
expect(getFlowType(typePath)).toEqual({
430+
name: 'MyType.x',
431+
raw: 'MyType.x<any>',
432+
elements: [
433+
{
434+
name: 'any',
435+
},
436+
],
437+
});
438+
});
439+
402440
describe('React types', () => {
403441
function test(type, expected) {
404442
const typePath = statement(`

0 commit comments

Comments
 (0)