| 
									
										
										
										
											2020-09-23 16:38:20 +02:00
										 |  |  | const acorn = require('acorn'); | 
					
						
							|  |  |  | const { commentToData } = require('./comment-to-data'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const parser = acorn.Parser; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function extractComments(js) { | 
					
						
							|  |  |  |   let comments = []; | 
					
						
							|  |  |  |   const file = { data: {} }; | 
					
						
							| 
									
										
										
										
											2021-10-27 16:08:44 +02:00
										 |  |  |   try { | 
					
						
							|  |  |  |     parser.parse(js, { onComment: comments, ecmaVersion: 2020 }); | 
					
						
							|  |  |  |   } catch { | 
					
						
							| 
									
										
										
										
											2021-10-27 16:34:37 +02:00
										 |  |  |     throw Error(`extract-js-comments could not parse the code below, this challenge has invalid syntax:
 | 
					
						
							| 
									
										
										
										
											2020-09-23 16:38:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 16:08:44 +02:00
										 |  |  | ${js} | 
					
						
							|  |  |  | `);
 | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-09-23 16:38:20 +02:00
										 |  |  |   comments | 
					
						
							|  |  |  |     .map(({ value }) => value.trim()) | 
					
						
							|  |  |  |     .forEach(comment => commentToData(file, comment)); | 
					
						
							|  |  |  |   return file.data; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = extractComments; |