Skip to content

Commit cabef43

Browse files
committed
Access inheritance (fixes #78)
1 parent bac4c5b commit cabef43

File tree

5 files changed

+84
-31
lines changed

5 files changed

+84
-31
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ Parent.Child = function () {};
5858
Parent.Child.Grandchild = function () {};
5959
```
6060

61+
In addition, filtering by access is now applied to the entire hierarchy: if you
62+
mark a class as `@private`, neither it nor its children will be included in the
63+
output by default, regardless of the access specifiers of the children.
64+
6165
**mdast-based Markdown output**
6266

6367
We've switched from templating Markdown output with [Handlebars.js](http://handlebarsjs.com/)

index.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,27 @@ module.exports = function (indexes, options, callback) {
8282
return callback(error);
8383
}
8484
try {
85-
var flat = inputs
86-
.filter(filterJS)
87-
.reduce(function (memo, file) {
88-
return memo.concat(parseFn(file));
89-
}, [])
90-
.map(pipeline(
91-
lint,
92-
inferName(),
93-
inferKind(),
94-
inferParams(),
95-
inferReturn(),
96-
inferMembership(),
97-
nestParams,
98-
options.github ? github : noop
99-
))
100-
.filter(Boolean)
101-
.sort(sort.bind(undefined, options.order))
102-
.filter(filterAccess.bind(undefined, options.private ? [] : undefined))
103-
callback(null, options.hierarchy !== false ? hierarchy(flat) : flat);
85+
callback(null,
86+
filterAccess(
87+
options.private ? [] : undefined,
88+
hierarchy(
89+
inputs
90+
.filter(filterJS)
91+
.reduce(function (memo, file) {
92+
return memo.concat(parseFn(file));
93+
}, [])
94+
.map(pipeline(
95+
lint,
96+
inferName(),
97+
inferKind(),
98+
inferParams(),
99+
inferReturn(),
100+
inferMembership(),
101+
nestParams,
102+
options.github ? github : noop
103+
))
104+
.filter(Boolean)
105+
.sort(sort.bind(undefined, options.order)))));
104106
} catch (e) {
105107
callback(e);
106108
}

lib/filter_access.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
'use strict';
22

3+
var walk = require('./walk');
4+
35
/**
46
* Exclude given access levels from the generated documentation: this allows
57
* users to write documentation for non-public members by using the
68
* `@private` tag.
79
*
810
* @name access
911
* @public
10-
* @param {Array<string>} [levels=[private]] excluded access levels.
11-
* @param {Object} comment a parsed comment
12-
* @return {boolean} whether the comment should be output
12+
* @param {Array<string>} [levels=['private']] excluded access levels.
13+
* @param {Array<Object>} comments parsed comments (can be nested)
14+
* @return {Array<Object>} filtered comments
1315
*/
14-
module.exports = function (levels, comment) {
16+
module.exports = function (levels, comments) {
1517
levels = levels || ['private'];
16-
return levels.indexOf(comment.access) === -1;
18+
19+
function filter(comment) {
20+
return levels.indexOf(comment.access) === -1;
21+
}
22+
23+
function recurse(comment) {
24+
for (var scope in comment.members) {
25+
comment.members[scope] = comment.members[scope].filter(filter);
26+
}
27+
}
28+
29+
return walk(comments.filter(filter), recurse);
1730
};

lib/walk.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
*/
99
function walk(comments, fn) {
1010
comments.forEach(function (comment) {
11+
fn(comment);
1112
for (var scope in comment.members) {
1213
walk(comment.members[scope], fn);
1314
}
14-
fn(comment);
1515
});
1616
return comments;
1717
}

test/lib/filter_access.js

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,56 @@ var test = require('tap').test,
44
filterAccess = require('../../lib/filter_access');
55

66
test('filterAccess default', function (t) {
7-
t.equal(filterAccess(null, {
7+
t.deepEqual(filterAccess(null, [{
88
access: 'private'
9-
}), false);
9+
}]), []);
1010
t.end();
1111
});
1212

1313
test('filterAccess public', function (t) {
14-
t.equal(filterAccess(null, {
14+
t.deepEqual(filterAccess(null, [{
1515
access: 'public'
16-
}), true);
16+
}]), [{
17+
access: 'public'
18+
}]);
1719
t.end();
1820
});
1921

2022
test('filterAccess override', function (t) {
21-
t.equal(filterAccess([], {
23+
t.deepEqual(filterAccess([], [{
24+
access: 'private'
25+
}]), [{
2226
access: 'private'
23-
}), true);
27+
}]);
28+
t.end();
29+
});
30+
31+
test('filterAccess nesting', function (t) {
32+
t.deepEqual(filterAccess(null, [{
33+
access: 'public',
34+
members: {
35+
static: [{
36+
access: 'public'
37+
}, {
38+
access: 'private'
39+
}]
40+
}
41+
}, {
42+
access: 'private',
43+
members: {
44+
static: [{
45+
access: 'public'
46+
}, {
47+
access: 'private'
48+
}]
49+
}
50+
}]), [{
51+
access: 'public',
52+
members: {
53+
static: [{
54+
access: 'public'
55+
}]
56+
}
57+
}]);
2458
t.end();
2559
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy