Skip to content

Commit

Permalink
Fix: dont stick IIFE with outpadded docblock
Browse files Browse the repository at this point in the history
Fixes #176
  • Loading branch information
qfox committed Dec 7, 2015
1 parent 0094e85 commit 4735603
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/rules/validate-jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function patchNodesInFile(file) {

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

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

Expand All @@ -267,13 +267,25 @@ function patchNodesInFile(file) {
* @param {?module:esprima/Node} node
* @returns {?module:esprima/Node}
*/
function findDocCommentBeforeNode(node) {
function findDocCommentBeforeNode(node, self) {
var res = file.getPrevToken(file.getFirstNodeToken(node), {includeComments: true});
if (res && res.type === 'Block' && res.value.charAt(0) === '*' &&
res.loc.start.column === node.loc.start.column) {
return res;
if (!res || res.type !== 'Block' || res.value.charAt(0) !== '*') {
return null;
}
return null;

// Indent should be the same
if (res.loc.start.column !== node.loc.start.column) {
return null;
}

// IIFE should be on the next line to be sticked
if ((self.type === 'FunctionExpression' || self.type === 'ArrowFunctionExpression') &&
self.parentNode.type === 'CallExpression' &&
(self.loc.start.line > res.loc.end.line + 2)) {
return null;
}

return res;
}

// mark object as patched
Expand Down
15 changes: 15 additions & 0 deletions test/lib/rules/validate-jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ describe('lib/rules/validate-jsdoc', function () {
function foo () {}
},
errors: 1
}, {
it: 'should not stick docblock to IIFE #176',
rules: {enforceExistence: true},
code: function() {
/**
* Descriptive description of bar.
*
* @method bar
* @param x
*/

( function ( $ ) {
// dummy
}( jQuery ) );
}
}
/* jshint ignore:end */
]);
Expand Down

0 comments on commit 4735603

Please sign in to comment.