build(docs-infra): add NgModules references to provided injectables (#41960)

Parse the `providers` property of each NgModule doc and add this doc to
the `ngModules` property of each provided injectable.

PR Close #41960
This commit is contained in:
Pete Bacon Darwin
2021-05-06 22:07:22 +01:00
committed by Alex Rickabaugh
parent 85f5cb45d2
commit 71f37c9d9c
2 changed files with 41 additions and 0 deletions
@@ -158,6 +158,19 @@ module.exports = function processNgModuleDocs(getDocFromAlias, createDocMessage,
processNgModuleProviders(ngModuleDoc) {
// Add providers from the NgModule decorator.
const providers = ngModuleDoc.ngmoduleOptions.providers || [];
// Update injectables which are provided by this NgModule
for (const provider of providers) {
const injectable = parseProvider(provider);
const injectableDocs = getDocFromAlias(injectable, ngModuleDoc);
if (injectableDocs.length !== 1) {
continue;
}
const injectableDoc = injectableDocs[0];
injectableDoc.ngModules = injectableDoc.ngModules || [];
injectableDoc.ngModules.push(ngModuleDoc);
}
// And also add those associated via the `Injectable` `providedIn` property.
if (Array.isArray(ngModuleDoc.providers)) {
for (const provider of ngModuleDoc.providers) {
@@ -168,6 +181,7 @@ module.exports = function processNgModuleDocs(getDocFromAlias, createDocMessage,
if (providers.length > 0) {
ngModuleDoc.providers = providers;
}
}
};
@@ -191,6 +205,15 @@ module.exports = function processNgModuleDocs(getDocFromAlias, createDocMessage,
}
};
function parseProvider(provider) {
const match = /\{\s*provide:\s*(\w+)\s*,/.exec(provider);
if (match) {
return match[1];
} else {
return provider;
}
}
/**
* Compute the name of the array that will hold items of this type in the NgModule document.
*/