@@ -213,7 +213,6 @@ function patchNodesInFile(file) {
213
213
}
214
214
215
215
// jsdoc property for nodes
216
- var fileComments = file . getComments ( ) ;
217
216
file . iterateNodesByType ( [
218
217
'FunctionDeclaration' , 'FunctionExpression'
219
218
] , function ( node ) {
@@ -230,27 +229,38 @@ function patchNodesInFile(file) {
230
229
*/
231
230
function getJsdoc ( ) {
232
231
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 ) ;
234
234
this . _jsdoc = res ? jsdoc . createDocCommentByCommentNode ( res ) : null ;
235
235
}
236
236
return this . _jsdoc ;
237
237
}
238
238
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
+
239
253
/**
240
254
* Finds DocComment in file before passed line number
241
255
*
242
- * @param {number } line
256
+ * @param {?module:esprima/Node } node
243
257
* @returns {?module:esprima/Node }
244
258
*/
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 ;
254
264
}
255
265
return null ;
256
266
}
0 commit comments