Skip to content

Commit

Permalink
Access inheritance (fixes #78)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Oct 16, 2015
1 parent bac4c5b commit 862546d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 31 deletions.
40 changes: 21 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,27 @@ module.exports = function (indexes, options, callback) {
return callback(error);
}
try {
var flat = inputs
.filter(filterJS)
.reduce(function (memo, file) {
return memo.concat(parseFn(file));
}, [])
.map(pipeline(
lint,
inferName(),
inferKind(),
inferParams(),
inferReturn(),
inferMembership(),
nestParams,
options.github ? github : noop
))
.filter(Boolean)
.sort(sort.bind(undefined, options.order))
.filter(filterAccess.bind(undefined, options.private ? [] : undefined))
callback(null, options.hierarchy !== false ? hierarchy(flat) : flat);
callback(null,
filterAccess(
options.private ? [] : undefined,
hierarchy(
inputs
.filter(filterJS)
.reduce(function (memo, file) {
return memo.concat(parseFn(file));
}, [])
.map(pipeline(
lint,
inferName(),
inferKind(),
inferParams(),
inferReturn(),
inferMembership(),
nestParams,
options.github ? github : noop
))
.filter(Boolean)
.sort(sort.bind(undefined, options.order)))));
} catch (e) {
callback(e);
}
Expand Down
23 changes: 18 additions & 5 deletions lib/filter_access.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
'use strict';

var walk = require('./walk');

/**
* Exclude given access levels from the generated documentation: this allows
* users to write documentation for non-public members by using the
* `@private` tag.
*
* @name access
* @public
* @param {Array<string>} [levels=[private]] excluded access levels.
* @param {Object} comment a parsed comment
* @return {boolean} whether the comment should be output
* @param {Array<string>} [levels=['private']] excluded access levels.
* @param {Array<Object>} comments parsed comments (can be nested)
* @return {Array<Object>} filtered comments
*/
module.exports = function (levels, comment) {
module.exports = function (levels, comments) {
levels = levels || ['private'];
return levels.indexOf(comment.access) === -1;

function filter(comment) {
return levels.indexOf(comment.access) === -1;
}

function recurse(comment) {
for (var scope in comment.members) {
comment.members[scope] = comment.members[scope].filter(filter);
}
}

return walk(comments.filter(filter), recurse);
};
2 changes: 1 addition & 1 deletion lib/walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
*/
function walk(comments, fn) {
comments.forEach(function (comment) {
fn(comment);
for (var scope in comment.members) {
walk(comment.members[scope], fn);
}
fn(comment);
});
return comments;
}
Expand Down
46 changes: 40 additions & 6 deletions test/lib/filter_access.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,56 @@ var test = require('tap').test,
filterAccess = require('../../lib/filter_access');

test('filterAccess default', function (t) {
t.equal(filterAccess(null, {
t.deepEqual(filterAccess(null, [{
access: 'private'
}), false);
}]), []);
t.end();
});

test('filterAccess public', function (t) {
t.equal(filterAccess(null, {
t.deepEqual(filterAccess(null, [{
access: 'public'
}), true);
}]), [{
access: 'public'
}]);
t.end();
});

test('filterAccess override', function (t) {
t.equal(filterAccess([], {
t.deepEqual(filterAccess([], [{
access: 'private'
}]), [{
access: 'private'
}), true);
}]);
t.end();
});

test('filterAccess nesting', function (t) {
t.deepEqual(filterAccess(null, [{
access: 'public',
members: {
static: [{
access: 'public'
}, {
access: 'private'
}]
}
}, {
access: 'private',
members: {
static: [{
access: 'public'
}, {
access: 'private'
}]
}
}]), [{
access: 'public',
members: {
static: [{
access: 'public'
}]
}
}]);
t.end();
});

0 comments on commit 862546d

Please sign in to comment.