| 
									
										
										
										
											2014-12-06 11:25:17 +00:00
										 |  |  | module.exports = function AttachCommentTreeVisitor(ParseTreeVisitor, log) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function AttachCommentTreeVisitorImpl() { | 
					
						
							|  |  |  |     ParseTreeVisitor.call(this); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   AttachCommentTreeVisitorImpl.prototype = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     __proto__: ParseTreeVisitor.prototype, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     visit: function(tree, comments) { | 
					
						
							|  |  |  |       this.comments = comments; | 
					
						
							|  |  |  |       this.index = 0; | 
					
						
							|  |  |  |       this.currentComment = this.comments[this.index]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (this.currentComment) log.silly('comment: ' + | 
					
						
							|  |  |  |           this.currentComment.range.start.line + ' - ' + | 
					
						
							| 
									
										
										
										
											2015-03-18 17:54:15 +00:00
										 |  |  |           this.currentComment.range.end.line + ' : ' + | 
					
						
							|  |  |  |           this.currentComment.range.toString()); | 
					
						
							| 
									
										
										
										
											2014-12-06 11:25:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       ParseTreeVisitor.prototype.visit.call(this, tree); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Really we ought to subclass ParseTreeVisitor but this is fiddly in ES5 so
 | 
					
						
							|  |  |  |     // it is easier to simply override the prototype's method on the instance
 | 
					
						
							|  |  |  |     visitAny: function(tree) { | 
					
						
							| 
									
										
										
										
											2015-03-18 17:54:15 +00:00
										 |  |  |       if (tree && tree.location && tree.location.start && this.currentComment && | 
					
						
							|  |  |  |         this.currentComment.range.end.offset < tree.location.start.offset) { | 
					
						
							|  |  |  |         log.silly('tree: ' + tree.constructor.name + ' - ' + tree.location.start.line); | 
					
						
							|  |  |  |         while (this.currentComment && | 
					
						
							|  |  |  |                   this.currentComment.range.end.offset < tree.location.start.offset) { | 
					
						
							| 
									
										
										
										
											2015-03-24 11:25:58 +00:00
										 |  |  |           var commentText = this.currentComment.range.toString(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           // Only store the comment if it is JSDOC style (e.g. /** some comment */)
 | 
					
						
							|  |  |  |           if (/^\/\*\*([\w\W]*)\*\/$/.test(commentText)) { | 
					
						
							|  |  |  |             log.info('comment: ' + this.currentComment.range.start.line + ' - ' + | 
					
						
							|  |  |  |                                    this.currentComment.range.end.line + ' : ' + | 
					
						
							|  |  |  |                                    commentText); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             tree.commentBefore = this.currentComment; | 
					
						
							|  |  |  |             this.currentComment.treeAfter = tree; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-06 11:25:17 +00:00
										 |  |  |           this.index++; | 
					
						
							|  |  |  |           this.currentComment = this.comments[this.index]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return ParseTreeVisitor.prototype.visitAny.call(this, tree); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return AttachCommentTreeVisitorImpl; | 
					
						
							|  |  |  | }; |