From 8106d6fd0a7c37cc2cbc495120c5b8b883f6f3c7 Mon Sep 17 00:00:00 2001 From: Felix Kling Date: Tue, 24 Mar 2015 16:10:28 -0700 Subject: [PATCH] Updated documentation generator ... to handle Object.assign(Object.create(foo), { ... }) --- website/server/docgenHelpers.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/website/server/docgenHelpers.js b/website/server/docgenHelpers.js index 663727eed9e266..609a88eb0586d8 100644 --- a/website/server/docgenHelpers.js +++ b/website/server/docgenHelpers.js @@ -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;