Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3470ab5

Browse files
committed
chore(doc-gen): add directive names that aren't params to usage section
When a directive can be used as an attribute or CSS class, but doesn't take a value, its name is not included in the parameters, which previously meant that the directive name was missing from the Attribute / CSS Class usage section of the docs. This commit adds the name to the Usage section when it is missing from the parameters. Closes #14045 Closes #16265
1 parent 5d39a11 commit 3470ab5

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
describe('directives', function() {
4+
5+
describe('parameter section', function() {
6+
7+
it('should show the directive name only if it is a param (attribute) with a value', function() {
8+
browser.get('build/docs/index.html#!/api/ng/directive/ngInclude');
9+
expect(getParamNames().getText()).toContain('ngInclude | src');
10+
11+
browser.get('build/docs/index.html#!/api/ngRoute/directive/ngView');
12+
expect(getParamNames().getText()).not.toContain('ngView');
13+
});
14+
});
15+
16+
describe('usage section', function() {
17+
18+
it('should show the directive name if it is a param (attribute) with a value', function() {
19+
browser.get('build/docs/index.html#!/api/ng/directive/ngInclude');
20+
21+
expect(getUsageAs('element', 'ng-include').isPresent()).toBe(true);
22+
expect(getUsageAs('attribute', 'ng-include').isPresent()).toBe(true);
23+
expect(getUsageAs('CSS class', 'ng-include').isPresent()).toBe(true);
24+
});
25+
26+
it('should show the directive name if it is a void param (attribute)', function() {
27+
browser.get('build/docs/index.html#!/api/ngRoute/directive/ngView');
28+
29+
expect(getUsageAs('element', 'ng-view').isPresent()).toBe(true);
30+
expect(getUsageAs('attribute', 'ng-view').isPresent()).toBe(true);
31+
expect(getUsageAs('CSS class', 'ng-view').isPresent()).toBe(true);
32+
});
33+
});
34+
});
35+
36+
function getParamNames() {
37+
var argsSection = element(by.className('input-arguments'));
38+
39+
var paramNames = argsSection.all(by.css('tr td:nth-child(1)'));
40+
41+
return paramNames;
42+
}
43+
44+
// Based on the type of directive usage, the directive name will show up in the code block
45+
// with a specific class
46+
var typeClassMap = {
47+
element: 'tag',
48+
attribute: 'atn',
49+
'CSS class': 'atv'
50+
};
51+
52+
function getUsageAs(type, directiveName) {
53+
var usage = element(by.className('usage'));
54+
55+
var as = usage.element(by.cssContainingText('li', 'as ' + type));
56+
57+
return as.element(by.cssContainingText('span.' + typeClassMap[type], directiveName));
58+
}

docs/config/templates/ngdoc/api/directive.template.html

+17
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,23 @@ <h2 id="usage">Usage</h2>
2929
</li>
3030
{% endif -%}
3131

32+
33+
{% set hasNameAsParam = false %}
34+
35+
{# when a directive's name is not a parameter (i.e. doesn't take a value),
36+
add the directive name to the list of attributes and/or css classes #}
37+
38+
{%- for param in doc.params %}
39+
{% set hasNameAsParam = true if param.name === doc.name else hasNameAsParam %}
40+
{%- endfor %}
41+
3242
{%- if doc.restrict.attribute -%}
3343
<li>as attribute:
3444
{% code %}
3545
<{$ doc.element $}
46+
{%- if not hasNameAsParam %}
47+
{$ lib.directiveParam(doc.name, {}, '', '') $}
48+
{%- endif -%}
3649
{%- for param in doc.params %}
3750
{$ lib.directiveParam(param.name, param.type, '="', '"') $}
3851
{%- endfor %}>
@@ -43,10 +56,14 @@ <h2 id="usage">Usage</h2>
4356
{% endif -%}
4457

4558
{%- if doc.restrict.cssClass -%}
59+
4660
<li>as CSS class:
4761
{% code %}
4862
{% set sep = joiner(' ') %}
4963
<{$ doc.element $} class="
64+
{%- if not hasNameAsParam -%}
65+
{$ sep() $}{$ lib.directiveParam(doc.name, {}, '', '') $}
66+
{%- endif -%}
5067
{%- for param in doc.params -%}
5168
{$ sep() $}{$ lib.directiveParam(param.name, param.type, ': ', ';') $}
5269
{%- endfor %}"> ... </{$ doc.element $}>

0 commit comments

Comments
 (0)