Skip to content

Commit 629ea6f

Browse files
committed
Add support for inferring private based on the name
This adds a command line flag called `--infer-private` which is a string (defaults to `^_`) which is used as a regexp for inferring if a name is private or not. For example: ```js /** C */ class C { /** I'm public */ m() {} /** I'm private */ _p() {} } ``` Fixes #436
1 parent 7dad60b commit 629ea6f

10 files changed

+719
-19
lines changed

Diff for: docs/USAGE.md

+32-19
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,40 @@ Commands:
1919
readme inject documentation into your README.md
2020

2121
Options:
22-
--help Show help [boolean]
23-
--version Show version number [boolean]
24-
--shallow shallow mode turns off dependency resolution, only processing
25-
the specified files (or the main script specified in
26-
package.json) [boolean] [default: false]
27-
--config, -c configuration file. an array defining explicit sort order
28-
--external a string / glob match pattern that defines which external
29-
modules will be whitelisted and included in the generated
30-
documentation. [default: null]
31-
--extension, -e only input source files matching this extension will be
32-
parsed, this option can be used multiple times.
33-
--polyglot polyglot mode turns off dependency resolution and enables
34-
multi-language support. use this to document c++ [boolean]
35-
--private, -p generate documentation tagged as private
22+
--help Show help [boolean]
23+
--version Show version number [boolean]
24+
--shallow shallow mode turns off dependency resolution, only
25+
processing the specified files (or the main script
26+
specified in package.json) [boolean] [default: false]
27+
--config, -c configuration file. an array defining explicit sort order
28+
--external a string / glob match pattern that defines which external
29+
modules will be whitelisted and included in the generated
30+
documentation. [default: null]
31+
--extension, -e only input source files matching this extension will be
32+
parsed, this option can be used multiple times.
33+
--polyglot polyglot mode turns off dependency resolution and enables
34+
multi-language support. use this to document c++ [boolean]
35+
--private, -p generate documentation tagged as private
3636
[boolean] [default: false]
37-
--access, -a Include only comments with a given access level, out of
38-
private, protected, public, undefined. By default, public,
39-
protected, and undefined access levels are included
37+
--access, -a Include only comments with a given access level, out of
38+
private, protected, public, undefined. By default, public,
39+
protected, and undefined access levels are included
4040
[choices: "public", "private", "protected", "undefined"]
41-
--github, -g infer links to github in documentation [boolean]
41+
--github, -g infer links to github in documentation [boolean]
42+
--infer-private Infer private access based on the name. This is a regular
43+
expression that is used to match the name [string]
44+
--theme, -t specify a theme: this must be a valid theme module
45+
--name project name. by default, inferred from package.json
46+
--watch, -w watch input files and rebuild documentation when they
47+
change [boolean]
48+
--project-version project version. by default, inferred from package.json
49+
--output, -o output location. omit for stdout, otherwise is a filename
50+
for single-file outputs and a directory name for multi-file
51+
outputs like html [default: "stdout"]
52+
--format, -f [choices: "json", "md", "html"] [default: "json"]
4253

4354
Examples:
44-
documentation build foo.js parse documentation in a given file
55+
documentation build foo.js -f md > parse documentation in a file and
56+
API.md generate API documentation as
57+
Markdown
4558
```

Diff for: index.js

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var fs = require('fs'),
1818
inferProperties = require('./lib/infer/properties'),
1919
inferMembership = require('./lib/infer/membership'),
2020
inferReturn = require('./lib/infer/return'),
21+
inferAccess = require('./lib/infer/access'),
2122
formatLint = require('./lib/lint').formatLint,
2223
lintComments = require('./lib/lint').lintComments;
2324

@@ -150,6 +151,7 @@ function buildSync(indexes, options) {
150151
}, [])
151152
.map(pipeline(
152153
inferName(),
154+
inferAccess(options.inferPrivate),
153155
inferAugments(),
154156
inferKind(),
155157
inferParams(),
@@ -205,6 +207,7 @@ module.exports.lint = function lint(indexes, options, callback) {
205207
.map(pipeline(
206208
lintComments,
207209
inferName(),
210+
inferAccess(options.inferPrivate),
208211
inferAugments(),
209212
inferKind(),
210213
inferParams(),

Diff for: lib/commands/shared_options.js

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ function sharedInputOptions(parser) {
4848
type: 'boolean',
4949
describe: 'infer links to github in documentation',
5050
alias: 'g'
51+
})
52+
.option('infer-private', {
53+
type: 'string',
54+
describe: 'Infer private access based on the name. This is a regular expression that ' +
55+
'is used to match the name'
5156
});
5257
}
5358

Diff for: lib/infer/access.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
var shouldSkipInference = require('./should_skip_inference');
4+
5+
module.exports = function (pattern) {
6+
var re = pattern && new RegExp(pattern);
7+
8+
/**
9+
* Infers access (only private atm) from the name.
10+
*
11+
* @name inferAccess
12+
* @param {Object} comment parsed comment
13+
* @returns {Object} comment with access inferred
14+
*/
15+
return shouldSkipInference(function inferAccess(comment) {
16+
// This needs to run after inferName beacuse we infer the access based on
17+
// the name.
18+
if (re && comment.name && comment.access === undefined && re.test(comment.name)) {
19+
comment.access = 'private';
20+
}
21+
22+
return comment;
23+
});
24+
};

Diff for: test/bin.js

+17
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,23 @@ test('--private flag', function (t) {
228228
}, false);
229229
});
230230

231+
test('--infer-private flag', function (t) {
232+
documentation(['build fixture/infer-private.input.js --infer-private ^_'], {}, function (err, data) {
233+
t.error(err);
234+
235+
// This uses JSON.parse with a reviver used as a visitor.
236+
JSON.parse(data, function (n, v) {
237+
// Make sure we do not see any names that match `^_`.
238+
if (n === 'name') {
239+
t.equal(typeof v, 'string');
240+
t.ok(!/_$/.test(v));
241+
}
242+
return v;
243+
});
244+
t.end();
245+
}, false);
246+
});
247+
231248
test('write to file', function (t) {
232249

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

Diff for: test/fixture/infer-private.input.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* _p description
3+
*/
4+
function _p() {}
5+
6+
/** C description */
7+
class C {
8+
/** m description */
9+
m() {}
10+
/** _p description */
11+
_p() {}
12+
}

0 commit comments

Comments
 (0)