Skip to content

Commit e381c1a

Browse files
weekensAlexej Yaroshevich
authored andcommitted
New Rule: requireReturnDescription
Added requireReturnDescription rule implementation, tests, and documentation. Fixes #122 Closes gh-125
1 parent f3985b0 commit e381c1a

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,54 @@ function method(arg) {}
847847
```
848848

849849

850+
### requireReturnDescription
851+
852+
Ensures a return description exists.
853+
854+
Type: `Boolean`
855+
856+
Values: `true`
857+
858+
Context: `functions`
859+
860+
Tags: `return`, `returns`
861+
862+
#### Example
863+
864+
```js
865+
"requireReturnDescription": true
866+
```
867+
868+
##### Valid
869+
870+
```js
871+
/**
872+
* @returns {Boolean} Method result.
873+
*/
874+
function method() {
875+
return false;
876+
}
877+
878+
/**
879+
* @returns {String} method result
880+
*/
881+
function method() {
882+
return 'Hello!';
883+
}
884+
```
885+
886+
##### Invalid
887+
888+
```js
889+
/**
890+
* @returns {Boolean}
891+
*/
892+
function method() {
893+
return false;
894+
}
895+
```
896+
897+
850898
## Browser Usage
851899

852900
NOT SUPPORTED ATM. SORRY.

lib/rules/validate-jsdoc/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var validatorsByName = module.exports = {
1111

1212
checkReturnTypes: require('./check-return-types'),
1313
requireReturnTypes: require('./require-return-types'),
14+
requireReturnDescription: require('./require-return-description'),
1415
checkRedundantReturns: require('./check-redundant-returns'),
1516

1617
checkAnnotations: require('./check-annotations'),
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = validateReturnTagDescription;
2+
module.exports.tags = ['return', 'returns'];
3+
module.exports.scopes = ['function'];
4+
module.exports.options = {
5+
requireReturnDescription: {allowedValues: [true]}
6+
};
7+
8+
/**
9+
* Validator for @return description.
10+
*
11+
* @param {(FunctionDeclaration|FunctionExpression)} node
12+
* @param {DocTag} tag
13+
* @param {Function} err
14+
*/
15+
function validateReturnTagDescription(node, tag, err) {
16+
// Check tag existence. Note: for @returns tag, description is parsed as a name.
17+
if (tag.name) {
18+
return;
19+
}
20+
21+
return err('Missing return description', tag.loc);
22+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
describe('lib/rules/validate-jsdoc/require-return-description', function() {
2+
var checker = global.checker({
3+
plugins: ['./lib/index']
4+
});
5+
6+
describe('not configured', function() {
7+
it('should report with undefined', function() {
8+
global.expect(function() {
9+
checker.configure({requireReturnDescription: undefined});
10+
}).to.throws(/accepted value/i);
11+
});
12+
13+
it('should report with an object', function() {
14+
global.expect(function() {
15+
checker.configure({requireReturnDescription: {}});
16+
}).to.throws(/accepted value/i);
17+
});
18+
});
19+
20+
describe('with true', function() {
21+
checker.rules({requireReturnDescription: true});
22+
23+
checker.cases([
24+
/* jshint ignore:start */
25+
{
26+
it: 'should not report',
27+
code: function() {
28+
function yay(yey) {
29+
}
30+
}
31+
},
32+
{
33+
it: 'should report missing jsdoc-return description for function',
34+
errors: 1,
35+
code: function() {
36+
var x = 1;
37+
38+
/**
39+
* @returns
40+
*/
41+
function funcName() {
42+
// dummy
43+
}
44+
}
45+
},
46+
{
47+
it: 'should report missing jsdoc-return description for method',
48+
errors: 1,
49+
code: function() {
50+
Cls.prototype = {
51+
/**
52+
* @returns
53+
*/
54+
run: function() {
55+
// dummy
56+
}
57+
};
58+
}
59+
},
60+
{
61+
it: 'should not report valid jsdoc for function',
62+
code: function() {
63+
var x = 1;
64+
65+
/**
66+
* @returns {Boolean} False.
67+
*/
68+
function funcName() {
69+
return false;
70+
}
71+
}
72+
},
73+
{
74+
it: 'should not report valid jsdoc for method',
75+
code: function() {
76+
Cls.prototype = {
77+
/**
78+
* @returns {Boolean} False.
79+
*/
80+
run: function() {
81+
return false;
82+
}
83+
};
84+
}
85+
},
86+
{
87+
it: 'should report with right location',
88+
code: function() {
89+
Cls.prototype = {
90+
/**
91+
* @returns
92+
*/
93+
run: function() {
94+
return false;
95+
}
96+
};
97+
},
98+
errors: {column: 7, line: 3}
99+
}
100+
/* jshint ignore:end */
101+
]);
102+
103+
});
104+
});
105+

0 commit comments

Comments
 (0)