Skip to content

chore: filter alias exports in docs #8782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tools/dgeni/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Package} from 'dgeni';
import {DocsPrivateFilter} from './processors/docs-private-filter';
import {Categorizer} from './processors/categorizer';
import {FilterExportAliases} from './processors/filter-export-aliases';
import {ComponentGrouper} from './processors/component-grouper';
import {ReadTypeScriptModules} from 'dgeni-packages/typescript/processors/readTypeScriptModules';
import {TsParser} from 'dgeni-packages/typescript/services/TsParser';
Expand Down Expand Up @@ -47,6 +48,8 @@ const materialPackages = globSync(path.join(sourceDir, 'lib', '*/'))

export const apiDocsPackage = new Package('material2-api-docs', dgeniPackageDeps);

// Processor that filters out aliased exports that should not be shown in the docs.
apiDocsPackage.processor(new FilterExportAliases());

// Processor that filters out symbols that should not be shown in the docs.
apiDocsPackage.processor(new DocsPrivateFilter());
Expand Down
48 changes: 48 additions & 0 deletions tools/dgeni/processors/filter-export-aliases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {DocCollection, Processor} from 'dgeni';
import {ExportDoc} from 'dgeni-packages/typescript/api-doc-types/ExportDoc';
import {ModuleDoc} from 'dgeni-packages/typescript/api-doc-types/ModuleDoc';

/**
* Processor to filter out Dgeni documents that are export aliases. Keeping them means
* that a given document shows up multiple times in the docs.
*
* ```ts
* export {ObserveContent} from './X';
* export {ObserveContent as CdkObserveContent} from './X';
* ```
*
* This is a common pattern inside of Angular Material, but causes Dgeni to generate
* documents for both exports. The second document is identical to the original document and
* doesn't even show the aliased name.
*
* See: https://github.com/angular/dgeni-packages/issues/248
*/
export class FilterExportAliases implements Processor {
name = 'filter-export-aliases';
$runBefore = ['categorizer'];

$process(docs: DocCollection) {
return docs.filter(doc => this.filterAliasExport(doc));
}

private filterAliasExport(doc: ExportDoc) {
if (!doc.moduleDoc) {
return true;
}

const moduleDoc = doc.moduleDoc as ModuleDoc;
const duplicateDocs = moduleDoc.exports.filter(exportDoc => exportDoc.id === doc.id);

// Remove the current export document if there are multiple Dgeni export documents with the
// same document id. If there are multiple docs with the same id, we can assume that this doc
// is an alias export.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to more specifically target things marked as @deprecated? It's possible we may want to re-export something not as a deprecation, but as an alternate name (e.g. re-exporting something from cdk with "mat").

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Unfortunately it's not possible to detect if the re-export was marked as deprecated.

/** @deprecated */
export {XX as PP} from './test';

Dgeni doesn't provide access to the export statement/symbol. In theory it would be possible to walk through imports file by file using the AST, but this more turns into it's own docs generation tool then.

I assume even Dgeni in general (with angular/dgeni-packages#248) won't be able to differentiate between deprecated re-exports or normal re-exports.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we formatted it like this?

export {XX as /** @deprecated */ PP} from './test';

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tried that, but that information is being lost as well.

if (duplicateDocs && duplicateDocs.length > 1) {
moduleDoc.exports.splice(
moduleDoc.exports.findIndex(exportDoc => exportDoc.id === doc.id), 1);
return false;
}

return true;
}

}