-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
little refactoring of jsdoc validators
- Loading branch information
Alexej Yaroshevich
committed
Aug 20, 2014
1 parent
c0924b6
commit 4b2dfad
Showing
3 changed files
with
157 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
var jsDocHelpers = require('../../jsdoc-helpers'); | ||
|
||
module.exports = validateParamLine; | ||
module.exports.coveredOptions = [ | ||
'checkParamNames', | ||
'requireParamTypes', | ||
'checkRedundantParams', | ||
'checkTypes', | ||
]; | ||
|
||
/** | ||
* validator for @param | ||
* @param {{type: 'FunctionDeclaration'}|{type: 'FunctionExpression'}} node | ||
* @param {Number} line | ||
* @param {Function} err | ||
*/ | ||
function validateParamLine(node, line, err) { | ||
var options = this._options; | ||
if (line.indexOf('@param') !== 0) { | ||
return; | ||
} | ||
|
||
// checking validity | ||
var match = line.match(/^@param\s+(?:{(.+?)})?\s*(\[)?([a-zA-Z0-9_\.\$]+)/); | ||
if (!match) { | ||
return err('Invalid JsDoc @param'); | ||
} | ||
|
||
var jsDocType = match[1]; | ||
var jsDocName = match[3]; | ||
var jsDocOptional = match[2] === '['; | ||
|
||
// checking existance | ||
if (options.requireParamTypes && !jsDocType) { | ||
return err('Missing JsDoc @param type'); | ||
} | ||
|
||
var jsDocParsedType = jsDocHelpers.parse(jsDocType); | ||
if (options.checkTypes && jsDocParsedType.invalid) { | ||
return err('Invalid JsDoc type definition'); | ||
} | ||
|
||
// skip if there is dot in param name (object's inner param) | ||
if (jsDocName.indexOf('.') !== -1) { | ||
return; | ||
} | ||
|
||
// checking redudant | ||
var param = node.params[node.jsDoc.paramIndex]; | ||
if (options.checkRedundantParams && !jsDocOptional && !param) { | ||
return err('Redundant JsDoc @param'); | ||
} | ||
|
||
// checking name | ||
if (options.checkParamNames && jsDocName !== param.name) { | ||
return err('Invalid JsDoc @param argument name'); | ||
} | ||
|
||
node.jsDoc.paramIndex++; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
var jsDocHelpers = require('../../jsdoc-helpers'); | ||
var esprimaHelpers = require('../../esprima-helpers'); | ||
|
||
module.exports = validateReturnsLine; | ||
module.exports.coveredOptions = [ | ||
'checkReturnTypes', | ||
'requireReturnTypes', | ||
'checkRedundantReturns', | ||
'checkTypes', | ||
]; | ||
|
||
/** | ||
* validator for @return/@returns | ||
* @param {(FunctionDeclaration|FunctionExpression)} node | ||
* @param {Number} line | ||
* @param {Function} err | ||
*/ | ||
function validateReturnsLine(node, line, err) { | ||
var options = this._options; | ||
if (line.indexOf('@return') !== 0) { | ||
return; | ||
} | ||
|
||
// checking validity | ||
var match = line.match(/^@returns?\s+(?:{(.+?)})?/); | ||
if (!match) { | ||
return err('Invalid JsDoc @returns'); | ||
} | ||
|
||
var jsDocType = match[1]; | ||
|
||
// checking existance | ||
if (options.requireReturnTypes && !jsDocType) { | ||
err('Missing JsDoc @returns type'); | ||
} | ||
|
||
var jsDocParsedType = jsDocHelpers.parse(jsDocType); | ||
if (options.checkTypes && jsDocParsedType.invalid) { | ||
return err('Invalid JsDoc type definition'); | ||
} | ||
|
||
if (!options.checkRedundantReturns && !options.checkReturnTypes) { | ||
return; | ||
} | ||
|
||
var returnsArgumentStatements = []; | ||
esprimaHelpers.treeIterator.iterate(node, function(n/*, parentNode, parentCollection*/) { | ||
if (n && n.type === 'ReturnStatement' && n.argument) { | ||
if (node === esprimaHelpers.closestScopeNode(n)) { | ||
returnsArgumentStatements.push(n.argument); | ||
} | ||
} | ||
}); | ||
|
||
// checking redundant | ||
if (options.checkRedundantReturns && !returnsArgumentStatements.length) { | ||
err('Redundant JsDoc @returns'); | ||
} | ||
|
||
// try to check returns types | ||
if (options.checkReturnTypes && jsDocParsedType) { | ||
returnsArgumentStatements.forEach(function (argument) { | ||
if (!jsDocHelpers.match(jsDocParsedType, argument)) { | ||
err('Wrong returns value', argument.loc.start); | ||
} | ||
}); | ||
} | ||
} |