angular-docs-cn/aio/tools/transforms/examples-package/processors/check-for-unused-example-regions.js
Pete Bacon Darwin fb97e67fcb build(docs-infra): fail if there are unused example regions (#40479)
We can define regions in our examples that can be referenced
and rendered in guides as code snippets. It is quite hard to ensure
that these regions are maintained correctly. One reason for this is
it is hard to know whether a region is being used or not.

This commit adds a new processor that checks for unused named
regions in examples and fails if any are found.

Fixes #19761

PR Close #40479
2021-01-20 16:12:15 -08:00

39 lines
1.3 KiB
JavaScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* A processor that will fail if there are named example regions that are not used in any docs.
*
* @param {*} exampleMap - contains all the regions extracted from example files.
*/
module.exports = function checkForUnusedExampleRegions(exampleMap) {
return {
$runAfter: ['renderExamples'],
$runBefore: ['writing-files'],
$process() {
const unusedExampleRegions = [];
for (const exampleFolder of Object.values(exampleMap)) {
for (const exampleFile of Object.values(exampleFolder)) {
for (const [regionName, region] of Object.entries(exampleFile.regions)) {
if (regionName === '' || region.usedInDoc) continue;
unusedExampleRegions.push(region);
}
}
}
if (unusedExampleRegions.length > 0) {
const message = (unusedExampleRegions.length === 1 ?
'There is 1 unused example region:\n' :
`There are ${unusedExampleRegions.length} unused example regions:\n`) +
unusedExampleRegions.map(region => ` - ${region.id}`).join('\n');
throw new Error(message);
}
},
};
};