Added support for "include" and "exclude" of source files in the project configuration. Closes issue #56.

This commit is contained in:
Michael Mathews 2012-06-01 00:00:02 +01:00
parent 95bb744fee
commit 1f6f4a7ee7
5 changed files with 76 additions and 18 deletions

View File

@ -245,13 +245,15 @@ function main() {
env.opts._.splice(i--, 1);
}
}
if (env.opts.source.include) {
env.opts._ = (env.opts._ || []).concat(env.opts.source.include);
}
if (env.opts._.length > 0) { // are there any files to scan and parse?
var filter = new (require('jsdoc/src/filter').Filter)(env.conf.source);
var includeMatch = (env.conf.source && env.conf.source.includePattern)? new RegExp(env.conf.source.includePattern) : null,
excludeMatch = (env.conf.source && env.conf.source.excludePattern)? new RegExp(env.conf.source.excludePattern) : null;
sourceFiles = app.jsdoc.scanner.scan(env.opts._, (env.opts.recurse? 10 : undefined), includeMatch, excludeMatch);
sourceFiles = app.jsdoc.scanner.scan(env.opts._, (env.opts.recurse? 10 : undefined), filter);
require('jsdoc/src/handlers').attachTo(app.jsdoc.parser);

View File

@ -0,0 +1,43 @@
/**
@module jsdoc/src/filter
@author Michael Mathews <micmath@gmail.com>
@license Apache License 2.0 - See file 'LICENSE.md' in this project.
*/
/**
@constructor
@param {object} opts
@param {string[]} opts.exclude - Specific files to exclude.
@param {string|RegExp} opts.includePattern
@param {string|RegExp} opts.excludePattern
*/
exports.Filter = function(opts) {
this.exclude = opts.exclude || null;
this.includePattern = opts.includePattern?
typeof opts.includePattern === 'string'? new RegExp(opts.includePattern) : opts.includePattern
: null;
this.excludePattern = opts.excludePattern?
typeof opts.excludePattern === 'string'? new RegExp(opts.excludePattern) : opts.excludePattern
: null;
}
/**
@param {string} filepath - The filepath to check.
@returns {boolean} Should the given file be included?
*/
exports.Filter.prototype.isIncluded = function(filepath) {
if ( this.includePattern && !this.includePattern.test(filepath) ) {
return false;
}
if ( this.excludePattern && this.excludePattern.test(filepath) ) {
return false;
}
if ( this.exclude && this.exclude.indexOf(filepath) > -1 ) {
return false;
}
return true;
}

View File

@ -28,7 +28,7 @@ common.mixin(exports.Scanner.prototype, common.events);
@param {number} [depth=1]
@fires sourceFileFound
*/
exports.Scanner.prototype.scan = function(searchPaths, depth, includeMatch, excludeMatch) {
exports.Scanner.prototype.scan = function(searchPaths, depth, filter) {
var filePaths = [],
that = this;
@ -45,15 +45,7 @@ exports.Scanner.prototype.scan = function(searchPaths, depth, includeMatch, excl
});
filePaths = filePaths.filter(function($) {
if (includeMatch && !includeMatch.test($)) {
return false
}
if (excludeMatch && excludeMatch.test($)) {
return false
}
return true;
return filter.isIncluded($);
});
filePaths = filePaths.filter(function($) {

View File

@ -0,0 +1,19 @@
describe("jsdoc/src/filter", function() {
var filter = new (require('jsdoc/src/filter').Filter)({
includePattern: new RegExp(".+\\.js(doc)?$"),
excludePattern: new RegExp("(^|\\/)_"),
exclude: ['.ignore', 'scratch/conf.js']
});
var files = ['yes.js', '/yes.jsdoc', '/_nope.js', '.ignore'];
files = files.filter(function($) {
return filter.isIncluded($);
});
it("should return the correct source files", function() {
expect(files.length).toEqual(2);
expect(files.indexOf("yes.js")).toBeGreaterThan(-1);
expect(files.indexOf("/yes.jsdoc")).toBeGreaterThan(-1);
});
});

View File

@ -1,8 +1,10 @@
describe("jsdoc/src/scanner", function() {
var scanner = new (require('jsdoc/src/scanner').Scanner)(),
includeMatch = new RegExp(".+\\.js(doc)?$"),
excludeMatch = new RegExp("(^|\\/)_"),
sourceFiles = scanner.scan([__dirname+'/test/fixtures/src/'], 3, includeMatch, excludeMatch);
filter = new (require('jsdoc/src/filter').Filter)({
includePattern: new RegExp(".+\\.js(doc)?$"),
excludePattern: new RegExp("(^|\\/)_")
}),
sourceFiles = scanner.scan([__dirname+'/test/fixtures/src/'], 3, filter);
sourceFiles = sourceFiles.map(function($) {
return $.replace(__dirname, '');