Skip to content

Commit

Permalink
Add support for jsDoc filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
identityclash committed Apr 25, 2018
1 parent b091983 commit 0fdab7f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 9 additions & 0 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@ var swaggerDefinition = {
basePath: '/', // Base path (optional)
};

// jsDocFilter has only one parameter - jsComment
// jsComment contains the actual route jsDocumentation
var jsDocFilter = function(jsComment) {
// Do anything here below just to filter out comments
// as long as the function returns boolean
return typeof jsComment !== 'undefined';
};

// Options for the swagger docs
var options = {
// Import swaggerDefinitions
swaggerDefinition: swaggerDefinition,
// Path to the API docs
apis: ['./example/routes*.js', './example/parameters.yaml'],
jsDocFilter: jsDocFilter,
};

// Initialize swagger-jsdoc -> returns validated swagger spec in json format
Expand Down
10 changes: 7 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ var swaggerHelpers = require('./swagger-helpers');
* Parses the provided API file for JSDoc comments.
* @function
* @param {string} file - File to be parsed
* @param {object} jsDocFilter - Function returning boolean to filter docs
* @returns {{jsdoc: array, yaml: array}} JSDoc comments and Yaml files
* @requires doctrine
*/
function parseApiFile(file) {
function parseApiFile(file, jsDocFilter) {
var jsDocRegex = /\/\*\*([\s\S]*?)\*\//gm;
var fileContent = fs.readFileSync(file, { encoding: 'utf8' });
var ext = path.extname(file);
Expand All @@ -31,7 +32,10 @@ function parseApiFile(file) {
if (regexResults) {
for (var i = 0; i < regexResults.length; i = i + 1) {
var jsDocComment = doctrine.parse(regexResults[i], { unwrap: true });
jsDocComments.push(jsDocComment);

if (typeof jsDocFilter !== 'function' || !!jsDocFilter(jsDocComment)) {
jsDocComments.push(jsDocComment);
}
}
}
}
Expand Down Expand Up @@ -102,7 +106,7 @@ module.exports = function(options) {

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

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

0 comments on commit 0fdab7f

Please sign in to comment.