Skip to content

Commit 0fdab7f

Browse files
committed
Add support for jsDoc filtering
1 parent b091983 commit 0fdab7f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

example/app.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,21 @@ var swaggerDefinition = {
3030
basePath: '/', // Base path (optional)
3131
};
3232

33+
// jsDocFilter has only one parameter - jsComment
34+
// jsComment contains the actual route jsDocumentation
35+
var jsDocFilter = function(jsComment) {
36+
// Do anything here below just to filter out comments
37+
// as long as the function returns boolean
38+
return typeof jsComment !== 'undefined';
39+
};
40+
3341
// Options for the swagger docs
3442
var options = {
3543
// Import swaggerDefinitions
3644
swaggerDefinition: swaggerDefinition,
3745
// Path to the API docs
3846
apis: ['./example/routes*.js', './example/parameters.yaml'],
47+
jsDocFilter: jsDocFilter,
3948
};
4049

4150
// Initialize swagger-jsdoc -> returns validated swagger spec in json format

lib/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ var swaggerHelpers = require('./swagger-helpers');
1414
* Parses the provided API file for JSDoc comments.
1515
* @function
1616
* @param {string} file - File to be parsed
17+
* @param {object} jsDocFilter - Function returning boolean to filter docs
1718
* @returns {{jsdoc: array, yaml: array}} JSDoc comments and Yaml files
1819
* @requires doctrine
1920
*/
20-
function parseApiFile(file) {
21+
function parseApiFile(file, jsDocFilter) {
2122
var jsDocRegex = /\/\*\*([\s\S]*?)\*\//gm;
2223
var fileContent = fs.readFileSync(file, { encoding: 'utf8' });
2324
var ext = path.extname(file);
@@ -31,7 +32,10 @@ function parseApiFile(file) {
3132
if (regexResults) {
3233
for (var i = 0; i < regexResults.length; i = i + 1) {
3334
var jsDocComment = doctrine.parse(regexResults[i], { unwrap: true });
34-
jsDocComments.push(jsDocComment);
35+
36+
if (typeof jsDocFilter !== 'function' || !!jsDocFilter(jsDocComment)) {
37+
jsDocComments.push(jsDocComment);
38+
}
3539
}
3640
}
3741
}
@@ -102,7 +106,7 @@ module.exports = function(options) {
102106

103107
// Parse the documentation in the APIs array.
104108
for (var i = 0; i < apiPaths.length; i = i + 1) {
105-
var files = parseApiFile(apiPaths[i]);
109+
var files = parseApiFile(apiPaths[i], options.jsDocFilter);
106110
var swaggerJsDocComments = filterJsDocComments(files.jsdoc);
107111

108112
var problems = swaggerHelpers.findDeprecated([files, swaggerJsDocComments]);

0 commit comments

Comments
 (0)