@@ -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 }
0 commit comments