|
| 1 | +import {DocCollection, Processor} from 'dgeni'; |
| 2 | +import {ExportDoc} from 'dgeni-packages/typescript/api-doc-types/ExportDoc'; |
| 3 | +import {ModuleDoc} from 'dgeni-packages/typescript/api-doc-types/ModuleDoc'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Processor to filter out Dgeni documents that are export aliases. Keeping them means |
| 7 | + * that a given document shows up multiple times in the docs. |
| 8 | + * |
| 9 | + * ```ts |
| 10 | + * export {ObserveContent} from './X'; |
| 11 | + * export {ObserveContent as CdkObserveContent} from './X'; |
| 12 | + * ``` |
| 13 | + * |
| 14 | + * This is a common pattern inside of Angular Material, but causes Dgeni to generate |
| 15 | + * documents for both exports. The second document is identical to the original document and |
| 16 | + * doesn't even show the aliased name. |
| 17 | + * |
| 18 | + * See: https://github.com/angular/dgeni-packages/issues/248 |
| 19 | + */ |
| 20 | +export class FilterExportAliases implements Processor { |
| 21 | + name = 'filter-export-aliases'; |
| 22 | + $runBefore = ['categorizer']; |
| 23 | + |
| 24 | + $process(docs: DocCollection) { |
| 25 | + return docs.filter(doc => this.filterAliasExport(doc)); |
| 26 | + } |
| 27 | + |
| 28 | + private filterAliasExport(doc: ExportDoc) { |
| 29 | + if (!doc.moduleDoc) { |
| 30 | + return true; |
| 31 | + } |
| 32 | + |
| 33 | + const moduleDoc = doc.moduleDoc as ModuleDoc; |
| 34 | + const duplicateDocs = moduleDoc.exports.filter(exportDoc => exportDoc.id === doc.id); |
| 35 | + |
| 36 | + // Remove the current export document if there are multiple Dgeni export documents with the |
| 37 | + // same document id. If there are multiple docs with the same id, we can assume that this doc |
| 38 | + // is an alias export. |
| 39 | + if (duplicateDocs && duplicateDocs.length > 1) { |
| 40 | + moduleDoc.exports.splice( |
| 41 | + moduleDoc.exports.findIndex(exportDoc => exportDoc.id === doc.id), 1); |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | +} |
0 commit comments