Skip to content

Commit 4548ecf

Browse files
lvivierAlexej Yaroshevich
authored andcommitted
Misc: find docblock before a blank line
- docblock must be indented same as fn - add test for blank line
1 parent aa40ed5 commit 4548ecf

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

lib/rules/validate-jsdoc.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ function patchNodesInFile(file) {
213213
}
214214

215215
// jsdoc property for nodes
216-
var fileComments = file.getComments();
217216
file.iterateNodesByType([
218217
'FunctionDeclaration', 'FunctionExpression'
219218
], function(node) {
@@ -230,27 +229,38 @@ function patchNodesInFile(file) {
230229
*/
231230
function getJsdoc() {
232231
if (!this.hasOwnProperty('_jsdoc')) {
233-
var res = findDocCommentBeforeLine(this.loc.start.line);
232+
var node = this.type === 'FunctionExpression' ? findFirstNodeInLine(this) : this;
233+
var res = findDocCommentBeforeNode(node);
234234
this._jsdoc = res ? jsdoc.createDocCommentByCommentNode(res) : null;
235235
}
236236
return this._jsdoc;
237237
}
238238

239+
/**
240+
* Finds the first node on the same line as passed node
241+
*
242+
* @param {?module:esprima/Node} node
243+
* @returns {?module:esprima/Node}
244+
*/
245+
function findFirstNodeInLine(node) {
246+
var parent = node.parentNode;
247+
if (!parent || parent.loc.start.line !== node.loc.start.line) {
248+
return node;
249+
}
250+
return findFirstNodeInLine(parent);
251+
}
252+
239253
/**
240254
* Finds DocComment in file before passed line number
241255
*
242-
* @param {number} line
256+
* @param {?module:esprima/Node} node
243257
* @returns {?module:esprima/Node}
244258
*/
245-
function findDocCommentBeforeLine(line) {
246-
line--; // todo: buggy behaviour, can't jump back over empty lines
247-
for (var i = 0, l = fileComments.length; i < l; i++) {
248-
var commentNode = fileComments[i];
249-
// v.substr(jsdoc.loc.start.column);
250-
if (commentNode.loc.end.line === line && commentNode.type === 'Block' &&
251-
commentNode.value.charAt(0) === '*') {
252-
return commentNode;
253-
}
259+
function findDocCommentBeforeNode(node) {
260+
var res = file.getPrevToken(file.getFirstNodeToken(node), {includeComments: true});
261+
if (res && res.type === 'Block' && res.value.charAt(0) === '*' &&
262+
res.loc.start.column === node.loc.start.column) {
263+
return res;
254264
}
255265
return null;
256266
}

test/lib/rules/validate-jsdoc.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ describe('lib/rules/validate-jsdoc', function () {
5252
// doesn't mean
5353
}
5454
}
55+
}, {
56+
it: 'should find docblock after a blank line',
57+
rules: {enforceExistence: true},
58+
code: function() {
59+
/**
60+
* Foo
61+
*/
62+
63+
function foo () {}
64+
}
65+
}, {
66+
it: 'should not stick docblock with diff indent',
67+
rules: {enforceExistence: true},
68+
code: function() {
69+
/**
70+
* Foo
71+
*/
72+
function foo () {}
73+
},
74+
errors: 1
5575
}
5676
/* jshint ignore:end */
5777
]);

0 commit comments

Comments
 (0)