Skip to content

Commit ca972d4

Browse files
author
Alexej Yaroshevich
committed
requireNewlineAfterDescription: rule to enforce newline after doc description
Fixes #85 Closes gh-99
1 parent d6068f4 commit ca972d4

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

lib/rules/validate-jsdoc/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ var validatorsByName = module.exports = {
1414

1515
checkAnnotations: require('./check-annotations'),
1616

17+
requireNewlineAfterDescription: require('./require-newline-after-description'),
18+
1719
checkRedundantAccess: require('./check-redundant-access'),
1820
enforceExistence: require('./enforce-existence'),
1921
leadingUnderscoreAccess: require('./leading-underscore-access')
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = requireNewlineAfterDescription;
2+
module.exports.scopes = ['function'];
3+
module.exports.options = {
4+
requireNewlineAfterDescription: {allowedValues: [true]}
5+
};
6+
7+
var RE_NEWLINE_AT_THE_END = /\n$/;
8+
var RE_NEWLINES = /\n/g;
9+
10+
/**
11+
* Requires newline after description in jsdoc comment
12+
*
13+
* @param {(FunctionDeclaration|FunctionExpression)} node
14+
* @param {Function} err
15+
*/
16+
function requireNewlineAfterDescription(node, err) {
17+
var doc = node.jsdoc;
18+
if (!doc || !doc.tags.length || !doc.description || !doc.description.length) {
19+
return;
20+
}
21+
22+
if (!RE_NEWLINE_AT_THE_END.test(doc.description)) {
23+
var loc = node.jsdoc.loc.start;
24+
var lines = doc.description.split(RE_NEWLINES);
25+
err('Newline required after description', {
26+
line: loc.line + lines.length,
27+
column: loc.column + 3 + lines[lines.length - 1].length
28+
});
29+
}
30+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
describe('lib/rules/validate-jsdoc/require-newline-after-description', function () {
2+
var checker = global.checker({
3+
additionalRules: ['lib/rules/validate-jsdoc.js']
4+
});
5+
6+
describe('not configured', function() {
7+
8+
it('should report with undefined', function() {
9+
global.expect(function() {
10+
checker.configure({requireNewlineAfterDescription: undefined});
11+
}).to.throws(/accepted value/i);
12+
});
13+
14+
it('should report with an object', function() {
15+
global.expect(function() {
16+
checker.configure({requireNewlineAfterDescription: {}});
17+
}).to.throws(/accepted value/i);
18+
});
19+
20+
});
21+
22+
describe('with true', function() {
23+
checker.rules({requireNewlineAfterDescription: true});
24+
25+
checker.cases([
26+
/* jshint ignore:start */
27+
{
28+
it: 'should not report common cases',
29+
code: function() {
30+
function fun(p) {
31+
}
32+
33+
/**
34+
* Description
35+
*/
36+
function fun(p) {
37+
}
38+
39+
/**
40+
* @param p
41+
*/
42+
function fun(p) {
43+
}
44+
}
45+
}, {
46+
it: 'should report newline absence after description',
47+
code: function () {
48+
/**
49+
* Some description
50+
* @param {number} p description without hyphen
51+
*/
52+
function fun(p) {
53+
}
54+
},
55+
errors: {
56+
line: 2,
57+
column: 19
58+
},
59+
}, {
60+
it: 'should not report newline after description',
61+
code: function () {
62+
/**
63+
* Some description.
64+
* Get me higher and higher!
65+
*
66+
* @param {number} p description without hyphen
67+
*/
68+
function fun(p) {}
69+
}
70+
}
71+
/* jshint ignore:end */
72+
]);
73+
74+
});
75+
76+
});

0 commit comments

Comments
 (0)