Skip to content

Commit

Permalink
Added support for "include" and "exclude" of source files in the proj…
Browse files Browse the repository at this point in the history
…ect configuration. Closes issue jsdoc#56.
  • Loading branch information
micmath committed May 31, 2012
1 parent 95bb744 commit 1f6f4a7
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 18 deletions.
12 changes: 7 additions & 5 deletions jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
43 changes: 43 additions & 0 deletions rhino_modules/jsdoc/src/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
@module jsdoc/src/filter
@author Michael Mathews <[email protected]>
@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;
}
12 changes: 2 additions & 10 deletions rhino_modules/jsdoc/src/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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($) {
Expand Down
19 changes: 19 additions & 0 deletions test/specs/jsdoc/src/filter.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
8 changes: 5 additions & 3 deletions test/specs/jsdoc/src/scanner.js
Original file line number Diff line number Diff line change
@@ -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, '');
Expand Down

0 comments on commit 1f6f4a7

Please sign in to comment.