2015-03-19 21:05:07 +00:00
|
|
|
"use strict";
|
|
|
|
var docgen = require('react-docgen');
|
|
|
|
|
|
|
|
function stylePropTypeHandler(documentation, path) {
|
2015-08-31 19:23:11 +00:00
|
|
|
var propTypesPath = docgen.utils.getMemberValuePath(path, 'propTypes');
|
2015-03-19 21:05:07 +00:00
|
|
|
if (!propTypesPath) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-29 10:04:44 +00:00
|
|
|
|
2015-03-19 21:05:07 +00:00
|
|
|
propTypesPath = docgen.utils.resolveToValue(propTypesPath);
|
|
|
|
if (!propTypesPath || propTypesPath.node.type !== 'ObjectExpression') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the there is a style prop
|
|
|
|
propTypesPath.get('properties').each(function(propertyPath) {
|
|
|
|
if (propertyPath.node.type !== 'Property' ||
|
|
|
|
docgen.utils.getPropertyName(propertyPath) !== 'style') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var valuePath = docgen.utils.resolveToValue(propertyPath.get('value'));
|
|
|
|
// If it's a call to StyleSheetPropType, do stuff
|
|
|
|
if (valuePath.node.type !== 'CallExpression' ||
|
|
|
|
valuePath.node.callee.name !== 'StyleSheetPropType') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Get type of style sheet
|
|
|
|
var styleSheetModule = docgen.utils.resolveToModule(
|
|
|
|
valuePath.get('arguments', 0)
|
|
|
|
);
|
|
|
|
if (styleSheetModule) {
|
|
|
|
var propDescriptor = documentation.getPropDescriptor('style');
|
|
|
|
propDescriptor.type = {name: 'stylesheet', value: styleSheetModule};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-29 10:04:44 +00:00
|
|
|
function deprecatedPropTypeHandler(documentation, path) {
|
|
|
|
var propTypesPath = docgen.utils.getMemberValuePath(path, 'propTypes');
|
|
|
|
if (!propTypesPath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
propTypesPath = docgen.utils.resolveToValue(propTypesPath);
|
|
|
|
if (!propTypesPath || propTypesPath.node.type !== 'ObjectExpression') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks for deprecatedPropType function and add deprecation info.
|
|
|
|
propTypesPath.get('properties').each(function(propertyPath) {
|
|
|
|
var valuePath = docgen.utils.resolveToValue(propertyPath.get('value'));
|
|
|
|
// If it's a call to deprecatedPropType, do stuff
|
|
|
|
if (valuePath.node.type !== 'CallExpression' ||
|
|
|
|
valuePath.node.callee.name !== 'deprecatedPropType') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var propDescriptor = documentation.getPropDescriptor(
|
|
|
|
docgen.utils.getPropertyName(propertyPath)
|
|
|
|
);
|
|
|
|
// The 2nd argument of deprecatedPropType is the deprecation message.
|
|
|
|
// Used printValue to get the string otherwise there was issues with template
|
|
|
|
// strings.
|
|
|
|
propDescriptor.deprecationMessage = docgen.utils
|
|
|
|
.printValue(valuePath.get('arguments', 1))
|
|
|
|
// Remove the quotes printValue adds.
|
|
|
|
.slice(1, -1);
|
|
|
|
|
|
|
|
// Get the actual prop type.
|
|
|
|
propDescriptor.type = docgen.utils.getPropType(
|
|
|
|
valuePath.get('arguments', 0)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-19 21:05:07 +00:00
|
|
|
function findExportedOrFirst(node, recast) {
|
2015-08-31 19:23:11 +00:00
|
|
|
return docgen.resolver.findExportedComponentDefinition(node, recast) ||
|
|
|
|
docgen.resolver.findAllComponentDefinitions(node, recast)[0];
|
2015-03-19 21:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function findExportedObject(ast, recast) {
|
|
|
|
var objPath;
|
|
|
|
recast.visit(ast, {
|
|
|
|
visitAssignmentExpression: function(path) {
|
|
|
|
if (!objPath && docgen.utils.isExportsOrModuleAssignment(path)) {
|
|
|
|
objPath = docgen.utils.resolveToValue(path.get('right'));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (objPath) {
|
2015-03-24 23:10:28 +00:00
|
|
|
// Hack: This is easier than replicating the default propType
|
|
|
|
// handler.
|
|
|
|
// This converts any expression, e.g. `foo` to an object expression of
|
|
|
|
// the form `{propTypes: foo}`
|
2015-03-19 21:05:07 +00:00
|
|
|
var b = recast.types.builders;
|
2015-03-24 23:10:28 +00:00
|
|
|
var nt = recast.types.namedTypes;
|
|
|
|
var obj = objPath.node;
|
|
|
|
|
|
|
|
// Hack: This is converting calls like
|
|
|
|
//
|
|
|
|
// Object.apply(Object.create(foo), { bar: 42 })
|
|
|
|
//
|
|
|
|
// to an AST representing an object literal:
|
|
|
|
//
|
|
|
|
// { ...foo, bar: 42 }
|
|
|
|
if (nt.CallExpression.check(obj) &&
|
|
|
|
recast.print(obj.callee).code === 'Object.assign') {
|
|
|
|
obj = objPath.node.arguments[1];
|
|
|
|
var firstArg = objPath.node.arguments[0];
|
|
|
|
if (recast.print(firstArg.callee).code === 'Object.create') {
|
|
|
|
firstArg = firstArg.arguments[0];
|
|
|
|
}
|
|
|
|
obj.properties.unshift(
|
|
|
|
b.spreadProperty(firstArg)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-19 21:05:07 +00:00
|
|
|
objPath.replace(b.objectExpression([
|
2015-03-24 23:10:28 +00:00
|
|
|
b.property('init', b.literal('propTypes'), obj)
|
2015-03-19 21:05:07 +00:00
|
|
|
]));
|
|
|
|
}
|
|
|
|
return objPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.stylePropTypeHandler = stylePropTypeHandler;
|
2016-01-29 10:04:44 +00:00
|
|
|
exports.deprecatedPropTypeHandler = deprecatedPropTypeHandler;
|
2015-03-19 21:05:07 +00:00
|
|
|
exports.findExportedOrFirst = findExportedOrFirst;
|
|
|
|
exports.findExportedObject = findExportedObject;
|