| 
									
										
										
										
											2017-01-10 00:11:34 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-17 20:00:47 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 23:18:45 +08:00
										 |  |  | /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */ | 
					
						
							|  |  |  | /** @typedef {import("./Dependency").SourcePosition} SourcePosition */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-07-11 17:53:31 +08:00
										 |  |  |  * @param {SourcePosition} pos position | 
					
						
							| 
									
										
										
										
											2018-07-10 23:18:45 +08:00
										 |  |  |  * @returns {string} formatted position | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | const formatPosition = pos => { | 
					
						
							| 
									
										
										
										
											2018-07-11 17:53:31 +08:00
										 |  |  | 	if (pos && typeof pos === "object") { | 
					
						
							| 
									
										
										
										
											2018-07-10 23:18:45 +08:00
										 |  |  | 		if ("line" in pos && "column" in pos) { | 
					
						
							|  |  |  | 			return `${pos.line}:${pos.column}`; | 
					
						
							|  |  |  | 		} else if ("line" in pos) { | 
					
						
							|  |  |  | 			return `${pos.line}:?`; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-04-17 20:00:47 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-10 23:18:45 +08:00
										 |  |  | 	return ""; | 
					
						
							| 
									
										
										
										
											2017-04-17 20:00:47 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 23:18:45 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-07-11 17:53:31 +08:00
										 |  |  |  * @param {DependencyLocation} loc location | 
					
						
							| 
									
										
										
										
											2018-07-10 23:18:45 +08:00
										 |  |  |  * @returns {string} formatted location | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | const formatLocation = loc => { | 
					
						
							| 
									
										
										
										
											2018-07-11 17:53:31 +08:00
										 |  |  | 	if (loc && typeof loc === "object") { | 
					
						
							| 
									
										
										
										
											2018-07-10 23:18:45 +08:00
										 |  |  | 		if ("start" in loc && loc.start && "end" in loc && loc.end) { | 
					
						
							|  |  |  | 			if ( | 
					
						
							|  |  |  | 				typeof loc.start === "object" && | 
					
						
							|  |  |  | 				typeof loc.start.line === "number" && | 
					
						
							|  |  |  | 				typeof loc.end === "object" && | 
					
						
							|  |  |  | 				typeof loc.end.line === "number" && | 
					
						
							|  |  |  | 				typeof loc.end.column === "number" && | 
					
						
							|  |  |  | 				loc.start.line === loc.end.line | 
					
						
							|  |  |  | 			) { | 
					
						
							|  |  |  | 				return `${formatPosition(loc.start)}-${loc.end.column}`; | 
					
						
							| 
									
										
										
										
											2018-07-15 15:57:36 +08:00
										 |  |  | 			} else if ( | 
					
						
							|  |  |  | 				typeof loc.start === "object" && | 
					
						
							|  |  |  | 				typeof loc.start.line === "number" && | 
					
						
							|  |  |  | 				typeof loc.start.column !== "number" && | 
					
						
							|  |  |  | 				typeof loc.end === "object" && | 
					
						
							|  |  |  | 				typeof loc.end.line === "number" && | 
					
						
							|  |  |  | 				typeof loc.end.column !== "number" | 
					
						
							|  |  |  | 			) { | 
					
						
							|  |  |  | 				return `${loc.start.line}-${loc.end.line}`; | 
					
						
							| 
									
										
										
										
											2018-07-10 23:18:45 +08:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				return `${formatPosition(loc.start)}-${formatPosition(loc.end)}`; | 
					
						
							| 
									
										
										
										
											2017-04-17 20:00:47 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-07-10 23:18:45 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if ("start" in loc && loc.start) { | 
					
						
							|  |  |  | 			return formatPosition(loc.start); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if ("name" in loc && "index" in loc) { | 
					
						
							|  |  |  | 			return `${loc.name}[${loc.index}]`; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if ("name" in loc) { | 
					
						
							|  |  |  | 			return loc.name; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-01-10 00:11:34 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-10 23:18:45 +08:00
										 |  |  | 	return ""; | 
					
						
							| 
									
										
										
										
											2017-01-10 00:11:34 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-04-17 20:00:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = formatLocation; |