Skip to content

Commit 8a6cac8

Browse files
committed
Add --access flag. Fixes #262
1 parent 370a7c5 commit 8a6cac8

File tree

7 files changed

+66
-18
lines changed

7 files changed

+66
-18
lines changed

bin/documentation.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ function parseArgs() {
5151
default: false,
5252
alias: 'p'
5353
})
54+
.option('access', {
55+
describe: 'Include only comments with a given access level, out of private, ' +
56+
'protected, public, undefined. By default, public, protected, and undefined access ' +
57+
'levels are included',
58+
choices: ['public', 'private', 'protected', 'undefined'],
59+
alias: 'a'
60+
})
5461
.option('github', {
5562
type: 'boolean',
5663
describe: 'infer links to github in documentation',
@@ -64,6 +71,14 @@ function parseArgs() {
6471
}
6572
options = extend(options, argv);
6673

74+
if (typeof options.access === 'string') {
75+
options.access = [options.access];
76+
}
77+
78+
if (options.private) {
79+
options.access = (options.access || ['public', 'undefined', 'protected']).concat(['private']);
80+
}
81+
6782
var command = argv._[0],
6883
inputs = argv._.slice(1);
6984

index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function expandInputs(indexes, options, callback) {
5757
* Generate JavaScript documentation as a list of parsed JSDoc
5858
* comments, given a root file as a path.
5959
*
60-
* @name documentation
60+
* @alias documentation
6161
* @param {Array<string>|string} indexes files to process
6262
* @param {Object} options options
6363
* @param {Array<string>} options.external a string regex / glob match pattern
@@ -70,6 +70,8 @@ function expandInputs(indexes, options, callback) {
7070
* even in JavaScript code. With the polyglot option set, this has no effect.
7171
* @param {Array<string|Object>} [options.order=[]] optional array that
7272
* defines sorting order of documentation
73+
* @param {Array<string>} [options.access=[]] an array of access levels
74+
* to output in documentation
7375
* @param {Object} [options.hljs] hljs optional options
7476
* @param {boolean} [options.hljs.highlightAuto=false] hljs automatically detect language
7577
* @param {Array} [options.hljs.languages] languages for hljs to choose from
@@ -85,6 +87,10 @@ module.exports = function (indexes, options, callback) {
8587
indexes = [indexes];
8688
}
8789

90+
if (!options.access) {
91+
options.access = ['public', 'undefined', 'protected'];
92+
}
93+
8894
var parseFn = (options.polyglot) ? polyglot : parseJavaScript;
8995

9096
return expandInputs(indexes, options, function (error, inputs) {
@@ -93,8 +99,7 @@ module.exports = function (indexes, options, callback) {
9399
}
94100
try {
95101
callback(null,
96-
filterAccess(
97-
options.private ? [] : undefined,
102+
filterAccess(options.access,
98103
hierarchy(
99104
inputs
100105
.filter(filterJS(options.extension))

lib/commands/build.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,3 @@ function addOutputArgs(yargs) {
117117
})
118118
.help('help');
119119
}
120-

lib/commands/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ module.exports = {
1414
'serve': require('./serve'),
1515
'lint': require('./lint')
1616
};
17-

lib/filter_access.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ var walk = require('./walk');
77
* users to write documentation for non-public members by using the
88
* `@private` tag.
99
*
10-
* @public
11-
* @param {Array<string>} [levels=['private']] excluded access levels.
10+
* @param {Array<string>} [levels=['public', 'undefined', 'protected']] included access levels.
1211
* @param {Array<Object>} comments parsed comments (can be nested)
1312
* @return {Array<Object>} filtered comments
1413
*/
1514
function filterAccess(levels, comments) {
16-
levels = levels || ['private'];
15+
levels = levels || ['public', 'undefined', 'protected'];
1716

1817
function filter(comment) {
19-
return levels.indexOf(comment.access) === -1;
18+
return levels.indexOf(String(comment.access)) !== -1;
2019
}
2120

2221
function recurse(comment) {

test/bin.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,22 @@ test('with an invalid command', function (t) {
189189
});
190190
}, options);
191191

192+
test('--access flag', function (t) {
193+
documentation(['build --shallow fixture/internal.input.js -a public'], {}, function (err, data) {
194+
t.error(err);
195+
t.equal(data, '[]');
196+
t.end();
197+
}, false);
198+
});
199+
200+
test('--private flag', function (t) {
201+
documentation(['build fixture/internal.input.js --private'], {}, function (err, data) {
202+
t.error(err);
203+
t.ok(data.length > 2, 'outputs docs');
204+
t.end();
205+
}, false);
206+
});
207+
192208
test('write to file', function (t) {
193209

194210
var dst = path.join(os.tmpdir(), (Date.now() + Math.random()).toString());

test/lib/filter_access.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,39 @@
33
var test = require('tap').test,
44
filterAccess = require('../../lib/filter_access');
55

6-
test('filterAccess default', function (t) {
7-
t.deepEqual(filterAccess(null, [{
6+
test('filterAccess public, protected, undefined, no private', function (t) {
7+
t.deepEqual(filterAccess(['public', 'protected', 'undefined'], [{
8+
access: 'public'
9+
}, {
10+
access: 'protected'
11+
}, {
12+
foo: 2
13+
}, {
814
access: 'private'
9-
}]), []);
15+
}]), [{
16+
access: 'public'
17+
}, {
18+
access: 'protected'
19+
}, {
20+
foo: 2
21+
}]);
1022
t.end();
1123
});
1224

13-
test('filterAccess public', function (t) {
14-
t.deepEqual(filterAccess(null, [{
15-
access: 'public'
16-
}]), [{
25+
test('filterAccess explicit public', function (t) {
26+
t.deepEqual(filterAccess(['public'], [
27+
{ access: 'public' },
28+
{ access: 'protected' },
29+
{ foo: 2 },
30+
{ access: 'private' }]),
31+
[{
1732
access: 'public'
1833
}]);
1934
t.end();
2035
});
2136

2237
test('filterAccess override', function (t) {
23-
t.deepEqual(filterAccess([], [{
38+
t.deepEqual(filterAccess(['private'], [{
2439
access: 'private'
2540
}]), [{
2641
access: 'private'
@@ -29,7 +44,7 @@ test('filterAccess override', function (t) {
2944
});
3045

3146
test('filterAccess nesting', function (t) {
32-
t.deepEqual(filterAccess(null, [{
47+
t.deepEqual(filterAccess(['public', 'protected', 'undefined'], [{
3348
access: 'public',
3449
members: {
3550
static: [{

0 commit comments

Comments
 (0)