build(aio): refactor the decorator doc processing (#24000)

PR Close #24000
This commit is contained in:
Pete Bacon Darwin 2018-05-18 06:33:42 +01:00 committed by Miško Hevery
parent 62443b04a0
commit d4d8125b2d
1 changed files with 27 additions and 29 deletions

View File

@ -48,64 +48,62 @@ const {mergeProperties} = require('../../helpers/utils');
module.exports = function mergeDecoratorDocs(log) {
return {
$runAfter: ['processing-docs'],
$runBefore: ['docs-processed'],
$runBefore: ['docs-processed', 'checkContentRules'],
propertiesToMerge: [],
makeDecoratorCalls: [
{type: '', description: 'toplevel', functionName: 'makeDecorator'},
{type: 'Prop', description: 'property', functionName: 'makePropDecorator'},
{type: 'Param', description: 'parameter', functionName: 'makeParamDecorator'},
],
$process: function(docs) {
$process(docs) {
var makeDecoratorCalls = this.makeDecoratorCalls;
var propertiesToMerge = this.propertiesToMerge;
var docsToMerge = Object.create(null);
const decoratorDocs = Object.create(null);
docs.forEach(function(doc) {
// find all the decorators, signified by a call to `make...Decorator<Decorator>(metadata)`
docs.forEach(doc => {
const initializer = getInitializer(doc);
if (initializer) {
makeDecoratorCalls.forEach(function(call) {
// find all the decorators, signified by a call to `make...Decorator<Decorator>(metadata)`
this.makeDecoratorCalls.forEach(function(call) {
if (initializer.expression && initializer.expression.text === call.functionName) {
log.debug('mergeDecoratorDocs: found decorator', doc.docType, doc.name);
doc.docType = 'decorator';
doc.decoratorLocation = call.description;
// Get the type of the decorator metadata from the first "type" argument of the call.
// For example the `X` of `createDecorator<X>(...)`.
doc.decoratorType = initializer.arguments[0].text;
// clear the symbol type named since it is not needed
doc.symbolTypeName = undefined;
const decoratorType = initializer.arguments[0].text;
// keep track of the names of the metadata interface that will need to be merged into this decorator doc
docsToMerge[doc.name + 'Decorator'] = doc;
log.debug('mergeDecoratorDocs: found decorator', doc.docType, doc.name, decoratorType);
doc.docType = 'decorator';
doc.decoratorLocation = call.description;
doc.decoratorType = decoratorType;
decoratorDocs[doc.name + 'Decorator'] = doc;
}
});
}
});
// merge the metadata docs into the decorator docs
docs = docs.filter(function(doc) {
if (docsToMerge[doc.name]) {
// merge the info from the associated metadata interfaces into the decorator docs
docs = docs.filter(doc => {
if (decoratorDocs[doc.name]) {
// We have found an `XxxDecorator` document that will hold the call signature of the decorator
var decoratorDoc = docsToMerge[doc.name];
var callMember = doc.members.filter(function(member) { return member.isCallMember; })[0];
var decoratorDoc = decoratorDocs[doc.name];
var callMember = doc.members.find(member => member.isCallMember);
log.debug(
'mergeDecoratorDocs: merging', doc.name, 'into', decoratorDoc.name,
callMember.description.substring(0, 50));
// Merge the documentation found in this call signature into the original decorator
mergeProperties(decoratorDoc, callMember, propertiesToMerge);
mergeProperties(decoratorDoc, callMember, this.propertiesToMerge);
// remove doc from its module doc's exports
doc.moduleDoc.exports =
doc.moduleDoc.exports.filter(function(exportDoc) { return exportDoc !== doc; });
// remove from the overall list of docs to be rendered
return false;
doc.moduleDoc.exports = doc.moduleDoc.exports.filter(exportDoc => exportDoc !== doc);
}
return true;
return !decoratorDocs[doc.name];
});
return docs;
}
};
};