Skip to content

Commit f0df112

Browse files
Marcin Szamotulskidanez
authored andcommitted
contextTypeHandler and childContextTypeHandler
Add two new handlers based on `handlers.propTypeHandler`.
1 parent 7fc1f68 commit f0df112

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

src/Documentation.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212

1313
class Documentation {
1414
_props: Object;
15+
_context: Object;
16+
_childContext: Object;
1517
_composes: Set<string>;
1618
_data: Object;
1719

1820
constructor() {
1921
this._props = new Map();
22+
this._context = new Map();
23+
this._childContext = new Map();
2024
this._composes = new Set();
2125
this._data = new Map();
2226
}
@@ -41,6 +45,22 @@ class Documentation {
4145
return propDescriptor;
4246
}
4347

48+
getContextDescriptor(propName: string): PropDescriptor {
49+
var propDescriptor = this._context.get(propName);
50+
if (!propDescriptor) {
51+
this._context.set(propName, propDescriptor = {});
52+
}
53+
return propDescriptor;
54+
}
55+
56+
getChildContextDescriptor(propName: string): PropDescriptor {
57+
var propDescriptor = this._childContext.get(propName);
58+
if (!propDescriptor) {
59+
this._childContext.set(propName, propDescriptor = {});
60+
}
61+
return propDescriptor;
62+
}
63+
4464
toObject(): Object {
4565
var obj = {};
4666

@@ -55,6 +75,20 @@ class Documentation {
5575
}
5676
}
5777

78+
if (this._context.size > 0) {
79+
obj.context = {};
80+
for (var [name, descriptor] of this._context) {
81+
obj.context[name] = descriptor;
82+
}
83+
}
84+
85+
if (this._childContext.size > 0) {
86+
obj.childContext = {};
87+
for (var [name, descriptor] of this._childContext) {
88+
obj.childContext[name] = descriptor;
89+
}
90+
}
91+
5892
if (this._composes.size > 0) {
5993
obj.composes = Array.from(this._composes);
6094
}

src/handlers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export {default as componentDocblockHandler} from './componentDocblockHandler';
1515
export {default as componentMethodsHandler} from './componentMethodsHandler';
1616
export {default as componentMethodsJsDocHandler} from './componentMethodsJsDocHandler';
1717
export {default as defaultPropsHandler} from './defaultPropsHandler';
18-
export {default as propTypeHandler} from './propTypeHandler';
18+
export {propTypeHandler, contextTypeHandler, childContextTypeHandler} from './propTypeHandler';
1919
export {default as propTypeCompositionHandler} from './propTypeCompositionHandler';
2020
export {default as propDocBlockHandler} from './propDocBlockHandler';
2121
export {default as displayNameHandler} from './displayNameHandler';

src/handlers/propTypeHandler.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ function isPropTypesExpression(path) {
3333
return false;
3434
}
3535

36-
function amendPropTypes(documentation, path) {
36+
function amendPropTypes(getDescriptor, path) {
3737
if (!types.ObjectExpression.check(path.node)) {
3838
return;
3939
}
4040

4141
path.get('properties').each(function(propertyPath) {
4242
switch (propertyPath.node.type) {
4343
case types.Property.name:
44-
var propDescriptor = documentation.getPropDescriptor(
44+
var propDescriptor = getDescriptor(
4545
getPropertyName(propertyPath)
4646
);
4747
var valuePath = propertyPath.get('value');
@@ -59,25 +59,42 @@ function amendPropTypes(documentation, path) {
5959
var resolvedValuePath = resolveToValue(propertyPath.get('argument'));
6060
switch (resolvedValuePath.node.type) {
6161
case types.ObjectExpression.name: // normal object literal
62-
amendPropTypes(documentation, resolvedValuePath);
62+
amendPropTypes(getDescriptor, resolvedValuePath);
6363
break;
6464
}
6565
break;
6666
}
6767
});
6868
}
6969

70-
export default function propTypeHandler(
71-
documentation: Documentation,
72-
path: NodePath
73-
) {
74-
var propTypesPath = getMemberValuePath(path, 'propTypes');
75-
if (!propTypesPath) {
76-
return;
77-
}
78-
propTypesPath = resolveToValue(propTypesPath);
79-
if (!propTypesPath) {
80-
return;
70+
export function getPropTypeHandler(propName: string) {
71+
return function (
72+
documentation: Documentation,
73+
path: NodePath
74+
) {
75+
var propTypesPath = getMemberValuePath(path, propName);
76+
if (!propTypesPath) {
77+
return;
78+
}
79+
propTypesPath = resolveToValue(propTypesPath);
80+
if (!propTypesPath) {
81+
return;
82+
}
83+
let getDescriptor;
84+
switch(propName) {
85+
case 'childContextTypes':
86+
getDescriptor = documentation.getChildContextDescriptor;
87+
break;
88+
case 'contextTypes':
89+
getDescriptor = documentation.getContextDescriptor;
90+
break;
91+
default:
92+
getDescriptor = documentation.getPropDescriptor;
93+
}
94+
amendPropTypes(getDescriptor.bind(documentation), propTypesPath);
8195
}
82-
amendPropTypes(documentation, propTypesPath);
8396
}
97+
98+
export const propTypeHandler = getPropTypeHandler('propTypes')
99+
export const contextTypeHandler = getPropTypeHandler('contextTypes')
100+
export const childContextTypeHandler = getPropTypeHandler('childContextTypes')

0 commit comments

Comments
 (0)