Skip to content

Commit 4735603

Browse files
committed
Fix: dont stick IIFE with outpadded docblock
Fixes #176
1 parent 0094e85 commit 4735603

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

lib/rules/validate-jsdoc.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ function patchNodesInFile(file) {
240240

241241
var node = functionNodeTypes.indexOf(this.type) !== -1 ?
242242
findFirstNodeInLine(this) : this;
243-
var res = findDocCommentBeforeNode(node);
243+
var res = findDocCommentBeforeNode(node, this);
244244

245245
this._jsdoc = res ? jsdoc.createDocCommentByCommentNode(res) : null;
246246

@@ -267,13 +267,25 @@ function patchNodesInFile(file) {
267267
* @param {?module:esprima/Node} node
268268
* @returns {?module:esprima/Node}
269269
*/
270-
function findDocCommentBeforeNode(node) {
270+
function findDocCommentBeforeNode(node, self) {
271271
var res = file.getPrevToken(file.getFirstNodeToken(node), {includeComments: true});
272-
if (res && res.type === 'Block' && res.value.charAt(0) === '*' &&
273-
res.loc.start.column === node.loc.start.column) {
274-
return res;
272+
if (!res || res.type !== 'Block' || res.value.charAt(0) !== '*') {
273+
return null;
275274
}
276-
return null;
275+
276+
// Indent should be the same
277+
if (res.loc.start.column !== node.loc.start.column) {
278+
return null;
279+
}
280+
281+
// IIFE should be on the next line to be sticked
282+
if ((self.type === 'FunctionExpression' || self.type === 'ArrowFunctionExpression') &&
283+
self.parentNode.type === 'CallExpression' &&
284+
(self.loc.start.line > res.loc.end.line + 2)) {
285+
return null;
286+
}
287+
288+
return res;
277289
}
278290

279291
// mark object as patched

test/lib/rules/validate-jsdoc.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ describe('lib/rules/validate-jsdoc', function () {
7272
function foo () {}
7373
},
7474
errors: 1
75+
}, {
76+
it: 'should not stick docblock to IIFE #176',
77+
rules: {enforceExistence: true},
78+
code: function() {
79+
/**
80+
* Descriptive description of bar.
81+
*
82+
* @method bar
83+
* @param x
84+
*/
85+
86+
( function ( $ ) {
87+
// dummy
88+
}( jQuery ) );
89+
}
7590
}
7691
/* jshint ignore:end */
7792
]);

0 commit comments

Comments
 (0)