Skip to content

Commit 32afa7d

Browse files
committed
Split extension logic into requireExtension and parseExtension
This changes logic quite a bit: no more filter_js and better thought-through settings that differentiate between require extensions, which are necessary to keep module-deps from crashing, and parse extensions, which add new filetypes to the range that we think are parseable. I don't think this is done yet, unfortunately, because smart_glob uses parse extensions as inputs, and that goes kind of against how polyglot/shallow mode is probably expected to work: right now you'd have to add --parseExtension cpp, for instance, for C++ parsing.
1 parent 0486223 commit 32afa7d

File tree

10 files changed

+47
-61
lines changed

10 files changed

+47
-61
lines changed

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ var fs = require('fs'),
2626
markdownAST = require('./lib/output/markdown_ast'),
2727
loadConfig = require('./lib/load_config');
2828

29+
var parseExtensions = ['js', 'jsx', 'es5', 'es6'];
30+
2931
/**
3032
* Build a pipeline of comment handlers.
3133
* @param {...Function|null} args - Pipeline elements. Each is a function that accepts
@@ -61,6 +63,8 @@ function expandInputs(indexes, options, callback) {
6163
} else {
6264
inputFn = dependency;
6365
}
66+
options.parseExtensions = parseExtensions
67+
.concat(options.parseExtension || []);
6468
inputFn(indexes, options, callback);
6569
}
6670

lib/commands/shared_options.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,22 @@ module.exports.sharedInputOptions = {
2424
'modules will be whitelisted and included in the generated documentation.',
2525
default: null
2626
},
27-
'extension': {
28-
describe: 'only input source files matching this extension will be parsed, ' +
29-
'this option can be used multiple times.',
30-
alias: 'e'
27+
'requireExtension': {
28+
describe: 'additional extensions to include in require() and import\'s search algorithm.' +
29+
'For instance, adding .es5 would allow require("adder") to find "adder.es5"',
30+
coerce: function (value) {
31+
// Ensure that the value is an array
32+
return [].concat(value);
33+
},
34+
alias: 're'
35+
},
36+
'parseExtension': {
37+
describe: 'additional extensions to parse as source code.',
38+
coerce: function (value) {
39+
// Ensure that the value is an array
40+
return [].concat(value);
41+
},
42+
alias: 'pe'
3143
},
3244
'polyglot': {
3345
type: 'boolean',

lib/filter_js.js

-34
This file was deleted.

lib/input/dependency.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var mdeps = require('module-deps-sortable');
44
var fs = require('fs');
55
var path = require('path');
66
var babelify = require('babelify');
7-
var filterJS = require('../filter_js');
87
var concat = require('concat-stream');
98
var moduleFilters = require('../../lib/module_filters');
109
var smartGlob = require('../smart_glob.js');
@@ -22,7 +21,6 @@ var smartGlob = require('../smart_glob.js');
2221
* @returns {undefined} calls callback
2322
*/
2423
function dependencyStream(indexes, options, callback) {
25-
var filterer = filterJS(options.extension, options.polyglot);
2624
var md = mdeps({
2725
/**
2826
* Determine whether a module should be included in documentation
@@ -32,14 +30,11 @@ function dependencyStream(indexes, options, callback) {
3230
filter: function (id) {
3331
return !!options.external || moduleFilters.internalOnly(id);
3432
},
35-
extensions: [].concat(options.extension || [])
36-
// We don't document JSON files, but we do include them in this list,
37-
// because browserify & node both support unnamed requires to .json
38-
// files: `require('foo')` can require `foo.json` automatically.
39-
.concat(['js', 'es6', 'jsx', 'json'])
33+
extensions: [].concat(options.requireExtension || [])
4034
.map(function (ext) {
41-
return '.' + ext;
42-
}),
35+
return '.' + ext.replace(/^\./, '');
36+
})
37+
.concat(['.js', '.json', '.es6', '.jsx']),
4338
transform: [babelify.configure({
4439
sourceMap: false,
4540
compact: false,
@@ -57,7 +52,7 @@ function dependencyStream(indexes, options, callback) {
5752
})],
5853
postFilter: moduleFilters.externals(indexes, options)
5954
});
60-
smartGlob(indexes, options.extension).forEach(function (index) {
55+
smartGlob(indexes, options.parseExtensions).forEach(function (index) {
6156
md.write(path.resolve(index));
6257
});
6358
md.end();
@@ -66,7 +61,16 @@ function dependencyStream(indexes, options, callback) {
6661
});
6762
md.pipe(concat(function (inputs) {
6863
callback(null, inputs
69-
.filter(filterer)
64+
.filter(function (input) {
65+
// At this point, we may have allowed a JSON file to be caught by
66+
// module-deps, or anything else allowed by requireExtension.
67+
// otherwise module-deps would complain about
68+
// it not being found. But Babel can't parse JSON, so we filter non-JavaScript
69+
// files away.
70+
return options.parseExtensions.indexOf(
71+
path.extname(input.file).replace(/^\./, '')
72+
) > -1;
73+
})
7074
.map(function (input) {
7175
// un-transform babelify transformed source
7276
input.source = fs.readFileSync(input.file, 'utf8');

lib/input/shallow.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ module.exports = function (indexes, options, callback) {
2828
} else if (typeof index === 'object') {
2929
objects.push(index);
3030
} else {
31-
throw new Error('indexes should be either strings or objects');
31+
throw new Error('Indexes should be either strings or objects');
3232
}
3333
});
34-
return callback(null, objects.concat(smartGlob(strings, options.extension)));
34+
return callback(null, objects.concat(smartGlob(strings, options.parseExtensions)));
3535
};

lib/smart_glob.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function convertPathToPosix(filepath) {
3535
*/
3636
function processPath(options) {
3737
var cwd = (options && options.cwd) || process.cwd();
38-
var extensions = (options && options.extensions) || ['.js'];
38+
var extensions = (options && options.requireExtension) || ['.js'];
3939

4040
extensions = extensions.map(function (ext) {
4141
return ext.replace(/^\./, '');

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"standard-changelog": "0.0.1",
6363
"cz-conventional-changelog": "1.2.0",
6464
"documentation-schema": "0.0.1",
65-
"eslint": "^3.1.0",
65+
"eslint": "^3.12.2",
6666
"fs-extra": "^1.0.0",
6767
"json-schema": "0.2.3",
6868
"mock-fs": "^3.5.0",

test/bin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ test('external modules option', function (t) {
141141

142142
test('extension option', function (t) {
143143
documentation(['build fixture/extension/index.otherextension ' +
144-
'--extension=otherextension'], function (err, data) {
144+
'--requireExtension=otherextension --parseExtension=otherextension'], function (err, data) {
145145
t.ifError(err);
146146
t.equal(data.length, 1, 'includes a file with an arbitrary extension');
147147
t.end();

test/fixture/require-json.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{}
1+
{"foo":"bar"}

test/lib/parsers/polyglot.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ test('polyglot', function (t) {
2727
description: remark().parse('color'),
2828
type: { name: 'number', type: 'NameExpression' } } ],
2929
tags: [ { description: null, lineNumber: 2, name: 'hexToUInt32Color', title: 'name' },
30-
{ description: null, lineNumber: 3, name: 'hex', title: 'param', type: {
31-
name: 'string', type: 'NameExpression'
32-
} },
33-
{ description: 'color', lineNumber: 4, title: 'returns', type: {
34-
name: 'number', type: 'NameExpression'
35-
} } ] } ], 'polyglot parser');
30+
{ description: null, lineNumber: 3, name: 'hex', title: 'param', type: {
31+
name: 'string', type: 'NameExpression'
32+
} },
33+
{ description: 'color', lineNumber: 4, title: 'returns', type: {
34+
name: 'number', type: 'NameExpression'
35+
} } ] } ], 'polyglot parser');
3636
t.end();
3737
});

0 commit comments

Comments
 (0)