Updated documentation generator

... to handle Object.assign(Object.create(foo), { ... })
This commit is contained in:
Felix Kling 2015-03-24 16:10:28 -07:00
parent 2cad104fbc
commit 8106d6fd0a
1 changed files with 27 additions and 3 deletions

View File

@ -52,11 +52,35 @@ function findExportedObject(ast, recast) {
});
if (objPath) {
// 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}`
var b = recast.types.builders;
// This is a bit hacky, but easier than replicating the default propType
// handler. All this does is convert `{...}` to `{propTypes: {...}}`.
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)
);
}
objPath.replace(b.objectExpression([
b.property('init', b.literal('propTypes'), objPath.node)
b.property('init', b.literal('propTypes'), obj)
]));
}
return objPath;