Skip to content

Commit 21646a0

Browse files
Marcin Szamotulskidanez
authored andcommitted
contextTypeHandler and childContextTypeHandler
Add two new handlers based on `handlers.propTypeHandler`.
1 parent 6875e86 commit 21646a0

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

@@ -57,6 +77,20 @@ class Documentation {
5777
}
5878
}
5979

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

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');
@@ -60,25 +60,42 @@ function amendPropTypes(documentation, path) {
6060
var resolvedValuePath = resolveToValue(propertyPath.get('argument'));
6161
switch (resolvedValuePath.node.type) {
6262
case types.ObjectExpression.name: // normal object literal
63-
amendPropTypes(documentation, resolvedValuePath);
63+
amendPropTypes(getDescriptor, resolvedValuePath);
6464
break;
6565
}
6666
break;
6767
}
6868
});
6969
}
7070

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

0 commit comments

Comments
 (0)