Skip to content

Commit

Permalink
feat(bin): Support globs on windows and use smarter recursion (#629)
Browse files Browse the repository at this point in the history
This brings logic from eslint over to documentation: instead of readdirSync, we're using the glob
module. This also, I hope, will let us support globs on Windows without changing OSX/Linux behavior.
Fixes #607
  • Loading branch information
tmcw authored Dec 26, 2016
1 parent 7a66b3f commit cb8fdfa
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 122 deletions.
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,28 @@ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

--------------------------------------------------------------------------------

Contains sections of eslint

ESLint
Copyright JS Foundation and other contributors, https://js.foundation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
39 changes: 39 additions & 0 deletions docs/POLYGLOT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# About documentation.js, polyglot mode, and file extensions

Base assumptions:

* documentation.js subsists on a combination of _source comments_ and
_smart inferences from source code_.
* The default mode of documentation.js is parsing JavaScript, but it has another
mode, called `--polyglot` mode, that doesn't include any inference at all
and lets you document other kinds of source code.
* The default settings for everything should work for most projects, but
this is a guide for if you have a particular setup.

## File extensions

Let's talk about file extensions. We have two different flags for controlling
file extensions: `requireExtension` and `parseExtension`.

* requireExtension adds additional filetypes to the node.js `require()` method.
By default, you can call, for instance, `require('foo')`, and the require algorithm
will look for `foo.js`, `foo` the module, and `foo.json`. Adding another
extension in requireExtension lets it look for `foo.otherextension`.
* parseExtension adds additional filetypes to the list of filetypes documentation.js
thinks it can parse, and it also adds those additional filetypes to the default
files it looks for when you specify a directory or glob as input.

## Polyglot

Polyglot mode switches documentation.js from running on babylon and [babel](https://babeljs.io/)
as a JavaScript parser, to using [get-comments](https://github.com/tunnckocore/get-comments).
This lets it grab comments formatted in the `/** Comment */` style from source
code that _isn't_ JavaScript, like C++ or CSS code.

Since documentation.js doesn't _parse_ C++ and lots of other languages (parsing JavaScript is complicated enough!),
it can't make any of its smart inferences about their source code: it just
takes documentation comments and shows them as-is.

You _can_ use polyglot mode to turn off inference across the board, but I don't recommend
it. See the 'too much inference' topic in [TROUBLESHOOTING.md](TROUBLESHOOTING.md)
for detail about that.
12 changes: 4 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var fs = require('fs'),
sort = require('./lib/sort'),
nest = require('./lib/nest'),
filterAccess = require('./lib/filter_access'),
filterJS = require('./lib/filter_js'),
dependency = require('./lib/input/dependency'),
shallow = require('./lib/input/shallow'),
parseJavaScript = require('./lib/parsers/javascript'),
Expand All @@ -27,6 +26,8 @@ var fs = require('fs'),
markdownAST = require('./lib/output/markdown_ast'),
loadConfig = require('./lib/load_config');

var parseExtensions = ['js', 'jsx', 'es5', 'es6'];

/**
* Build a pipeline of comment handlers.
* @param {...Function|null} args - Pipeline elements. Each is a function that accepts
Expand Down Expand Up @@ -62,6 +63,8 @@ function expandInputs(indexes, options, callback) {
} else {
inputFn = dependency;
}
options.parseExtensions = parseExtensions
.concat(options.parseExtension || []);
inputFn(indexes, options, callback);
}

Expand Down Expand Up @@ -214,8 +217,6 @@ function buildSync(indexes, options) {
options.github && github,
garbageCollect);

var jsFilterer = filterJS(options.extension, options.polyglot);

return filterAccess(options.access,
hierarchy(
sort(
Expand All @@ -231,10 +232,6 @@ function buildSync(indexes, options) {
indexObject = index;
}

if (!jsFilterer(indexObject)) {
return [];
}

return parseFn(indexObject, options).map(buildPipeline);
})
.filter(Boolean), options)));
Expand Down Expand Up @@ -309,7 +306,6 @@ function lint(indexes, options, callback) {
callback(null,
formatLint(hierarchy(
inputs
.filter(filterJS(options.extension, options.polyglot))
.reduce(function (memo, file) {
return memo.concat(parseFn(file, options).map(lintPipeline));
}, [])
Expand Down
20 changes: 16 additions & 4 deletions lib/commands/shared_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,22 @@ module.exports.sharedInputOptions = {
'modules will be whitelisted and included in the generated documentation.',
default: null
},
'extension': {
describe: 'only input source files matching this extension will be parsed, ' +
'this option can be used multiple times.',
alias: 'e'
'requireExtension': {
describe: 'additional extensions to include in require() and import\'s search algorithm.' +
'For instance, adding .es5 would allow require("adder") to find "adder.es5"',
coerce: function (value) {
// Ensure that the value is an array
return [].concat(value);
},
alias: 're'
},
'parseExtension': {
describe: 'additional extensions to parse as source code.',
coerce: function (value) {
// Ensure that the value is an array
return [].concat(value);
},
alias: 'pe'
},
'polyglot': {
type: 'boolean',
Expand Down
34 changes: 0 additions & 34 deletions lib/filter_js.js

This file was deleted.

48 changes: 28 additions & 20 deletions lib/input/dependency.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use strict';

var mdeps = require('module-deps-sortable'),
fs = require('fs'),
path = require('path'),
babelify = require('babelify'),
filterJS = require('../filter_js'),
concat = require('concat-stream'),
moduleFilters = require('../../lib/module_filters'),
expandDirectories = require('./expand_directories');
var mdeps = require('module-deps-sortable');
var fs = require('fs');
var path = require('path');
var babelify = require('babelify');
var concat = require('concat-stream');
var moduleFilters = require('../../lib/module_filters');
var smartGlob = require('../smart_glob.js');

/**
* Returns a readable stream of dependencies, given an array of entry
Expand All @@ -22,8 +21,6 @@ var mdeps = require('module-deps-sortable'),
* @returns {undefined} calls callback
*/
function dependencyStream(indexes, options, callback) {
var filterer = filterJS(options.extension, options.polyglot);

var md = mdeps({
/**
* Determine whether a module should be included in documentation
Expand All @@ -33,11 +30,11 @@ function dependencyStream(indexes, options, callback) {
filter: function (id) {
return !!options.external || moduleFilters.internalOnly(id);
},
extensions: [].concat(options.extension || [])
.concat(['js', 'es6', 'jsx', 'json'])
extensions: [].concat(options.requireExtension || [])
.map(function (ext) {
return '.' + ext;
}),
return '.' + ext.replace(/^\./, '');
})
.concat(['.js', '.json', '.es6', '.jsx']),
transform: [babelify.configure({
sourceMap: false,
compact: false,
Expand All @@ -55,19 +52,30 @@ function dependencyStream(indexes, options, callback) {
})],
postFilter: moduleFilters.externals(indexes, options)
});
expandDirectories(indexes, filterer).forEach(function (index) {
smartGlob(indexes, options.parseExtensions).forEach(function (index) {
md.write(path.resolve(index));
});
md.end();
md.once('error', function (error) {
return callback(error);
});
md.pipe(concat(function (inputs) {
callback(null, inputs.map(function (input) {
// un-transform babelify transformed source
input.source = fs.readFileSync(input.file, 'utf8');
return input;
}));
callback(null, inputs
.filter(function (input) {
// At this point, we may have allowed a JSON file to be caught by
// module-deps, or anything else allowed by requireExtension.
// otherwise module-deps would complain about
// it not being found. But Babel can't parse JSON, so we filter non-JavaScript
// files away.
return options.parseExtensions.indexOf(
path.extname(input.file).replace(/^\./, '')
) > -1;
})
.map(function (input) {
// un-transform babelify transformed source
input.source = fs.readFileSync(input.file, 'utf8');
return input;
}));
}));
}

Expand Down
40 changes: 0 additions & 40 deletions lib/input/expand_directories.js

This file was deleted.

17 changes: 13 additions & 4 deletions lib/input/shallow.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var filterJS = require('../filter_js');
var expandDirectories = require('./expand_directories');
var smartGlob = require('../smart_glob.js');

/**
* A readable source for content that doesn't do dependency resolution, but
Expand All @@ -21,6 +20,16 @@ var expandDirectories = require('./expand_directories');
* @return {undefined} calls callback
*/
module.exports = function (indexes, options, callback) {
var filterer = filterJS(options.extension, options.polyglot);
return callback(null, expandDirectories(indexes, filterer));
var objects = [];
var strings = [];
indexes.forEach(function (index) {
if (typeof index === 'string') {
strings.push(index);
} else if (typeof index === 'object') {
objects.push(index);
} else {
throw new Error('Indexes should be either strings or objects');
}
});
return callback(null, objects.concat(smartGlob(strings, options.parseExtensions)));
};
Loading

0 comments on commit cb8fdfa

Please sign in to comment.