diff --git a/index.js b/index.js index 9dafccc21..532f88c3d 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,11 @@ 'use strict'; var sort = require('./lib/sort'), - concat = require('concat-stream'), nestParams = require('./lib/nest_params'), filterAccess = require('./lib/filter_access'), filterJS = require('./lib/filter_js'), - dependency = require('./streams/input/dependency'), - shallow = require('./streams/input/shallow'), + dependency = require('./lib/input/dependency'), + shallow = require('./lib/input/shallow'), parse = require('./lib/parsers/javascript'), polyglot = require('./lib/parsers/polyglot'), github = require('./lib/github'), @@ -47,11 +46,12 @@ module.exports = function (indexes, options, callback) { indexes = [indexes]; } - var inputStream = options.polyglot ? - shallow(indexes).pipe(polyglot()) : - (options.shallow ? shallow(indexes) : dependency(indexes, options)); + var inputFn = (options.polyglot || options.shallow) ? shallow : dependency; - return inputStream.pipe(concat(function (inputs) { + return inputFn(indexes, options, function (error, inputs) { + if (error) { + return callback(error); + } try { var flat = inputs .filter(filterJS) @@ -77,7 +77,7 @@ module.exports = function (indexes, options, callback) { } catch (e) { callback(e); } - })); + }); }; module.exports.formats = { diff --git a/streams/input/dependency.js b/lib/input/dependency.js similarity index 56% rename from streams/input/dependency.js rename to lib/input/dependency.js index 2254696e6..4ddae9133 100644 --- a/streams/input/dependency.js +++ b/lib/input/dependency.js @@ -1,7 +1,10 @@ 'use strict'; var mdeps = require('module-deps'), + fs = require('fs'), path = require('path'), + babelify = require('babelify'), + concat = require('concat-stream'), moduleFilters = require('../../lib/module_filters'); /** @@ -13,22 +16,33 @@ var mdeps = require('module-deps'), * * @param {Array} indexes paths to entry files as strings * @param {Object} options optional options passed - * @param {Array} [options.transform=[]] optional array of transforms - * @returns {ReadableStream} output + * @param {Function} callback called with (err, inputs) + * @returns {undefined} calls callback */ -function dependencyStream(indexes, options) { +function dependencyStream(indexes, options, callback) { var md = mdeps({ filter: function (id) { return !!options.external || moduleFilters.internalOnly(id); }, - transform: options.transform, + transform: [babelify.configure({ + sourceMap: false + })], postFilter: moduleFilters.externals(indexes, options) }); indexes.forEach(function (index) { md.write(path.resolve(index)); }); md.end(); - return md; + 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 + })); + })); } module.exports = dependencyStream; diff --git a/streams/input/shallow.js b/lib/input/shallow.js similarity index 75% rename from streams/input/shallow.js rename to lib/input/shallow.js index b55c6dbf2..446050c64 100644 --- a/streams/input/shallow.js +++ b/lib/input/shallow.js @@ -1,6 +1,5 @@ 'use strict'; -var streamify = require('stream-array'); var fs = require('fs'); /** @@ -16,10 +15,12 @@ var fs = require('fs'); * or without fs access. * * @param {Array} indexes entry points - * @return {ReadableStream} this emits data + * @param {Object} options parsing options + * @param {Function} callback called with (err, inputs) + * @return {undefined} calls callback */ -module.exports = function (indexes) { - return streamify(indexes.map(function (index) { +module.exports = function (indexes, options, callback) { + return callback(null, indexes.map(function (index) { if (typeof index === 'string') { return { source: fs.readFileSync(index, 'utf8'), diff --git a/lib/parsers/javascript.js b/lib/parsers/javascript.js index 66d0f9e6b..1ae03d10e 100644 --- a/lib/parsers/javascript.js +++ b/lib/parsers/javascript.js @@ -34,7 +34,6 @@ var parseOpts = { } }; - /** * Receives a module-dep item, * reads the file, parses the JavaScript, and parses the JSDoc. diff --git a/package.json b/package.json index ed215b274..b950281ab 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ }, "dependencies": { "ast-types": "^0.8.12", + "babelify": "^6.3.0", "babylon": "^5.8.23", "brfs": "^1.4.0", "concat-stream": "^1.5.0", diff --git a/test/fixture/bad/syntax.output.json b/test/fixture/bad/syntax.output.json index d9965a79f..5d6b9f535 100644 --- a/test/fixture/bad/syntax.output.json +++ b/test/fixture/bad/syntax.output.json @@ -3,5 +3,7 @@ "loc": { "line": 1, "column": 0 - } + }, + "_babel": true, + "codeFrame": "> 1 | *\n | ^\n 2 | " } \ No newline at end of file diff --git a/test/fixture/jsx.input.js b/test/fixture/jsx.input.js new file mode 100644 index 000000000..f8d352467 --- /dev/null +++ b/test/fixture/jsx.input.js @@ -0,0 +1,7 @@ +var util = require('util'); + +/** + * This function returns the number one. + * @returns {Number} numberone + */ +module.exports = (

hello

); diff --git a/test/fixture/jsx.output.custom.md b/test/fixture/jsx.output.custom.md new file mode 100644 index 000000000..5eb617f6e --- /dev/null +++ b/test/fixture/jsx.output.custom.md @@ -0,0 +1,9 @@ +# exports + +This function returns the number one. + + +Returns **Number** numberone + + + diff --git a/test/fixture/jsx.output.json b/test/fixture/jsx.output.json new file mode 100644 index 000000000..4f7c22f5e --- /dev/null +++ b/test/fixture/jsx.output.json @@ -0,0 +1,64 @@ +[ + { + "description": "This function returns the number one.", + "tags": [ + { + "title": "returns", + "description": "numberone", + "lineNumber": 2, + "type": { + "type": "NameExpression", + "name": "Number" + } + } + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 6, + "column": 3 + } + }, + "context": { + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 32 + } + }, + "code": "var util = require('util');\n\n/**\n * This function returns the number one.\n * @returns {Number} numberone\n */\nmodule.exports = (

hello

);\n" + }, + "errors": [ + "memberof reference to module not found" + ], + "returns": [ + { + "title": "returns", + "description": "numberone", + "lineNumber": 2, + "type": { + "type": "NameExpression", + "name": "Number" + } + } + ], + "name": "exports", + "memberof": "module", + "scope": "static", + "members": { + "instance": [], + "static": [] + }, + "events": [], + "path": [ + "exports" + ] + } +] \ No newline at end of file diff --git a/test/fixture/jsx.output.md b/test/fixture/jsx.output.md new file mode 100644 index 000000000..5eb617f6e --- /dev/null +++ b/test/fixture/jsx.output.md @@ -0,0 +1,9 @@ +# exports + +This function returns the number one. + + +Returns **Number** numberone + + + diff --git a/test/fixture/jsx.output.md.json b/test/fixture/jsx.output.md.json new file mode 100644 index 000000000..a563ea3d5 --- /dev/null +++ b/test/fixture/jsx.output.md.json @@ -0,0 +1,139 @@ +{ + "type": "root", + "children": [ + { + "type": "root", + "children": [ + { + "depth": 1, + "type": "heading", + "children": [ + { + "type": "text", + "value": "exports" + } + ] + }, + { + "type": "root", + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "This function returns the number one.", + "position": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 38 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 38 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 38 + } + } + }, + { + "type": "root", + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "Returns " + }, + { + "type": "strong", + "children": [ + { + "type": "text", + "value": "Number" + } + ] + }, + { + "type": "text", + "value": " " + }, + { + "type": "root", + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "numberone", + "position": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + } + } + } + ] + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/test/streams/input/shallow.js b/test/lib/input/shallow.js similarity index 66% rename from test/streams/input/shallow.js rename to test/lib/input/shallow.js index 81b0784e4..7a5f92482 100644 --- a/test/streams/input/shallow.js +++ b/test/lib/input/shallow.js @@ -1,28 +1,28 @@ 'use strict'; var test = require('tap').test, - concat = require('concat-stream'), path = require('path'), - shallow = require('../../../streams/input/shallow'); + shallow = require('../../../lib/input/shallow'); test('shallow deps', function (t) { - shallow([path.resolve(path.join(__dirname, '../../fixture/es6.input.js'))]) - .pipe(concat(function (deps) { - t.equal(deps.length, 1); - t.ok(deps[0].file, 'has file'); - t.end(); - })); + shallow([path.resolve(path.join(__dirname, '../../fixture/es6.input.js'))], {}, function (err, deps) { + t.ifError(err); + t.equal(deps.length, 1); + t.ok(deps[0].file, 'has file'); + t.end(); + }); }); test('shallow deps multi', function (t) { shallow([ path.resolve(path.join(__dirname, '../../fixture/es6.input.js')), path.resolve(path.join(__dirname, '../../fixture/es6.output.json')) - ]).pipe(concat(function (deps) { + ], {}, function (err, deps) { + t.ifError(err); t.equal(deps.length, 2); t.ok(deps[0].file, 'has file'); t.end(); - })); + }); }); test('shallow deps literal', function (t) { @@ -32,8 +32,9 @@ test('shallow deps literal', function (t) { }; shallow([ obj - ]).pipe(concat(function (deps) { + ], {}, function (err, deps) { + t.ifError(err); t.equal(deps[0], obj); t.end(); - })); + }); }); diff --git a/test/test.js b/test/test.js index a49f1d0db..9311bcb67 100644 --- a/test/test.js +++ b/test/test.js @@ -90,11 +90,13 @@ test('bad input', function (tt) { t.equal(res, undefined); // make error a serializable object error = JSON.parse(JSON.stringify(error)); + // remove system-specific path + delete error.filename; var outputfile = file.replace('.input.js', '.output.json'); if (UPDATE) { fs.writeFileSync(outputfile, JSON.stringify(error, null, 2)); } - var expect = require(outputfile); + var expect = JSON.parse(fs.readFileSync(outputfile)); t.deepEqual(error, expect); t.end(); });