Skip to content
This repository has been archived by the owner on Feb 18, 2019. It is now read-only.

Commit

Permalink
Updated documentation generator
Browse files Browse the repository at this point in the history
... to handle Object.assign(Object.create(foo), { ... })
  • Loading branch information
fkling committed Mar 24, 2015
1 parent 2cad104 commit 8106d6f
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions website/server/docgenHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8106d6f

Please sign in to comment.