Skip to content

Commit 740381c

Browse files
Josh Hallkara
Josh Hall
authored andcommitted
docs: Show directive selector in generated API docs (#4139)
Fixes #3983
1 parent 7c81698 commit 740381c

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

tools/dgeni/processors/categorizer.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/**
2+
* We want to avoid emitting selectors that are deprecated but don't have a way to mark
3+
* them as such in the source code. Thus, we maintain a separate blacklist of selectors
4+
* that should not be emitted in the documentation.
5+
*/
6+
const SELECTOR_BLACKLIST = new Set([
7+
'[portal]',
8+
'[portalHost]',
9+
'textarea[md-autosize]',
10+
'[overlay-origin]',
11+
'[connected-overlay]',
12+
]);
13+
114
/**
215
* Processor to add properties to docs objects.
316
*
@@ -29,11 +42,12 @@ module.exports = function categorizer() {
2942
classDoc.properties.forEach(doc => decoratePropertyDoc(doc));
3043

3144
decoratePublicDoc(classDoc);
32-
45+
3346
// Categorize the current visited classDoc into its Angular type.
3447
if (isDirective(classDoc)) {
3548
classDoc.isDirective = true;
3649
classDoc.directiveExportAs = getDirectiveExportAs(classDoc);
50+
classDoc.directiveSelectors = getDirectiveSelectors(classDoc);
3751
} else if (isService(classDoc)) {
3852
classDoc.isService = true;
3953
} else if (isNgModule(classDoc)) {
@@ -171,6 +185,18 @@ function getDirectiveOutputAlias(doc) {
171185
return isDirectiveOutput(doc) ? doc.decorators.find(d => d.name == 'Output').arguments[0] : '';
172186
}
173187

188+
function getDirectiveSelectors(classDoc) {
189+
let metadata = classDoc.decorators
190+
.find(d => d.name === 'Component' || d.name === 'Directive').arguments[0];
191+
192+
let selectorMatches = /selector\s*:\s*(?:"|')([^']*?)(?:"|')/g.exec(metadata);
193+
selectorMatches = selectorMatches && selectorMatches[1];
194+
195+
return selectorMatches ? selectorMatches.split(/\s*,\s*/)
196+
.filter(s => s !== '' && !s.includes('mat') && !SELECTOR_BLACKLIST.has(s))
197+
: selectorMatches;
198+
}
199+
174200
function getDirectiveExportAs(doc) {
175201
let metadata = doc.decorators
176202
.find(d => d.name === 'Component' || d.name === 'Directive').arguments[0];

tools/dgeni/templates/class.template.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@ <h4 class="docs-api-h4 docs-api-class-name">
33
</h4>
44
<p class="docs-api-class-description">{$ class.description $}</p>
55

6+
{%- if class.directiveSelectors -%}
7+
<div class="docs-api-directive-selectors">
8+
<span class="docs-api-class-selector-label">selector:</span>
9+
{% for s in class.directiveSelectors %}
10+
<span class="docs-api-class-selector-name">{$ s $}</span>
11+
{% endfor %}
12+
</div>
13+
{%- endif -%}
14+
615
{%- if class.directiveExportAs -%}
716
<span class="docs-api-h4 docs-api-class-export-label">Exported as:</span>
817
<span class="docs-api-class-export-name">{$ class.directiveExportAs $}</span>
918
{%- endif -%}
1019

1120
{%- if class.isDeprecated -%}
1221
<div class="docs-api-class-deprecated-marker">Deprecated</div>
13-
{%- endif -%}
22+
{%- endif -%}
1423

1524
{$ propertyList(class.properties) $}
1625

0 commit comments

Comments
 (0)