Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/rules/validate-jsdoc/check-throws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = checkThrows;
module.exports.tags = ['throws', 'exception'];
module.exports.scopes = ['function'];
module.exports.options = {
checkThrows: {allowedValues: [true]}
};

/**
* Checking throw types
*
* @param {(FunctionDeclaration|FunctionExpression)} node
* @param {DocTag} tag
* @param {Function} err
*/
function checkThrows(node, tag, err) {
// try to check returns types
if (!tag.type || !tag.type.valid) {
return;
}

var _throws = 0;
this._iterate(function(_node) {
if (_node.type === 'ThrowStatement') {
_throws += 1;
}
}, node);

var throwsStatements = this._getThrowStatementsForNode(node);
throwsStatements.forEach(function(argument) {
if (!tag.type.match(argument)) {
err('Wrong returns value', argument.loc.start);
}
});
}
4 changes: 3 additions & 1 deletion lib/rules/validate-jsdoc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ var validatorsByName = module.exports = {

checkRedundantAccess: require('./check-redundant-access'),
enforceExistence: require('./enforce-existence'),
leadingUnderscoreAccess: require('./leading-underscore-access')
leadingUnderscoreAccess: require('./leading-underscore-access'),

checkThrows: require('./check-throws'),
};

Object.defineProperty(validatorsByName, 'load', {
Expand Down
175 changes: 175 additions & 0 deletions test/lib/rules/validate-jsdoc/check-throws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
describe('lib/rules/validate-jsdoc/check-throws', function () {
var checker = global.checker({
plugins: ['./lib/index']
});

describe('not configured', function() {

it('should report with undefined', function() {
global.expect(function() {
checker.configure({checkThrows: undefined});
}).to.throws(/accepted value/i);
});

it('should report with an object', function() {
global.expect(function() {
checker.configure({checkThrows: {}});
}).to.throws(/accepted value/i);
});

});

describe('with true', function() {
checker.rules({checkThrows: true});

checker.cases([
/* jshint ignore:start */
{
it: 'should not report method without @throws',
code: function () {
/**
* empty jsdoc
*/
function fun () {}
}
}, {
it: 'should not report throwing error',
code: function () {
/**
* @throws {Error}
*/
function fun () {
throw Error('Yolo');
}
}
}, {
it: 'should not report throwing new error',
code: function () {
/**
* @throws {Error}
*/
function fun () {
throw new Error('Yolo');
}
}
}, {
it: 'should not report untyped declaration',
code: function () {
/**
* @throws Maybe
*/
function fun () {
throw 'For sure';
}
}

}, {
it: 'should report method with throw and without @throws',
errors: 1,
code: function () {
/**
* empty jsdoc
*/
function fun () {
throw 'Anything';
}
}

}, {
it: 'should report wrong throw type',
errors: 1,
code: function () {
/**
* @throws {SomethingElse}
*/
function fun () {
throw Error('Yolo');
}
}
}, {
it: 'should report wrong throw type for strings',
errors: 1,
code: function () {
/**
* @throws {SomethingElse}
*/
function fun () {
throw 'For sure';
}
}
}, {
it: 'should report several @throws per method',
errors: 1,
code: function () {
/**
* @throws {Errors}
* @throws {SomethingElse}
*/
function fun () {}
}

}, {
it: 'should not report correct throws',
code: function () {
/**
* @throws {SomeError|SomethingElse|AnotherOne}
*/
function fun (q) {
if (q === 1) {
throw SomeError();
} else if (q === 2) {
throw SomethingElse();
} else {
throw AnotherOne();
}
}
}
}, {
it: 'should report incorrect types for @throws',
errors: 2,
code: function () {
/**
* @throws {SomethingElse|AnotherOne}
*/
function fun (q) {
throw q ? SomeError() : 'untyped';
}
}
}
/* jshint ignore:end */
]);

});

describe.skip('with abstractShouldThrow', function() {
checker.rules({checkThrows: 'abstractShouldThrow'});

checker.cases([
/* jshint ignore:start */
{
it: 'should not report abstract method with throw',
code: function () {
/**
* @abstract
* @return {number}
*/
function fun () {
throw Error('Yolo');
}
}
}, {
it: 'should report abstract method without throw',
errors: 1,
code: function () {
/**
* @abstract
* @return {number}
*/
function fun () {}
}
}
/* jshint ignore:end */
]);

});
});