Skip to content

Commit a5bccf0

Browse files
committed
Add Polyglot mode, simplify smartGlob to only take extensions
1 parent 32afa7d commit a5bccf0

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

docs/POLYGLOT.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# About documentation.js, polyglot mode, and file extensions
2+
3+
Base assumptions:
4+
5+
* documentation.js subsists on a combination of _source comments_ and
6+
_smart inferences from source code_.
7+
* The default mode of documentation.js is parsing JavaScript, but it has another
8+
mode, called `--polyglot` mode, that doesn't include any inference at all
9+
and lets you document other kinds of source code.
10+
* The default settings for everything should work for most projects, but
11+
this is a guide for if you have a particular setup.
12+
13+
## File extensions
14+
15+
Let's talk about file extensions. We have two different flags for controlling
16+
file extensions: `requireExtension` and `parseExtension`.
17+
18+
* requireExtension adds additional filetypes to the node.js `require()` method.
19+
By default, you can call, for instance, `require('foo')`, and the require algorithm
20+
will look for `foo.js`, `foo` the module, and `foo.json`. Adding another
21+
extension in requireExtension lets it look for `foo.otherextension`.
22+
* parseExtension adds additional filetypes to the list of filetypes documentation.js
23+
thinks it can parse, and it also adds those additional filetypes to the default
24+
files it looks for when you specify a directory or glob as input.
25+
26+
## Polyglot
27+
28+
Polyglot mode switches documentation.js from running on babylon and [babel](https://babeljs.io/)
29+
as a JavaScript parser, to using [get-comments](https://github.com/tunnckocore/get-comments).
30+
This lets it grab comments formatted in the `/** Comment */` style from source
31+
code that _isn't_ JavaScript, like C++ or CSS code.
32+
33+
Since documentation.js doesn't _parse_ C++ and lots of other languages (parsing JavaScript is complicated enough!),
34+
it can't make any of its smart inferences about their source code: it just
35+
takes documentation comments and shows them as-is.
36+
37+
You _can_ use polyglot mode to turn off inference across the board, but I don't recommend
38+
it. See the 'too much inference' topic in [TROUBLESHOOTING.md](TROUBLESHOOTING.md)
39+
for detail about that.

lib/smart_glob.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,14 @@ function convertPathToPosix(filepath) {
2626
*
2727
* Also makes sure all path separators are POSIX style for `glob` compatibility.
2828
*
29-
* @param {Object} [options] An options object
30-
* @param {string[]} [options.extensions=['.js']] An array of accepted extensions
31-
* @param {string} [options.cwd=process.cwd()] The cwd to use to resolve relative pathnames
29+
* @param {string[]} [extensions=['.js']] An array of accepted extensions
3230
* @returns {Function} A function that takes a pathname and returns a glob that
3331
* matches all files with the provided extensions if
3432
* pathname is a directory.
3533
*/
36-
function processPath(options) {
37-
var cwd = (options && options.cwd) || process.cwd();
38-
var extensions = (options && options.requireExtension) || ['.js'];
34+
function processPath(extensions) {
35+
var cwd = process.cwd();
36+
extensions = extensions || ['.js'];
3937

4038
extensions = extensions.map(function (ext) {
4139
return ext.replace(/^\./, '');
@@ -71,11 +69,11 @@ function processPath(options) {
7169
/**
7270
* Resolves any directory patterns into glob-based patterns for easier handling.
7371
* @param {string[]} patterns File patterns (such as passed on the command line).
74-
* @param {Object} options An options object.
72+
* @param {Array<string>} extensions A list of file extensions
7573
* @returns {string[]} The equivalent glob patterns and filepath strings.
7674
*/
77-
function resolveFileGlobPatterns(patterns, options) {
78-
var processPathExtensions = processPath(options);
75+
function resolveFileGlobPatterns(patterns, extensions) {
76+
var processPathExtensions = processPath(extensions);
7977
return patterns.map(processPathExtensions);
8078
}
8179

@@ -128,9 +126,7 @@ function listFilesToProcess(globPatterns) {
128126

129127
function smartGlob(indexes, extensions) {
130128
return listFilesToProcess(
131-
resolveFileGlobPatterns(indexes, {
132-
extensions: extensions
133-
})
129+
resolveFileGlobPatterns(indexes, extensions)
134130
);
135131
}
136132

test/bin.js

+13
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ test('extension option', function (t) {
148148
});
149149
});
150150

151+
/*
152+
* This tests that parseExtension adds extensions to smartGlob's
153+
* look through directories.
154+
*/
155+
test('polyglot + parseExtension + smartGlob', function (t) {
156+
documentation(['build fixture/polyglot ' +
157+
'--polyglot --parseExtension=cpp'], function (err, data) {
158+
t.ifError(err);
159+
t.equal(data.length, 1, 'includes a file with an arbitrary extension');
160+
t.end();
161+
});
162+
});
163+
151164
test('extension option', function (t) {
152165
documentation(['build fixture/extension.jsx'], function (err, data) {
153166
t.ifError(err);

0 commit comments

Comments
 (0)