| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | const should = require("should"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | const Parser = require("../lib/Parser"); | 
					
						
							|  |  |  | const BasicEvaluatedExpression = require("../lib/BasicEvaluatedExpression"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe("Parser", () => { | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 	/* eslint-disable no-undef */ | 
					
						
							|  |  |  | 	/* eslint-disable no-unused-vars */ | 
					
						
							|  |  |  | 	/* eslint-disable no-inner-declarations */ | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 	const testCases = { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		"call ident": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							|  |  |  | 				abc("test"); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			}, { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				abc: ["test"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							|  |  |  | 		"call member": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							|  |  |  | 				cde.abc("membertest"); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			}, { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				cdeabc: ["membertest"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2016-06-04 18:06:10 +08:00
										 |  |  | 		"call member using bracket notation": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							|  |  |  | 				cde["abc"]("membertest"); | 
					
						
							|  |  |  | 			}, { | 
					
						
							|  |  |  | 				cdeabc: ["membertest"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		"call inner member": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							|  |  |  | 				cde.ddd.abc("inner"); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			}, { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				cdedddabc: ["inner"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2016-06-04 18:06:10 +08:00
										 |  |  | 		"call inner member using bracket notation": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							|  |  |  | 				cde.ddd["abc"]("inner"); | 
					
						
							|  |  |  | 			}, { | 
					
						
							|  |  |  | 				cdedddabc: ["inner"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		"expression": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							|  |  |  | 				fgh; | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			}, { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				fgh: [""] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							|  |  |  | 		"expression sub": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							|  |  |  | 				fgh.sub; | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			}, { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				fghsub: ["notry"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2014-12-16 15:00:05 +08:00
										 |  |  | 		"member expression": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 				test[memberExpr]; | 
					
						
							|  |  |  | 				test[+memberExpr]; | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			}, { | 
					
						
							| 
									
										
										
										
											2014-12-16 15:00:05 +08:00
										 |  |  | 				expressions: ["memberExpr", "memberExpr"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		"in function definition": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							|  |  |  | 				(function(abc, cde, fgh) { | 
					
						
							|  |  |  | 					abc("test"); | 
					
						
							|  |  |  | 					cde.abc("test"); | 
					
						
							|  |  |  | 					cde.ddd.abc("test"); | 
					
						
							|  |  |  | 					fgh; | 
					
						
							|  |  |  | 					fgh.sub; | 
					
						
							|  |  |  | 				})(); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			}, {} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		], | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 		"const definition": [ | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			function() { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 				let abc, cde, fgh; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				abc("test"); | 
					
						
							|  |  |  | 				cde.abc("test"); | 
					
						
							|  |  |  | 				cde.ddd.abc("test"); | 
					
						
							|  |  |  | 				fgh; | 
					
						
							|  |  |  | 				fgh.sub; | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			}, {} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		], | 
					
						
							| 
									
										
										
										
											2017-01-24 17:39:17 +08:00
										 |  |  | 		"var definition": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							| 
									
										
										
										
											2017-01-25 20:43:17 +08:00
										 |  |  | 				var abc, cde, fgh; | 
					
						
							| 
									
										
										
										
											2017-01-24 17:39:17 +08:00
										 |  |  | 				abc("test"); | 
					
						
							|  |  |  | 				cde.abc("test"); | 
					
						
							|  |  |  | 				cde.ddd.abc("test"); | 
					
						
							|  |  |  | 				fgh; | 
					
						
							|  |  |  | 				fgh.sub; | 
					
						
							|  |  |  | 			}, {} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		"function definition": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							|  |  |  | 				function abc() {} | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				function cde() {} | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				function fgh() {} | 
					
						
							|  |  |  | 				abc("test"); | 
					
						
							|  |  |  | 				cde.abc("test"); | 
					
						
							|  |  |  | 				cde.ddd.abc("test"); | 
					
						
							|  |  |  | 				fgh; | 
					
						
							|  |  |  | 				fgh.sub; | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			}, {} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		], | 
					
						
							| 
									
										
										
										
											2017-12-30 23:27:17 +08:00
										 |  |  | 		"class definition": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							|  |  |  | 				class memberExpr { | 
					
						
							|  |  |  | 					cde() { | 
					
						
							|  |  |  | 						abc("cde"); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					static fgh() { | 
					
						
							|  |  |  | 						abc("fgh"); | 
					
						
							|  |  |  | 						fgh(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, { | 
					
						
							|  |  |  | 				abc: ["cde", "fgh"], | 
					
						
							|  |  |  | 				fgh: ["memberExpr"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		"in try": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							|  |  |  | 				try { | 
					
						
							|  |  |  | 					fgh.sub; | 
					
						
							|  |  |  | 					fgh; | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 					function test(ttt) { | 
					
						
							|  |  |  | 						fgh.sub; | 
					
						
							|  |  |  | 						fgh; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} catch(e) { | 
					
						
							|  |  |  | 					fgh.sub; | 
					
						
							|  |  |  | 					fgh; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			}, { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				fghsub: ["try", "notry", "notry"], | 
					
						
							| 
									
										
										
										
											2017-04-13 13:52:05 +08:00
										 |  |  | 				fgh: ["test", "test ttt", "test e"] | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 		"renaming with const": [ | 
					
						
							| 
									
										
										
										
											2014-01-31 17:57:28 +08:00
										 |  |  | 			function() { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 				const xyz = abc; | 
					
						
							| 
									
										
										
										
											2014-01-31 17:57:28 +08:00
										 |  |  | 				xyz("test"); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			}, { | 
					
						
							| 
									
										
										
										
											2014-01-31 17:57:28 +08:00
										 |  |  | 				abc: ["test"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2017-01-24 17:39:17 +08:00
										 |  |  | 		"renaming with var": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							| 
									
										
										
										
											2017-01-25 20:43:17 +08:00
										 |  |  | 				var xyz = abc; | 
					
						
							| 
									
										
										
										
											2017-01-24 17:39:17 +08:00
										 |  |  | 				xyz("test"); | 
					
						
							|  |  |  | 			}, { | 
					
						
							|  |  |  | 				abc: ["test"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2014-01-31 17:57:28 +08:00
										 |  |  | 		"renaming with assignment": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 				const xyz = abc; | 
					
						
							| 
									
										
										
										
											2014-01-31 17:57:28 +08:00
										 |  |  | 				xyz("test"); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			}, { | 
					
						
							| 
									
										
										
										
											2014-01-31 17:57:28 +08:00
										 |  |  | 				abc: ["test"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							|  |  |  | 		"renaming with IIFE": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 				! function(xyz) { | 
					
						
							| 
									
										
										
										
											2014-01-31 17:57:28 +08:00
										 |  |  | 					xyz("test"); | 
					
						
							|  |  |  | 				}(abc); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			}, { | 
					
						
							| 
									
										
										
										
											2014-01-31 17:57:28 +08:00
										 |  |  | 				abc: ["test"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2017-06-16 21:31:45 +08:00
										 |  |  | 		"renaming arguments with IIFE (called)": [ | 
					
						
							| 
									
										
										
										
											2014-01-31 17:57:28 +08:00
										 |  |  | 			function() { | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 				! function(xyz) { | 
					
						
							| 
									
										
										
										
											2014-01-31 17:57:28 +08:00
										 |  |  | 					xyz("test"); | 
					
						
							|  |  |  | 				}.call(fgh, abc); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			}, { | 
					
						
							| 
									
										
										
										
											2014-01-31 17:57:28 +08:00
										 |  |  | 				abc: ["test"], | 
					
						
							|  |  |  | 				fgh: [""] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2017-06-16 21:31:45 +08:00
										 |  |  | 		"renaming this's properties with IIFE (called)": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							|  |  |  | 				! function() { | 
					
						
							|  |  |  | 					this.sub; | 
					
						
							|  |  |  | 				}.call(ijk); | 
					
						
							|  |  |  | 			}, { | 
					
						
							|  |  |  | 				ijksub: ["test"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2017-07-12 00:12:15 +08:00
										 |  |  | 		"renaming this's properties with nested IIFE (called)": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							|  |  |  | 				! function() { | 
					
						
							|  |  |  | 					! function() { | 
					
						
							|  |  |  | 						this.sub; | 
					
						
							|  |  |  | 					}.call(this); | 
					
						
							|  |  |  | 				}.call(ijk); | 
					
						
							|  |  |  | 			}, { | 
					
						
							|  |  |  | 				ijksub: ["test"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2017-12-05 10:03:50 +08:00
										 |  |  | 		"new Foo(...)": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							| 
									
										
										
										
											2017-12-05 10:40:23 +08:00
										 |  |  | 				new xyz("membertest"); | 
					
						
							| 
									
										
										
										
											2017-12-05 10:03:50 +08:00
										 |  |  | 			}, { | 
					
						
							|  |  |  | 				xyz: ["membertest"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2017-12-30 23:27:17 +08:00
										 |  |  | 		"spread calls/literals": [ | 
					
						
							|  |  |  | 			function() { | 
					
						
							|  |  |  | 				var xyz = [...abc("xyz"), cde]; | 
					
						
							|  |  |  | 				Math.max(...fgh); | 
					
						
							|  |  |  | 			}, { | 
					
						
							|  |  |  | 				abc: ["xyz"], | 
					
						
							|  |  |  | 				fgh: ["xyz"] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		] | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 	/* eslint-enable no-undef */ | 
					
						
							|  |  |  | 	/* eslint-enable no-unused-vars */ | 
					
						
							|  |  |  | 	/* eslint-enable no-inner-declarations */ | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 	Object.keys(testCases).forEach((name) => { | 
					
						
							|  |  |  | 		it("should parse " + name, () => { | 
					
						
							|  |  |  | 			let source = testCases[name][0].toString(); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			source = source.substr(13, source.length - 14).trim(); | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 			const state = testCases[name][1]; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 			const testParser = new Parser({}); | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 			testParser.hooks.canRename.tap("abc", "ParserTest", (expr) => true); | 
					
						
							|  |  |  | 			testParser.hooks.canRename.tap("ijk", "ParserTest", (expr) => true); | 
					
						
							|  |  |  | 			testParser.hooks.call.tap("abc", "ParserTest", (expr) => { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 				if(!testParser.state.abc) testParser.state.abc = []; | 
					
						
							|  |  |  | 				testParser.state.abc.push(testParser.parseString(expr.arguments[0])); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				return true; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 			testParser.hooks.call.tap("cde.abc", "ParserTest", (expr) => { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 				if(!testParser.state.cdeabc) testParser.state.cdeabc = []; | 
					
						
							|  |  |  | 				testParser.state.cdeabc.push(testParser.parseString(expr.arguments[0])); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				return true; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 			testParser.hooks.call.tap("cde.ddd.abc", "ParserTest", (expr) => { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 				if(!testParser.state.cdedddabc) testParser.state.cdedddabc = []; | 
					
						
							|  |  |  | 				testParser.state.cdedddabc.push(testParser.parseString(expr.arguments[0])); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				return true; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 			testParser.hooks.expression.tap("fgh", "ParserTest", (expr) => { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 				if(!testParser.state.fgh) testParser.state.fgh = []; | 
					
						
							| 
									
										
										
										
											2017-07-20 14:34:04 +08:00
										 |  |  | 				testParser.state.fgh.push(Array.from(testParser.scope.definitions.asSet()).join(" ")); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				return true; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 			testParser.hooks.expression.tap("fgh.sub", "ParserTest", (expr) => { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 				if(!testParser.state.fghsub) testParser.state.fghsub = []; | 
					
						
							|  |  |  | 				testParser.state.fghsub.push(testParser.scope.inTry ? "try" : "notry"); | 
					
						
							| 
									
										
										
										
											2017-06-16 21:31:45 +08:00
										 |  |  | 				return true; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 			testParser.hooks.expression.tap("ijk.sub", "ParserTest", (expr) => { | 
					
						
							| 
									
										
										
										
											2017-06-16 21:31:45 +08:00
										 |  |  | 				if(!testParser.state.ijksub) testParser.state.ijksub = []; | 
					
						
							|  |  |  | 				testParser.state.ijksub.push("test"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				return true; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 			testParser.hooks.expression.tap("memberExpr", "ParserTest", (expr) => { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 				if(!testParser.state.expressions) testParser.state.expressions = []; | 
					
						
							|  |  |  | 				testParser.state.expressions.push(expr.name); | 
					
						
							| 
									
										
										
										
											2014-12-16 15:00:05 +08:00
										 |  |  | 				return true; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 			testParser.hooks.new.tap("xyz", "ParserTest", (expr) => { | 
					
						
							| 
									
										
										
										
											2017-12-05 10:03:50 +08:00
										 |  |  | 				if(!testParser.state.xyz) testParser.state.xyz = []; | 
					
						
							|  |  |  | 				testParser.state.xyz.push(testParser.parseString(expr.arguments[0])); | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 			const actual = testParser.parse(source); | 
					
						
							| 
									
										
										
										
											2013-10-16 14:57:37 +08:00
										 |  |  | 			should.strictEqual(typeof actual, "object"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			actual.should.be.eql(state); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 	it("should parse comments", () => { | 
					
						
							|  |  |  | 		const source = "//comment1\n/*comment2*/"; | 
					
						
							|  |  |  | 		const state = [{ | 
					
						
							|  |  |  | 			type: "Line", | 
					
						
							|  |  |  | 			value: "comment1" | 
					
						
							| 
									
										
										
										
											2016-09-09 03:41:03 +08:00
										 |  |  | 		}, { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 			type: "Block", | 
					
						
							|  |  |  | 			value: "comment2" | 
					
						
							| 
									
										
										
										
											2016-09-09 03:41:03 +08:00
										 |  |  | 		}]; | 
					
						
							| 
									
										
										
										
											2016-09-09 02:52:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 		const testParser = new Parser({}); | 
					
						
							| 
									
										
										
										
											2016-09-09 02:52:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 		testParser.hooks.program.tap("ParserTest", (ast, comments) => { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 			if(!testParser.state.comments) testParser.state.comments = comments; | 
					
						
							| 
									
										
										
										
											2016-09-09 02:52:53 +08:00
										 |  |  | 			return true; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 		const actual = testParser.parse(source); | 
					
						
							| 
									
										
										
										
											2016-09-09 02:52:53 +08:00
										 |  |  | 		should.strictEqual(typeof actual, "object"); | 
					
						
							|  |  |  | 		should.strictEqual(typeof actual.comments, "object"); | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 		actual.comments.forEach((element, index) => { | 
					
						
							| 
									
										
										
										
											2016-09-09 02:52:53 +08:00
										 |  |  | 			should.strictEqual(typeof element.type, "string"); | 
					
						
							|  |  |  | 			should.strictEqual(typeof element.value, "string"); | 
					
						
							|  |  |  | 			element.type.should.be.eql(state[index].type); | 
					
						
							|  |  |  | 			element.value.should.be.eql(state[index].value); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 	describe("expression evaluation", () => { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		function evaluateInParser(source) { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 			const parser = new Parser(); | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 			parser.hooks.call.tap("test", "ParserTest", (expr) => { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 				parser.state.result = parser.evaluateExpression(expr.arguments[0]); | 
					
						
							| 
									
										
										
										
											2014-12-16 15:12:30 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 			parser.hooks.evaluateIdentifier.tap("aString", "ParserTest", (expr) => | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 				new BasicEvaluatedExpression().setString("aString").setRange(expr.range)); | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 			parser.hooks.evaluateIdentifier.tap("b.Number", "ParserTest", (expr) => | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 				new BasicEvaluatedExpression().setNumber(123).setRange(expr.range)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			return parser.parse("test(" + source + ");").result; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 		const testCases = { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			"\"strrring\"": "string=strrring", | 
					
						
							|  |  |  | 			"\"strr\" + \"ring\"": "string=strrring", | 
					
						
							|  |  |  | 			"\"s\" + (\"trr\" + \"rin\") + \"g\"": "string=strrring", | 
					
						
							|  |  |  | 			"'S' + (\"strr\" + \"ring\") + 'y'": "string=Sstrrringy", | 
					
						
							| 
									
										
										
										
											2017-04-13 20:52:49 +08:00
										 |  |  | 			"/abc/": "regExp=/abc/", | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			"1": "number=1", | 
					
						
							|  |  |  | 			"1 + 3": "number=4", | 
					
						
							| 
									
										
										
										
											2017-04-13 20:52:49 +08:00
										 |  |  | 			"3 - 1": "number=2", | 
					
						
							| 
									
										
										
										
											2017-12-06 00:11:53 +08:00
										 |  |  | 			"2 * 3": "number=6", | 
					
						
							|  |  |  | 			"8 / 2": "number=4", | 
					
						
							|  |  |  | 			"2 ** 3": "number=8", | 
					
						
							|  |  |  | 			"12 & 5": "number=4", | 
					
						
							|  |  |  | 			"12 | 5": "number=13", | 
					
						
							|  |  |  | 			"12 ^ 5": "number=9", | 
					
						
							|  |  |  | 			"9 >>> 2": "number=2", | 
					
						
							|  |  |  | 			"9 >> 2": "number=2", | 
					
						
							|  |  |  | 			"9 << 2": "number=36", | 
					
						
							|  |  |  | 			"~3": "number=-4", | 
					
						
							| 
									
										
										
										
											2017-04-13 20:52:49 +08:00
										 |  |  | 			"1 == 1": "bool=true", | 
					
						
							|  |  |  | 			"1 === 1": "bool=true", | 
					
						
							|  |  |  | 			"3 != 1": "bool=true", | 
					
						
							|  |  |  | 			"3 !== 1": "bool=true", | 
					
						
							|  |  |  | 			"3 == 1": "bool=false", | 
					
						
							|  |  |  | 			"3 === 1": "bool=false", | 
					
						
							|  |  |  | 			"1 != 1": "bool=false", | 
					
						
							|  |  |  | 			"1 !== 1": "bool=false", | 
					
						
							|  |  |  | 			"true === false": "bool=false", | 
					
						
							|  |  |  | 			"false !== false": "bool=false", | 
					
						
							|  |  |  | 			"true == true": "bool=true", | 
					
						
							|  |  |  | 			"false != true": "bool=true", | 
					
						
							|  |  |  | 			"!'a'": "bool=false", | 
					
						
							|  |  |  | 			"!''": "bool=true", | 
					
						
							| 
									
										
										
										
											2015-02-28 08:42:09 +08:00
										 |  |  | 			"'pre' + a": "wrapped=['pre' string=pre]+[null]", | 
					
						
							|  |  |  | 			"a + 'post'": "wrapped=[null]+['post' string=post]", | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			"'pre' + a + 'post'": "wrapped=['pre' string=pre]+['post' string=post]", | 
					
						
							|  |  |  | 			"1 + a + 2": "", | 
					
						
							| 
									
										
										
										
											2015-02-28 08:42:09 +08:00
										 |  |  | 			"1 + a + 'post'": "wrapped=[null]+['post' string=post]", | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			"'' + 1 + a + 2": "wrapped=['' + 1 string=1]+[2 string=2]", | 
					
						
							|  |  |  | 			"'' + 1 + a + 2 + 3": "wrapped=['' + 1 string=1]+[2 + 3 string=23]", | 
					
						
							|  |  |  | 			"'' + 1 + a + (2 + 3)": "wrapped=['' + 1 string=1]+[2 + 3 string=5]", | 
					
						
							|  |  |  | 			"'pre' + (1 + a) + (2 + 3)": "wrapped=['pre' string=pre]+[2 + 3 string=5]", | 
					
						
							|  |  |  | 			"a ? 'o1' : 'o2'": "options=['o1' string=o1],['o2' string=o2]", | 
					
						
							|  |  |  | 			"a ? 'o1' : b ? 'o2' : 'o3'": "options=['o1' string=o1],['o2' string=o2],['o3' string=o3]", | 
					
						
							|  |  |  | 			"a ? (b ? 'o1' : 'o2') : 'o3'": "options=['o1' string=o1],['o2' string=o2],['o3' string=o3]", | 
					
						
							|  |  |  | 			"a ? (b ? 'o1' : 'o2') : c ? 'o3' : 'o4'": "options=['o1' string=o1],['o2' string=o2],['o3' string=o3],['o4' string=o4]", | 
					
						
							|  |  |  | 			"a ? 'o1' : b ? 'o2' : c ? 'o3' : 'o4'": "options=['o1' string=o1],['o2' string=o2],['o3' string=o3],['o4' string=o4]", | 
					
						
							|  |  |  | 			"a ? 'o1' : b ? b : c ? 'o3' : c": "options=['o1' string=o1],[b],['o3' string=o3],[c]", | 
					
						
							|  |  |  | 			"['i1', 'i2', 3, a, b ? 4 : 5]": "items=['i1' string=i1],['i2' string=i2],[3 number=3],[a],[b ? 4 : 5 options=[4 number=4],[5 number=5]]", | 
					
						
							| 
									
										
										
										
											2014-12-16 15:12:30 +08:00
										 |  |  | 			"typeof 'str'": "string=string", | 
					
						
							|  |  |  | 			"typeof aString": "string=string", | 
					
						
							|  |  |  | 			"typeof b.Number": "string=number", | 
					
						
							| 
									
										
										
										
											2016-06-04 18:06:10 +08:00
										 |  |  | 			"typeof b['Number']": "string=number", | 
					
						
							| 
									
										
										
										
											2014-12-16 15:12:30 +08:00
										 |  |  | 			"typeof b[Number]": "", | 
					
						
							|  |  |  | 			"b.Number": "number=123", | 
					
						
							| 
									
										
										
										
											2016-06-04 18:06:10 +08:00
										 |  |  | 			"b['Number']": "number=123", | 
					
						
							| 
									
										
										
										
											2014-12-16 15:12:30 +08:00
										 |  |  | 			"b[Number]": "", | 
					
						
							| 
									
										
										
										
											2017-09-15 02:35:33 +08:00
										 |  |  | 			"'str'.concat()": "string=str", | 
					
						
							|  |  |  | 			"'str'.concat('one')": "string=strone", | 
					
						
							|  |  |  | 			"'str'.concat('one').concat('two')": "string=stronetwo", | 
					
						
							|  |  |  | 			"'str'.concat('one').concat('two', 'three')": "string=stronetwothree", | 
					
						
							|  |  |  | 			"'str'.concat('one', 'two')": "string=stronetwo", | 
					
						
							|  |  |  | 			"'str'.concat('one', 'two').concat('three')": "string=stronetwothree", | 
					
						
							|  |  |  | 			"'str'.concat('one', 'two').concat('three', 'four')": "string=stronetwothreefour", | 
					
						
							|  |  |  | 			"'str'.concat('one', obj)": "wrapped=['str' string=str]+[null]", | 
					
						
							|  |  |  | 			"'str'.concat('one', obj).concat()": "wrapped=['str' string=str]+[null]", | 
					
						
							|  |  |  | 			"'str'.concat('one', obj, 'two')": "wrapped=['str' string=str]+['two' string=two]", | 
					
						
							|  |  |  | 			"'str'.concat('one', obj, 'two').concat()": "wrapped=['str' string=str]+['two' string=two]", | 
					
						
							|  |  |  | 			"'str'.concat('one', obj, 'two').concat('three')": "wrapped=['str' string=str]+['three' string=three]", | 
					
						
							|  |  |  | 			"'str'.concat(obj)": "wrapped=['str' string=str]+[null]", | 
					
						
							|  |  |  | 			"'str'.concat(obj).concat()": "wrapped=['str' string=str]+[null]", | 
					
						
							|  |  |  | 			"'str'.concat(obj).concat('one', 'two')": "wrapped=['str' string=str]+['one', 'two' string=onetwo]", | 
					
						
							|  |  |  | 			"'str'.concat(obj).concat(obj, 'one')": "wrapped=['str' string=str]+['one' string=one]", | 
					
						
							|  |  |  | 			"'str'.concat(obj).concat(obj, 'one', 'two')": "wrapped=['str' string=str]+['one', 'two' string=onetwo]", | 
					
						
							|  |  |  | 			"'str'.concat(obj).concat('one', obj, 'one')": "wrapped=['str' string=str]+['one' string=one]", | 
					
						
							|  |  |  | 			"'str'.concat(obj).concat('one', obj, 'two', 'three')": "wrapped=['str' string=str]+['two', 'three' string=twothree]", | 
					
						
							|  |  |  | 			"'str'.concat(obj, 'one')": "wrapped=['str' string=str]+['one' string=one]", | 
					
						
							|  |  |  | 			"'str'.concat(obj, 'one').concat()": "wrapped=['str' string=str]+['one' string=one]", | 
					
						
							|  |  |  | 			"'str'.concat(obj, 'one').concat('two', 'three')": "wrapped=['str' string=str]+['two', 'three' string=twothree]", | 
					
						
							|  |  |  | 			"'str'.concat(obj, 'one').concat(obj, 'two', 'three')": "wrapped=['str' string=str]+['two', 'three' string=twothree]", | 
					
						
							|  |  |  | 			"'str'.concat(obj, 'one').concat('two', obj, 'three')": "wrapped=['str' string=str]+['three' string=three]", | 
					
						
							|  |  |  | 			"'str'.concat(obj, 'one').concat('two', obj, 'three', 'four')": "wrapped=['str' string=str]+['three', 'four' string=threefour]", | 
					
						
							|  |  |  | 			"'str'.concat(obj, 'one', 'two')": "wrapped=['str' string=str]+['one', 'two' string=onetwo]", | 
					
						
							|  |  |  | 			"'str'.concat(obj, 'one', 'two').concat()": "wrapped=['str' string=str]+['one', 'two' string=onetwo]", | 
					
						
							|  |  |  | 			"'str'.concat(obj, 'one', 'two').concat('three', 'four')": "wrapped=['str' string=str]+['three', 'four' string=threefour]", | 
					
						
							|  |  |  | 			"'str'.concat(obj, 'one', 'two').concat(obj, 'three', 'four')": "wrapped=['str' string=str]+['three', 'four' string=threefour]", | 
					
						
							|  |  |  | 			"'str'.concat(obj, 'one', 'two').concat('three', obj, 'four')": "wrapped=['str' string=str]+['four' string=four]", | 
					
						
							|  |  |  | 			"'str'.concat(obj, 'one', 'two').concat('three', obj, 'four', 'five')": "wrapped=['str' string=str]+['four', 'five' string=fourfive]", | 
					
						
							| 
									
										
										
										
											2017-09-15 01:26:25 +08:00
										 |  |  | 			"`start${obj}mid${obj2}end`": "template=[start string=start],[mid string=mid],[end string=end]", // eslint-disable-line no-template-curly-in-string
 | 
					
						
							|  |  |  | 			"`start${'str'}mid${obj2}end`": "template=[start${'str'}mid string=startstrmid],[end string=end]", // eslint-disable-line no-template-curly-in-string
 | 
					
						
							| 
									
										
										
										
											2014-12-16 15:12:30 +08:00
										 |  |  | 			"'abc'.substr(1)": "string=bc", | 
					
						
							| 
									
										
										
										
											2017-04-13 20:52:49 +08:00
										 |  |  | 			"'abcdef'.substr(2, 3)": "string=cde", | 
					
						
							|  |  |  | 			"'abcdef'.substring(2, 3)": "string=c", | 
					
						
							|  |  |  | 			"'abcdef'.substring(2, 3, 4)": "", | 
					
						
							| 
									
										
										
										
											2016-06-04 18:06:10 +08:00
										 |  |  | 			"'abc'[\"substr\"](1)": "string=bc", | 
					
						
							| 
									
										
										
										
											2014-12-16 15:12:30 +08:00
										 |  |  | 			"'abc'[substr](1)": "", | 
					
						
							| 
									
										
										
										
											2017-04-13 20:52:49 +08:00
										 |  |  | 			"'1,2+3'.split(/[,+]/)": "array=[1],[2],[3]", | 
					
						
							|  |  |  | 			"'1,2+3'.split(expr)": "", | 
					
						
							|  |  |  | 			"'a' + (expr + 'c')": "wrapped=['a' string=a]+['c' string=c]", | 
					
						
							|  |  |  | 			"1 + 'a'": "string=1a", | 
					
						
							|  |  |  | 			"'a' + 1": "string=a1", | 
					
						
							|  |  |  | 			"'a' + expr + 1": "wrapped=['a' string=a]+[1 string=1]", | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 		Object.keys(testCases).forEach((key) => { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			function evalExprToString(evalExpr) { | 
					
						
							|  |  |  | 				if(!evalExpr) { | 
					
						
							|  |  |  | 					return "null"; | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 					const result = []; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 					if(evalExpr.isString()) result.push("string=" + evalExpr.string); | 
					
						
							|  |  |  | 					if(evalExpr.isNumber()) result.push("number=" + evalExpr.number); | 
					
						
							| 
									
										
										
										
											2017-04-13 20:52:49 +08:00
										 |  |  | 					if(evalExpr.isBoolean()) result.push("bool=" + evalExpr.bool); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 					if(evalExpr.isRegExp()) result.push("regExp=" + evalExpr.regExp); | 
					
						
							|  |  |  | 					if(evalExpr.isConditional()) result.push("options=[" + evalExpr.options.map(evalExprToString).join("],[") + "]"); | 
					
						
							|  |  |  | 					if(evalExpr.isArray()) result.push("items=[" + evalExpr.items.map(evalExprToString).join("],[") + "]"); | 
					
						
							| 
									
										
										
										
											2017-04-13 20:52:49 +08:00
										 |  |  | 					if(evalExpr.isConstArray()) result.push("array=[" + evalExpr.array.join("],[") + "]"); | 
					
						
							| 
									
										
										
										
											2017-09-15 01:26:25 +08:00
										 |  |  | 					if(evalExpr.isTemplateString()) result.push("template=[" + evalExpr.quasis.map(evalExprToString).join("],[") + "]"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 					if(evalExpr.isWrapped()) result.push("wrapped=[" + evalExprToString(evalExpr.prefix) + "]+[" + evalExprToString(evalExpr.postfix) + "]"); | 
					
						
							|  |  |  | 					if(evalExpr.range) { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 						const start = evalExpr.range[0] - 5; | 
					
						
							|  |  |  | 						const end = evalExpr.range[1] - 5; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 						return key.substr(start, end - start) + (result.length > 0 ? " " + result.join(" ") : ""); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					return result.join(" "); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 			it("should eval " + key, () => { | 
					
						
							|  |  |  | 				const evalExpr = evaluateInParser(key); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				evalExprToString(evalExpr).should.be.eql(testCases[key] ? key + " " + testCases[key] : key); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-10-31 09:48:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 	describe("async/await support", () => { | 
					
						
							|  |  |  | 		describe("should accept", () => { | 
					
						
							|  |  |  | 			const cases = { | 
					
						
							| 
									
										
										
										
											2016-10-31 09:48:33 +08:00
										 |  |  | 				"async function": "async function x() {}", | 
					
						
							|  |  |  | 				"async arrow function": "async () => {}", | 
					
						
							|  |  |  | 				"await expression": "async function x(y) { await y }" | 
					
						
							|  |  |  | 			}; | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 			const parser = new Parser(); | 
					
						
							|  |  |  | 			Object.keys(cases).forEach((name) => { | 
					
						
							|  |  |  | 				const expr = cases[name]; | 
					
						
							|  |  |  | 				it(name, () => { | 
					
						
							|  |  |  | 					const actual = parser.parse(expr); | 
					
						
							| 
									
										
										
										
											2016-10-31 09:48:33 +08:00
										 |  |  | 					should.strictEqual(typeof actual, "object"); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 		describe("should parse await", () => { | 
					
						
							|  |  |  | 			const cases = { | 
					
						
							| 
									
										
										
										
											2016-10-31 09:48:33 +08:00
										 |  |  | 				"require": [ | 
					
						
							|  |  |  | 					"async function x() { await require('y'); }", { | 
					
						
							|  |  |  | 						param: "y" | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				], | 
					
						
							|  |  |  | 				"System.import": [ | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 					"async function x() { const y = await System.import('z'); }", { | 
					
						
							| 
									
										
										
										
											2016-10-31 09:48:33 +08:00
										 |  |  | 						param: "z" | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				] | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 			const parser = new Parser(); | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 			parser.hooks.call.tap("require", "ParserTest", (expr) => { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 				const param = parser.evaluateExpression(expr.arguments[0]); | 
					
						
							|  |  |  | 				parser.state.param = param.string; | 
					
						
							| 
									
										
										
										
											2016-10-31 09:48:33 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 			parser.hooks.call.tap("System.import", "ParserTest", (expr) => { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 				const param = parser.evaluateExpression(expr.arguments[0]); | 
					
						
							|  |  |  | 				parser.state.param = param.string; | 
					
						
							| 
									
										
										
										
											2016-10-31 09:48:33 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 			Object.keys(cases).forEach((name) => { | 
					
						
							|  |  |  | 				it(name, () => { | 
					
						
							|  |  |  | 					const actual = parser.parse(cases[name][0]); | 
					
						
							| 
									
										
										
										
											2016-10-31 09:48:33 +08:00
										 |  |  | 					actual.should.be.eql(cases[name][1]); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-18 22:24:20 +08:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | }); |