| 
									
										
										
										
											2017-01-09 02:11:26 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2017-02-24 21:23:24 +08:00
										 |  |  | const path = require("path"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-08 10:52:49 +08:00
										 |  |  | const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); | 
					
						
							|  |  |  | const ConstDependency = require("./dependencies/ConstDependency"); | 
					
						
							| 
									
										
										
										
											2017-01-08 11:39:06 +08:00
										 |  |  | const UnsupportedFeatureWarning = require("./UnsupportedFeatureWarning"); | 
					
						
							| 
									
										
										
										
											2017-01-08 10:52:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 02:11:26 +08:00
										 |  |  | const ParserHelpers = exports; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ParserHelpers.addParsedVariableToModule = function(parser, name, expression) { | 
					
						
							|  |  |  | 	if(!parser.state.current.addVariable) return false; | 
					
						
							|  |  |  | 	var deps = []; | 
					
						
							|  |  |  | 	parser.parse(expression, { | 
					
						
							|  |  |  | 		current: { | 
					
						
							|  |  |  | 			addDependency: function(dep) { | 
					
						
							|  |  |  | 				dep.userRequest = name; | 
					
						
							|  |  |  | 				deps.push(dep); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		module: parser.state.module | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	parser.state.current.addVariable(name, expression, deps); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-01-08 10:52:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-24 21:23:24 +08:00
										 |  |  | ParserHelpers.requireFileAsExpression = function(context, pathToModule) { | 
					
						
							|  |  |  | 	var moduleJsPath = path.relative(context, pathToModule); | 
					
						
							|  |  |  | 	if(!/^[A-Z]:/i.test(moduleJsPath)) { | 
					
						
							|  |  |  | 		moduleJsPath = "./" + moduleJsPath.replace(/\\/g, "/"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return "require(" + JSON.stringify(moduleJsPath) + ")"; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 18:04:28 +08:00
										 |  |  | ParserHelpers.toConstantDependency = function(value) { | 
					
						
							|  |  |  | 	return function constDependency(expr) { | 
					
						
							| 
									
										
										
										
											2017-01-08 18:24:59 +08:00
										 |  |  | 		var dep = new ConstDependency(value, expr.range); | 
					
						
							| 
									
										
										
										
											2017-01-08 10:52:49 +08:00
										 |  |  | 		dep.loc = expr.loc; | 
					
						
							|  |  |  | 		this.state.current.addDependency(dep); | 
					
						
							|  |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2017-01-08 11:53:14 +08:00
										 |  |  | 	}; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 18:04:28 +08:00
										 |  |  | ParserHelpers.evaluateToString = function(value) { | 
					
						
							|  |  |  | 	return function stringExpression(expr) { | 
					
						
							| 
									
										
										
										
											2017-01-08 11:53:14 +08:00
										 |  |  | 		return new BasicEvaluatedExpression().setString(value).setRange(expr.range); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2017-01-08 10:52:49 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-01-08 11:39:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 18:04:28 +08:00
										 |  |  | ParserHelpers.evaluateToBoolean = function(value) { | 
					
						
							|  |  |  | 	return function booleanExpression(expr) { | 
					
						
							| 
									
										
										
										
											2017-01-08 13:57:17 +08:00
										 |  |  | 		return new BasicEvaluatedExpression().setBoolean(value).setRange(expr.range); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 18:04:28 +08:00
										 |  |  | ParserHelpers.expressionIsUnsupported = function(message) { | 
					
						
							| 
									
										
										
										
											2017-01-08 11:39:06 +08:00
										 |  |  | 	return function unsupportedExpression(expr) { | 
					
						
							|  |  |  | 		var dep = new ConstDependency("(void 0)", expr.range); | 
					
						
							|  |  |  | 		dep.loc = expr.loc; | 
					
						
							|  |  |  | 		this.state.current.addDependency(dep); | 
					
						
							|  |  |  | 		if(!this.state.module) return; | 
					
						
							|  |  |  | 		this.state.module.warnings.push(new UnsupportedFeatureWarning(this.state.module, message)); | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-01-09 17:34:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 20:11:06 +08:00
										 |  |  | ParserHelpers.skipTraversal = function skipTraversal() { | 
					
						
							|  |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2017-01-12 16:39:06 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-01-11 20:11:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | ParserHelpers.approve = function approve() { | 
					
						
							| 
									
										
										
										
											2017-01-09 17:34:47 +08:00
										 |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2017-01-12 16:39:06 +08:00
										 |  |  | }; |