astparser.js 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918
  1. //ast节点名称定义
  2. class AstNode {
  3. static File = "File"
  4. static Program = "Program";
  5. static Identifier = "Identifier";
  6. static BlockStatement = "BlockStatement";
  7. static IfStatement = "IfStatement";
  8. static ForStatement = "ForStatement";
  9. static ForInStatement = "ForInStatement";
  10. static ForStatement = "ForOfStatement";
  11. static WhileStatement = "WhileStatement";
  12. static DoWhileStatement = "DoWhileStatement";
  13. static SwitchStatement = "SwitchStatement";
  14. static LabeledStatement = "LabeledStatement";
  15. static ClassDeclaration = "ClassDeclaration";
  16. static ClassBody = "ClassBody";
  17. static ClassMethod = "ClassMethod";
  18. static ClassProperty = "ClassProperty";
  19. static VariableDeclaration = "VariableDeclaration";
  20. static VariableDeclarator = "VariableDeclarator";
  21. static FunctionDeclaration = "FunctionDeclaration";
  22. static ExpressionStatement = "ExpressionStatement";
  23. static ArrowFunctionExpression = "ArrowFunctionExpression";
  24. static FunctionExpression = "FunctionExpression";
  25. static AssignmentExpression = "AssignmentExpression";
  26. static AssignmentPattern = "AssignmentPattern";
  27. static MemberExpression = "MemberExpression";
  28. static ObjectProperty = "ObjectProperty"
  29. static CallExpression = "CallExpression";
  30. static ReturnStatement = "ReturnStatement";
  31. static CatchClause = "CatchClause";
  32. static TryStatement = "TryStatement";
  33. static BinaryExpression = "BinaryExpression";
  34. static ConditionalExpression = "ConditionalExpression";
  35. static LogicalExpression = "LogicalExpression";
  36. static ThisExpression = "ThisExpression";
  37. static TemplateLiteral = "TemplateLiteral";
  38. static NumericLiteral = "NumericLiteral";
  39. static AwaitExpression = "AwaitExpression";
  40. static StringLiteral = "StringLiteral";
  41. static ObjectExpression = "ObjectExpression";
  42. static BooleanLiteral = "BooleanLiteral";
  43. static ArrayExpression = "ArrayExpression";
  44. static NewExpression = "NewExpression";
  45. }
  46. //ast节点辅助处理函数
  47. class AstHelper {
  48. //所有块类型集合定义 if{} for{} while{} switch{} class{} try{} ()=>{} function(){}
  49. static M_BlockStatements = new Map([[AstNode.IfStatement, null], [AstNode.ForStatement, null], [AstNode.ForInStatement, null], [AstNode.ForOfStatement, null], [AstNode.WhileStatement, null],
  50. [AstNode.DoWhileStatement, null], [AstNode.SwitchStatement, null], [AstNode.ClassBody, null], [AstNode.TryStatement, null], [AstNode.FunctionDeclaration, null], [AstNode.ArrowFunctionExpression, null], [AstNode.FunctionExpression, null]]);
  51. //循环块集合定义
  52. static M_LoopBlockStatements = new Map([[AstNode.ForInStatement, null], [AstNode.ForOfStatement, null], [AstNode.ForStatement, null], [AstNode.WhileStatement, null], [AstNode.DoWhileStatement, null]]);
  53. //类成员集合定义
  54. static M_ClassMember = new Map([[AstNode.ClassMethod, null], [AstNode.ClassProperty, null]]);
  55. //函数定义-函数表达式,箭头函数,函数定义
  56. static M_FuncExpression = new Map([[AstNode.FunctionExpression, null], [AstNode.ArrowFunctionExpression, null], [AstNode.FunctionDeclaration, null]]);
  57. //window的变量定义
  58. static M_GlobalVar = new Map([["name", null], ["status", null], ["closed", null], ["length", null], ["origin", null], ["innerWidth", null], ["innerHeight", null], ["scrollX", null], ["pageXOffset", null], ["scrollY", null],
  59. ["pageYOffset", null], ["screenX", null], ["screenY", null], ["outerWidth", null], ["outerHeight", null], ["devicePixelRatio", null], ["event", null], ["screenLeft", null], ["screenTop", null],
  60. ["isSecureContext", null], ["crossOriginIsolated", null], ["originAgentCluster", null], ["credentialless", null]]);
  61. //事的定义方式
  62. static M_Event = new Map([["change", null], ["click", null], ["dblclick", null], ["mousedown", null], ["mouseup", null], ["mouseenter", null], ["mouseout", null], ["mouseleave", null], ["mouseover", null],
  63. ["mousemove", null], ["focus", null], ["keydown", null], ["keyup", null], ["blur", null], ["keypress", null], ["load", null], ["unload", null], ["resize", null], ["scroll", null],
  64. ["drag", null], ["dragstart", null], ["dragend", null], ["dragover", null], ["drop", null], ["error", null], ["wheel", null]]);
  65. //风险代码定义
  66. //static RiskCodes = ["eval","html","Function","outerHTML","innerHTML"];
  67. //获取变量值类型
  68. static AssignmentTypeConvert(curNode, rightNode) {
  69. if (!rightNode) return "";
  70. let assignmentType = rightNode.type;
  71. switch (assignmentType) {
  72. case AstNode.StringLiteral: {
  73. return "string";
  74. }
  75. case AstNode.NumericLiteral: {
  76. return "number";
  77. }
  78. case AstNode.ObjectExpression: {
  79. return "object";
  80. }
  81. case AstNode.ArrowFunctionExpression:
  82. case AstNode.FunctionExpression:
  83. {
  84. return "function";
  85. }
  86. case AstNode.BooleanLiteral: {
  87. return "boolean";
  88. }
  89. case AstNode.ArrayExpression: {
  90. return "array";
  91. }
  92. case AstNode.NewExpression: {
  93. return rightNode.callee?.name;
  94. }
  95. default: {
  96. return "";
  97. }
  98. }
  99. }
  100. //查找指定类型的父级节点
  101. static FindParentNode(node, type, abortNode) {
  102. let curNode = node;
  103. if (node.nodeRel?.[type]) return node.nodeRel[type];
  104. while (curNode) {
  105. if (curNode.root) return null;
  106. if (curNode.type == type) {
  107. //缓存查找到的节点类型
  108. if (!node.nodeRel) node.nodeRel = {};
  109. node.nodeRel[type] = curNode;
  110. return curNode;
  111. }
  112. if (abortNode && abortNode == curNode) return null;
  113. curNode = curNode.parent;
  114. }
  115. return null;
  116. }
  117. //根据多个节点类型遍历父级节点
  118. static FindParentNodeByTypes(node, types, abortNode) {
  119. let curNode = node;
  120. while (curNode) {
  121. if (curNode.root) return null;
  122. //if (types.indexOf(curNode.type) >= 0) return curNode;
  123. if (types.has(curNode.type)) return curNode;
  124. if (abortNode && abortNode == curNode) return null;
  125. curNode = curNode.parent;
  126. }
  127. return null;
  128. }
  129. //根据identifierName查找子节点
  130. static FindSubNodeByIdentifierName(node, identifierName) {
  131. if (!node) return null;
  132. let findNode = null;
  133. let option = {
  134. identifier: [(ast, pName, curNode, parent, des, depth) => {
  135. if (!findNode && curNode.name == identifierName) {
  136. findNode = curNode;
  137. }
  138. }]
  139. }
  140. AstHelper.TraverseNode(node, node, null, option, 0, false);
  141. return findNode;
  142. }
  143. //获取顶层调用节点,如$("#xx").children().get(0).on("click", ...)则获取$("#xx")所在的定义节点
  144. static GetTopCallNode(callNode) {
  145. while (callNode) {
  146. if (callNode.type == AstNode.MemberExpression) {
  147. callNode = callNode.object;
  148. }
  149. else if (callNode.type == AstNode.CallExpression) {
  150. callNode = callNode.callee;
  151. }
  152. if (callNode.type == AstNode.identifier) return callNode.parent;
  153. }
  154. return null;
  155. }
  156. //获取指定节点的层级深度
  157. static GetNodeDepth(node, depth) {
  158. if (!node) return depth;
  159. let keys = Object.keys(node);
  160. let retDepth = 0;
  161. keys.forEach((k) => {
  162. if (node[k] && typeof node[k] == "object"
  163. && k != "parent"
  164. && k != "loc"
  165. && k != "nodeRel"
  166. && k != "errors"
  167. && k != "directives"
  168. && k != "interpreter"
  169. && k != "comments"
  170. && k != "leadingComments"
  171. && k != "trailingComments"
  172. && k != "bufferNodes"
  173. ) {
  174. let curDepth = depth;
  175. if (node[k].type == AstNode.BlockStatement) {
  176. //每增加一层块语句{},则节点层级加一
  177. curDepth = AstHelper.GetNodeDepth(node[k], depth + 1);
  178. }
  179. else {
  180. curDepth = AstHelper.GetNodeDepth(node[k], depth);
  181. }
  182. if (curDepth > retDepth) retDepth = curDepth;
  183. }
  184. });
  185. return (retDepth > depth) ? retDepth : depth;
  186. }
  187. static _count = 0;
  188. //重置分析节点
  189. static ResetAnalyzeNode(ast) {
  190. ast.bufferNodes = {
  191. ProgramNode: [],
  192. ExpressionStatementNode: [],
  193. CallExpressionNode: [],
  194. FunctionExpressionNode: [],
  195. BlockStatementNode: [],
  196. FunctionDeclarationNode: [],
  197. IdentifierNode: [],
  198. ReturnStatementNode: [],
  199. CatchClauseNode: [],
  200. }
  201. }
  202. static TraverseNode(ast, curNode, parent, option, depth, useBuffer) {
  203. //AstHelper._count = 0;
  204. if (useBuffer && ast.bufferNodes) {
  205. AstHelper.TraverseBufferNode(ast, curNode, parent, option, depth);
  206. }
  207. else {
  208. if (useBuffer) AstHelper.ResetAnalyzeNode(ast);
  209. AstHelper.RecursiveNode(ast, curNode, parent, option, depth, useBuffer);
  210. }
  211. }
  212. //从缓存读取节点进行解析
  213. static TraverseBufferNode(ast, curNode, parent, option, depth) {
  214. let bufNodes = ast.bufferNodes;
  215. let nodeProcess = function (func, k, node, ptNode, des, depth) {
  216. if (!func) return;
  217. //if (Array.isArray(func)) {
  218. for (let f of func) f(ast, k, node, ptNode, des, depth);
  219. //}
  220. //else {
  221. // func(ast, k, node, ptNode, des, depth);
  222. //}
  223. }
  224. if (option.program) {
  225. for (pNode of bufNodes.ProgramNode) {
  226. nodeProcess(option.program, pNode.key, pNode.node, pNode.parentNode, pNode.des, pNode.depth);
  227. }
  228. }
  229. if (option.expression) {
  230. for (let pNode of bufNodes.ExpressionStatementNode) {
  231. nodeProcess(option.expression, pNode.key, pNode.node, pNode.parentNode, pNode.des, pNode.depth);
  232. }
  233. }
  234. if (option.callExp) {
  235. for (let pNode of bufNodes.CallExpressionNode) {
  236. nodeProcess(option.callExp, pNode.key, pNode.node, pNode.parentNode, pNode.des, pNode.depth);
  237. }
  238. }
  239. if (option.funcExp) {
  240. for (let pNode of bufNodes.FunctionExpressionNode) {
  241. nodeProcess(option.funcExp, pNode.key, pNode.node, pNode.parentNode, pNode.des, pNode.depth);
  242. }
  243. }
  244. if (option.block) {
  245. for (let pNode of bufNodes.BlockStatementNode) {
  246. nodeProcess(option.block, pNode.key, pNode.node, pNode.parentNode, pNode.des, pNode.depth);
  247. }
  248. }
  249. if (option.funcDec) {
  250. for (let pNode of bufNodes.FunctionDeclarationNode) {
  251. nodeProcess(option.funcDec, pNode.key, pNode.node, pNode.parentNode, pNode.des, pNode.depth);
  252. }
  253. }
  254. if (option.identifier) {
  255. for (let pNode of bufNodes.IdentifierNode) {
  256. nodeProcess(option.identifier, pNode.key, pNode.node, pNode.parentNode, pNode.des, pNode.depth);
  257. }
  258. }
  259. if (option.returnstatement) {
  260. for (let pNode of bufNodes.ReturnStatementNode) {
  261. nodeProcess(option.returnstatement, pNode.key, pNode.node, pNode.parentNode, pNode.des, pNode.depth);
  262. }
  263. }
  264. if (option.catchclause) {
  265. for (let pNode of bufNodes.CatchClauseNode) {
  266. nodeProcess(option.catchclause, pNode.key, pNode.node, pNode.parentNode, pNode.des, pNode.depth);
  267. }
  268. }
  269. }
  270. //遍历节点
  271. static RecursiveNode(ast, curNode, parent, option, depth, useBuffer) {
  272. if (!curNode) return;
  273. if (!depth) depth = 0;
  274. let keys = Object.keys(curNode);
  275. let insertBufNode = function (aryNode, k, node, ptNode, des, depth) {
  276. aryNode.push({ node: node, ast: ast, parentNode: ptNode, des: des, depth: depth, key: k, });
  277. }
  278. keys.forEach((k) => {
  279. let keyNode = curNode[k];
  280. if (keyNode && typeof keyNode == "object"
  281. && k != "parent"
  282. && k != "loc"
  283. && k != "nodeRel"
  284. && k != "errors"
  285. && k != "interpreter"
  286. && k != "comments"
  287. && k != "leadingComments"
  288. && k != "trailingComments"
  289. && k != "bufferNodes"
  290. ) {
  291. let ptNode = curNode;
  292. if (!ptNode.parent) {
  293. ptNode.parent = parent;
  294. //ptNode.propertyName = k;
  295. }
  296. keyNode.parent = ptNode;
  297. if (!keyNode.iid && !Array.isArray(keyNode)) {
  298. keyNode.iid = `n_${AstHelper._count}`;
  299. AstHelper._count = AstHelper._count + 1;
  300. }
  301. switch (keyNode.type) {
  302. case AstNode.Program: {
  303. if (option.program) for (let f of option.program) f(ast, k, keyNode, ptNode, "", depth);
  304. if (useBuffer) insertBufNode(ast.bufferNodes.ProgramNode, k, keyNode, ptNode, "", depth);
  305. break;
  306. }
  307. case AstNode.ExpressionStatement: {
  308. if (option.expression) for (let f of option.expression) f(ast, k, keyNode, ptNode, "", depth);
  309. if (useBuffer) insertBufNode(ast.bufferNodes.ExpressionStatementNode, k, keyNode, ptNode, "", depth);
  310. break;
  311. }
  312. case AstNode.CallExpression: {
  313. if (option.callExp) for (let f of option.callExp) f(ast, k, keyNode, ptNode, "", depth);
  314. if (useBuffer) insertBufNode(ast.bufferNodes.CallExpressionNode, k, keyNode, ptNode, "", depth);
  315. break;
  316. }
  317. case AstNode.FunctionExpression: {
  318. if (option.funcExp) for (let f of option.funcExp) f(ast, k, keyNode, ptNode, "", depth);
  319. if (useBuffer) insertBufNode(ast.bufferNodes.FunctionExpressionNode, k, keyNode, ptNode, "", depth);
  320. break;
  321. }
  322. case AstNode.BlockStatement: {
  323. if (option.block) for (let f of option.block) f(ast, k, keyNode, ptNode, "", depth);
  324. if (useBuffer) insertBufNode(ast.bufferNodes.BlockStatementNode, k, keyNode, ptNode, "", depth);
  325. break;
  326. }
  327. case AstNode.FunctionDeclaration: {
  328. if (option.funcDec) for (let f of option.funcDec) f(ast, k, keyNode, ptNode, "", depth);
  329. if (useBuffer) insertBufNode(ast.bufferNodes.FunctionDeclarationNode, k, keyNode, ptNode, "", depth);
  330. break;
  331. }
  332. case AstNode.Identifier: {
  333. if (option.identifier) for (let f of option.identifier) f(ast, k, keyNode, ptNode, "", depth);
  334. if (useBuffer) insertBufNode(ast.bufferNodes.IdentifierNode, k, keyNode, ptNode, "", depth);
  335. break;
  336. }
  337. case AstNode.ReturnStatement: {
  338. if (option.returnstatement) for (let f of option.returnstatement) f(ast, k, keyNode, ptNode, "", depth);
  339. if (useBuffer) insertBufNode(ast.bufferNodes.ReturnStatementNode, k, keyNode, ptNode, "", depth);
  340. break;
  341. }
  342. case AstNode.CatchClause: {
  343. if (option.catchclause) for (let f of option.catchclause) f(ast, k, keyNode, ptNode, "", depth);
  344. if (useBuffer) insertBufNode(ast.bufferNodes.CatchClauseNode, k, keyNode, ptNode, "", depth);
  345. break;
  346. }
  347. }
  348. //节点深度
  349. AstHelper.RecursiveNode(ast, keyNode, ptNode, option, depth + 1, useBuffer);
  350. }
  351. });
  352. }
  353. //获取父级路径
  354. static GetParentPath(ast, node) {
  355. if (!node) return "";
  356. let path = "";
  357. let pNode = node.parent;
  358. let info;
  359. let curCallLink = "";
  360. while (pNode) {
  361. info = null;
  362. curCallLink = "";
  363. if (pNode.root) break;
  364. if (pNode.type == AstNode.FunctionDeclaration) {
  365. //if (path) path = "/" + path;
  366. //path = pNode.id?.name + path;
  367. info = AstHelper.GetCurNodeCallInfoByFuncDeclaration(ast, pNode, pNode.parent);
  368. if (info) {
  369. curCallLink = info.name + (info.member ? "." + info.member : "");
  370. }
  371. }
  372. else if (pNode.type == AstNode.CallExpression) {
  373. curCallLink = AstHelper.GetCallLinkInfo(pNode, pNode.parent);
  374. }
  375. else if (pNode.type == AstNode.VariableDeclarator) {
  376. info = AstHelper.GetCurNodeCallInfoByVariableDeclarator(ast, pNode, pNode.parent);
  377. if (info) {
  378. curCallLink = info.name + (info.member ? "." + info.member : "");
  379. }
  380. }
  381. else if (pNode.type == AstNode.AssignmentExpression) {
  382. info = AstHelper.GetCurNodeCallInfoByAssigmentExpress(ast, pNode, pNode.parent);
  383. if (info) {
  384. curCallLink = info.name + (info.member ? "." + info.member : "");
  385. }
  386. }
  387. else if (pNode.type == AstNode.ArrowFunctionExpression) {
  388. //info = AstHelper.GetCurNodeCallInfoByArrowFunc(ast, pNode, pNode.parent);
  389. }
  390. else if (pNode.type == AstNode.FunctionExpression) {
  391. info = AstHelper.GetCurNodeCallInfoByFuncExpression(ast, pNode, pNode.parent);
  392. if (info) {
  393. curCallLink = info.name + (info.member ? "." + info.member : "");
  394. }
  395. }
  396. else if (pNode.type == AstNode.ObjectProperty) {
  397. info = AstHelper.GetCurNodeCallInfoByObjectProperty(ast, pNode, pNode.parent);
  398. if (info) {
  399. curCallLink = info.name + (info.member ? "." + info.member : "");
  400. }
  401. }
  402. else if (pNode.type == AstNode.ClassProperty || pNode.type == AstNode.ClassMethod) {
  403. info = AstHelper.GetCurNodeCallInfoByClassMember(ast, pNode, pNode.parent);
  404. if (info) {
  405. curCallLink = info.name + (info.member ? "." + info.member : "");
  406. }
  407. }
  408. else {
  409. if (pNode.type && pNode.type != AstNode.BlockStatement && pNode.type != AstNode.ExpressionStatement && pNode.type != AstNode.IfStatement && pNode.type != AstNode.Program && pNode.type != AstNode.File) {
  410. console.log("获取父级信息找到了未知的节点类型:" + pNode?.type);
  411. }
  412. }
  413. if (curCallLink) {
  414. if (path) path = "/" + path;
  415. path = curCallLink + path;
  416. }
  417. if (pNode.parent?.root) {
  418. pNode = null;
  419. }
  420. else {
  421. if (info) {
  422. pNode = info.node?.parent;
  423. }
  424. else {
  425. pNode = pNode.parent;
  426. }
  427. }
  428. }
  429. return "/" + path;
  430. }
  431. //获取变量节点描述
  432. static GetVarNodeDes(node) {
  433. let des = "";
  434. if (!des) {
  435. node.trailingComments?.forEach((d) => {
  436. if (d.loc.start.line == node.loc.start.line) des = d.value;
  437. });
  438. }
  439. return des;
  440. }
  441. //获取函数节点描述
  442. static GetFuncNodeDes(node) {
  443. let des = "";
  444. if (node) {
  445. node.leadingComments?.forEach((d) => {
  446. des = d.value + des;
  447. //des = des + "\r\n" + x.value;
  448. });
  449. }
  450. return des;
  451. }
  452. //是否在对应的作用域节点内,即pNode是否domainNode的子节点
  453. static IsInDomainNode(pNode, domainNode, abortNode) {
  454. //获取函数定义节点
  455. while (pNode) {
  456. if (pNode == domainNode) return true;
  457. if (pNode.parent == abortNode) return false;
  458. pNode = pNode.parent;
  459. }
  460. return false;
  461. }
  462. //是否在参数节点中定义
  463. static IsInParasNode(pNode, abortNode) {
  464. let args = null;
  465. while (pNode) {
  466. args = pNode.parent?.arguments ?? pNode.parent?.parent?.arguments;
  467. if (args) {
  468. for (let arg of args) {
  469. if (pNode == arg) return true;
  470. }
  471. }
  472. if (pNode == abortNode) return;
  473. pNode = pNode.parent;
  474. }
  475. return false;
  476. }
  477. //是否数组的元素节点,如xx[node]
  478. static IsArrayEleNode(pNode, abortNode) {
  479. while (pNode) {
  480. if (pNode.parent.computed) return true;
  481. if (pNode == abortNode) return;
  482. pNode = pNode.parent;
  483. }
  484. return false;
  485. }
  486. //获取模板字符串的定义信息
  487. static GetTemplateLiteral(templateNode) {
  488. if (!templateNode) return "";
  489. let ret = "";
  490. let quasis = templateNode.quasis;
  491. let express = templateNode.expressions;
  492. for (let i = 0; i <= quasis.length - 1; i++) {
  493. ret = ret + quasis[i].value.raw;
  494. let curExpress = express[i];
  495. switch (curExpress?.type) {
  496. case AstNode.Identifier: {
  497. ret = ret + "${" + curExpress.name + "}"
  498. break;
  499. }
  500. case AstNode.BinaryExpression: {
  501. ret = ret + "${" + AstHelper.GetExpressLinkInfo(curExpress) + "}"
  502. break;
  503. }
  504. case AstNode.CallExpression:
  505. case AstNode.MemberExpression:
  506. {
  507. ret = ret + "${" + AstHelper.GetCallLinkInfo(curExpress) + "}"
  508. break;
  509. }
  510. }
  511. }
  512. return "`" + ret + "`";
  513. }
  514. //获取调用链信息,如a().b().c().d()
  515. static GetCallLinkInfo(callNode, parent) {
  516. //aa().bb().cc().ee("xxx");
  517. /**
  518. * $(f.aa());
  519. $(f.c);
  520. $(f);
  521. $("f");
  522. $("f" + a);
  523. $("f" + a + c());
  524. $("f" + a + c().e());
  525. $("f" + a + c().e().f);
  526. $(a());
  527. $(a().b());
  528. $(a().b().c);
  529. $(a() + "b");
  530. $(a() + b);
  531. $(a().b + b);
  532. $(a().b() + b);
  533. $(a().b.c() + b);
  534. */
  535. if (!callNode) return;
  536. let stackInfo = "";
  537. while (callNode) {
  538. if (callNode.type == AstNode.CallExpression) {
  539. parent = callNode;
  540. callNode = callNode.callee;
  541. }
  542. else if (callNode.type == AstNode.MemberExpression) {
  543. //aa.bb.xx[1].zlPring = {xx:function(){$("aaa")}}
  544. if (parent?.type == AstNode.MemberExpression && parent.computed) {
  545. //数组则不加点
  546. }
  547. else {
  548. if (stackInfo) stackInfo = "." + stackInfo;
  549. }
  550. if (parent?.type == AstNode.CallExpression) {
  551. stackInfo = callNode.property.name + "()" + stackInfo;
  552. }
  553. else {
  554. if (callNode.property.type == AstNode.Identifier) {
  555. if (callNode.computed == true) {
  556. stackInfo = "[" + callNode.property.name + "]" + stackInfo;
  557. }
  558. else {
  559. stackInfo = callNode.property.name + stackInfo;
  560. }
  561. }
  562. else if (callNode.property.type == AstNode.CallExpression) {
  563. if (callNode.computed == true) {
  564. stackInfo = "[" + AstHelper.GetCallLinkInfo(callNode.property) + "]" + stackInfo;
  565. }
  566. else {
  567. stackInfo = AstHelper.GetCallLinkInfo(callNode.property) + stackInfo;
  568. }
  569. }
  570. else {
  571. if (callNode.computed == true) {
  572. stackInfo = "[" + callNode.property.value + "]" + stackInfo;
  573. }
  574. else {
  575. stackInfo = callNode.property.value + stackInfo;
  576. }
  577. }
  578. }
  579. parent = callNode;
  580. callNode = callNode.object;
  581. }
  582. else if (callNode.type == AstNode.BinaryExpression) {
  583. stackInfo = "(" + AstHelper.GetExpressLinkInfo(callNode) + ")" + stackInfo;
  584. callNode = null;
  585. }
  586. else if (callNode.type == AstNode.Identifier) {
  587. if (parent.type == AstNode.MemberExpression && parent.computed == true) {
  588. //成员数组不做处理
  589. }
  590. else {
  591. //避免data[1]中间出现data.[1]
  592. if (stackInfo) stackInfo = "." + stackInfo;
  593. }
  594. if (parent?.type == AstNode.CallExpression) {
  595. stackInfo = callNode.name + "()" + stackInfo;
  596. }
  597. else {
  598. stackInfo = callNode.name + stackInfo;
  599. }
  600. callNode = null;
  601. }
  602. else if (callNode.type == AstNode.ThisExpression) {
  603. if (stackInfo) stackInfo = "." + stackInfo;
  604. stackInfo = "this" + stackInfo;
  605. callNode = null;
  606. }
  607. else {
  608. debugger;
  609. console.error("GetCallLinkInfo中发现未能识别的调用类型:" + callNode.type);
  610. callNode = null;
  611. }
  612. }
  613. return stackInfo;
  614. }
  615. //获取表达式堆栈
  616. static GetExpressLinkInfo(expNode) {
  617. if (!expNode) return "";
  618. let selectorPar = "";
  619. //二元
  620. //$(aa + bb + "ee" + cc + " tbody tr" + dd + "bb").css("");
  621. if (expNode.type == AstNode.BinaryExpression) {
  622. while (expNode) {
  623. if (expNode.right.type == AstNode.Identifier) {
  624. //变量
  625. selectorPar = expNode.operator + expNode.right.name + selectorPar;
  626. }
  627. else if (expNode.right.type == AstNode.CallExpression) {
  628. //函数
  629. selectorPar = expNode.operator + AstHelper.GetCallLinkInfo(expNode.right) + selectorPar;
  630. }
  631. else if (expNode.right.type == AstNode.MemberExpression) {
  632. //成员
  633. selectorPar = expNode.operator + AstHelper.GetCallLinkInfo(expNode.right, expNode) + selectorPar;
  634. }
  635. else if (expNode.right.type == AstNode.TemplateLiteral) {
  636. //模版字符串
  637. selectorPar = AstHelper.GetTemplateLiteral(expNode.right) + selectorPar;
  638. }
  639. else {
  640. //值
  641. if (expNode.right.type == AstNode.NumericLiteral) {
  642. selectorPar = expNode.operator + expNode.right.value + selectorPar;
  643. }
  644. else {
  645. selectorPar = expNode.operator + "'" + expNode.right.value + "'" + selectorPar;
  646. }
  647. }
  648. if (expNode.left.type == AstNode.BinaryExpression) {
  649. expNode = expNode.left;
  650. }
  651. else if (expNode.left.type == AstNode.Identifier) {
  652. selectorPar = expNode.left.name + selectorPar;
  653. expNode = null;
  654. }
  655. else if (expNode.left.type == AstNode.CallExpression) {
  656. selectorPar = AstHelper.GetCallLinkInfo(expNode.left) + selectorPar;
  657. expNode = null;
  658. }
  659. else if (expNode.left.type == AstNode.MemberExpression) {
  660. selectorPar = AstHelper.GetCallLinkInfo(expNode.left, expNode) + selectorPar;
  661. expNode = null;
  662. }
  663. else if (expNode.left.type == AstNode.TemplateLiteral) {
  664. //模版字符串
  665. selectorPar = AstHelper.GetTemplateLiteral(expNode.left) + selectorPar;
  666. expNode = null;
  667. }
  668. else {
  669. selectorPar = "'" + expNode.left.value + "'" + selectorPar;
  670. expNode = null;
  671. }
  672. }
  673. }
  674. //三元
  675. if (!selectorPar && expNode.type == AstNode.ConditionalExpression) {
  676. console.error("GetExpressLinkInfo调用中尚未处理三元表达式");
  677. }
  678. return selectorPar;
  679. }
  680. //获取参数节点的内容信息
  681. static GetParasString(parNode) {
  682. if (!parNode) return {};
  683. let parType = "";
  684. let selectorPar = "";
  685. if (parNode.type == AstNode.CallExpression) {
  686. //参数是函数
  687. selectorPar = AstHelper.GetCallLinkInfo(parNode);
  688. parType = "调用"
  689. }
  690. else if (parNode.type == AstNode.MemberExpression) {
  691. selectorPar = AstHelper.GetCallLinkInfo(parNode);
  692. parType = "成员"
  693. }
  694. else if (parNode.type == AstNode.TemplateLiteral) {
  695. selectorPar = AstHelper.GetTemplateLiteral(parNode);
  696. parType = "模板字符"
  697. }
  698. else if (parNode.type == AstNode.ThisExpression) {
  699. selectorPar = "this";
  700. parType = "变量"
  701. }
  702. else if (parNode.type == AstNode.BinaryExpression) {
  703. selectorPar = AstHelper.GetExpressLinkInfo(parNode);
  704. parType = "表达式"
  705. }
  706. else {
  707. if (parNode.type == AstNode.Identifier) {
  708. //参数是变量
  709. selectorPar = parNode.name;
  710. parType = "变量";
  711. }
  712. else {
  713. //参数是值
  714. //if (parNode.type != AstNode.NumericLiteral) {
  715. selectorPar = "'" + parNode.value + "'";
  716. parType = "值";
  717. //}
  718. //else {
  719. // selectorPar = parNode.value;
  720. // parType = "值";
  721. //}
  722. }
  723. }
  724. return {
  725. parContext: selectorPar,
  726. parType: parType,
  727. }
  728. }
  729. //获取当前节函数定义节点所在的函数调用信息
  730. static GetCurNodeCallInfoByFuncDeclaration(ast, curNode, parent) {
  731. let ret;
  732. let funcNode = curNode;
  733. if (funcNode) {
  734. ret = {
  735. name: funcNode.id.name,
  736. member: "",
  737. kind: "func",
  738. node: funcNode
  739. }
  740. }
  741. return ret;
  742. }
  743. //获取当前所在赋值节点的调用信息
  744. static GetCurNodeCallInfoByAssigmentExpress(ast, curNode, parent) {
  745. let ret;
  746. let onNode = curNode;
  747. if (onNode.left.type == AstNode.MemberExpression) {
  748. let leftCallName = "";
  749. if (onNode.left.object.type == AstNode.MemberExpression || onNode.left.object.type == AstNode.CallExpression) {
  750. leftCallName = AstHelper.GetCallLinkInfo(onNode.left.object);
  751. }
  752. else {
  753. leftCallName = onNode.left.object.name;
  754. }
  755. //xxx.onclick = function(){...}js on 绑定方式
  756. if (onNode.left?.property?.name.indexOf("on") == 0) {
  757. ret = {
  758. name: leftCallName,
  759. member: onNode.left?.property?.name,
  760. kind: "event",
  761. node: onNode,
  762. };
  763. }
  764. else {
  765. //xxx.init = function(){...}
  766. ret = {
  767. name: leftCallName,
  768. member: onNode.left?.property?.name,
  769. kind: "property",
  770. node: onNode,
  771. };
  772. }
  773. }
  774. else {
  775. ret = {
  776. name: onNode.left.name,
  777. member: "",
  778. kind: "variable",
  779. node: onNode,
  780. };
  781. }
  782. return ret;
  783. }
  784. //获取变量节点的调用信息
  785. static GetCurNodeCallInfoByVariableDeclarator(ast, curNode, parent) {
  786. let ret;
  787. ret = {
  788. name: curNode.id.name,
  789. member: "",
  790. kind: "variable",//函数变量
  791. node: curNode,
  792. };
  793. return ret;
  794. }
  795. //获取对象属性节点的调用信息
  796. static GetCurNodeCallInfoByObjectProperty(ast, curNode, parent) {
  797. let ret;
  798. let fNode = AstHelper.FindParentNode(curNode.parent, AstNode.CallExpression);
  799. if (fNode) {
  800. ret = AstHelper.GetCurNodeCallInfoByCallExpression(ast, fNode, fNode.parent);
  801. }
  802. else {
  803. fNode = AstHelper.FindParentNode(curNode.parent, AstNode.AssignmentExpression);
  804. if (fNode) {
  805. ret = AstHelper.GetCurNodeCallInfoByAssigmentExpress(ast, fNode, fNode.parent);
  806. if (ret && parent.parent.parent == fNode && curNode.key.type == AstNode.Identifier) {
  807. if (ret.member) ret.member = ret.member + ".";
  808. ret.member = ret.member + curNode.key.name;
  809. }
  810. }
  811. else {
  812. fNode = AstHelper.FindParentNode(curNode.parent, AstNode.VariableDeclarator);
  813. if (fNode) {
  814. ret = AstHelper.GetCurNodeCallInfoByVariableDeclarator(ast, fNode, fNode.parent);
  815. }
  816. else {
  817. console.error("GetCurNodeCallInfoByObjectProperty调用中未能识别节点类型:" + curNode.type);
  818. }
  819. }
  820. }
  821. return ret;
  822. }
  823. //获取当前节点所在函数表达式的调用
  824. //let xx = function(){...}
  825. //let xx.test = function(){...}
  826. static GetCurNodeCallInfoByFuncExpression(ast, curNode, parent) {
  827. let ret;
  828. let expression = curNode;
  829. let expParent = expression.parent;
  830. if (Array.isArray(expParent)) expParent = expression.parent.parent;
  831. if (expression) {
  832. if (expParent.type == AstNode.VariableDeclarator) {
  833. //通过变量定义的函数中使用
  834. //xx = function(){ ... }
  835. ret = AstHelper.GetCurNodeCallInfoByVariableDeclarator(ast, expParent, expParent.parent);
  836. }
  837. else if (expParent.type == AstNode.AssignmentExpression) {
  838. //xx.test = function(){...}
  839. ret = AstHelper.GetCurNodeCallInfoByAssigmentExpress(ast, expParent, expParent.parent);
  840. }
  841. else if (expParent.type == AstNode.CallExpression) {
  842. //test("",function(){...})
  843. ret = AstHelper.GetCurNodeCallInfoByCallExpression(ast, expParent, expParent.parent);
  844. }
  845. else if (expParent.type == AstNode.ObjectProperty) {
  846. //$.ajax({url:"",error:function(){...}})
  847. ret = AstHelper.GetCurNodeCallInfoByObjectProperty(ast, expParent, expParent.parent);
  848. }
  849. else {
  850. //(function () {... })
  851. ret = {
  852. name: "()",
  853. member: "",
  854. kind: "other",
  855. node: expParent,
  856. }
  857. }
  858. }
  859. return ret;
  860. }
  861. //获取调用前缀
  862. static GetCallPrePath(node) {
  863. let curNode = node;
  864. let path = "";
  865. if (node.type == AstNode.Identifier) {
  866. return node.name
  867. }
  868. else {
  869. if (node.type == AstNode.CallExpression) {
  870. curNode = curNode.callee;
  871. }
  872. else {
  873. curNode = curNode.object;
  874. }
  875. while (curNode) {
  876. if (curNode.property) {
  877. if (curNode.property.type == AstNode.Identifier) {
  878. if (path) path = "/" + path;
  879. path = curNode.property.name + path;
  880. }
  881. }
  882. if (curNode.type == AstNode.Identifier) {
  883. if (path) path = "/" + path;
  884. path = curNode.name + path;
  885. curNode = null;
  886. }
  887. else {
  888. if (curNode.type == AstNode.CallExpression) {
  889. curNode = curNode.callee;
  890. }
  891. else {
  892. curNode = curNode.object;
  893. }
  894. }
  895. }
  896. }
  897. return path;
  898. }
  899. //获取当前节点所在的函数参数中的调用
  900. //test("",function(){...})
  901. //xx.test("",function(){...})
  902. //$("body").on("click", "xxx", function (e) {...})
  903. //ele.addEventListener("click", function(){...})
  904. static GetCurNodeCallInfoByCallExpression(ast, curNode, parent) {
  905. let ret;
  906. let callNode = curNode;
  907. if (callNode.callee?.type == AstNode.MemberExpression) {
  908. //$("body").on("click", "xxx", function (e) {xxx();});
  909. let leftName = "";
  910. if (callNode.callee.object?.type == AstNode.MemberExpression || callNode.callee.object?.type == AstNode.CallExpression) {
  911. leftName = AstHelper.GetCallLinkInfo(callNode.callee.object, callNode.callee);
  912. }
  913. else {
  914. leftName = callNode.callee?.object?.name;
  915. }
  916. if (callNode.callee.property?.name == "addEventListener") {
  917. //通过addEventListener方式绑定的事件
  918. ret = {
  919. name: callNode.callee?.object?.name,
  920. member: callNode.arguments[0].value,
  921. kind: "event",
  922. node: callNode,
  923. };
  924. }
  925. else if (callNode.callee.property?.name == "on") {
  926. //通过jquery方式绑定$("xxx").on("click",function(){});
  927. //$("xxx").on("click",".filter",function(){});
  928. let jNode = AstHelper.FindSubNodeByIdentifierName(callNode, "$");
  929. let argsNode = null;
  930. if (jNode) {
  931. argsNode = jNode.parent.arguments[0];
  932. }
  933. else {
  934. argsNode = callNode.callee?.object.arguments[0];
  935. }
  936. let curParContext = "";
  937. if (argsNode) {
  938. curParContext = AstHelper.GetParasString(argsNode);
  939. if (curParContext) curParContext = curParContext.parContext;
  940. }
  941. if (jNode) {
  942. leftName = leftName.replace("$()", `$(${curParContext})`);
  943. }
  944. ret = {
  945. name: leftName,
  946. member: callNode.arguments[0].value,
  947. kind: "event",
  948. node: callNode,
  949. };
  950. if (callNode.arguments.length >= 3) {
  951. ret.ext = {
  952. filter: callNode.arguments[1].value,
  953. }
  954. }
  955. }
  956. else if (AstHelper.M_Event.has(callNode.callee.property?.name)) {
  957. let jNode = AstHelper.FindSubNodeByIdentifierName(callNode, "$");
  958. let argsNode = null;
  959. let curParContext = "";
  960. if (jNode) {
  961. argsNode = jNode.parent.arguments[0];
  962. if (argsNode) {
  963. curParContext = AstHelper.GetParasString(argsNode);
  964. if (curParContext) curParContext = curParContext.parContext;
  965. leftName = leftName.replace("$()", `$(${curParContext})`);
  966. }
  967. }
  968. ret = {
  969. name: leftName,
  970. member: callNode.callee.property?.name,
  971. kind: "event",
  972. node: callNode,
  973. };
  974. }
  975. else {
  976. //tbody.children().eq(indexSelRow).addClass("choose_row");
  977. ret = {
  978. name: leftName,
  979. member: callNode.callee.property?.name,
  980. kind: "func",
  981. node: callNode,
  982. };
  983. }
  984. }
  985. else {
  986. if (callNode.callee?.name == "$") {
  987. ret = {
  988. name: callNode.callee?.name ?? "",
  989. member: "",
  990. kind: "event",
  991. node: callNode,
  992. };
  993. }
  994. else {
  995. let callName = callNode.callee?.name ?? "";
  996. if (!callName) {
  997. callName = "(自执行)()";
  998. }
  999. ret = {
  1000. name: callName,
  1001. member: "",
  1002. kind: "immediately",
  1003. node: callNode,
  1004. };
  1005. }
  1006. }
  1007. return ret;
  1008. }
  1009. //获取当前节点所在箭头函数的调用
  1010. //let xx = ()=>{...}
  1011. //let ele.onxxx = ()=>{...}
  1012. //let ele.addEventListener("click",()=>{...})
  1013. //xx = ()=>{...}
  1014. //()=>{...}
  1015. static GetCurNodeCallInfoByArrowFunc(ast, curNode, parent) {
  1016. let ret;
  1017. let arrowNode = curNode;
  1018. let aryParent = arrowNode.parent;
  1019. //如果是数组,则继续取上级节点
  1020. if (Array.isArray(aryParent)) aryParent = aryParent.parent;
  1021. if (aryParent?.type == AstNode.VariableDeclarator) {
  1022. //let xx = ()=>{...}
  1023. ret = AstHelper.GetCurNodeCallInfoByVariableDeclarator(ast, aryParent, aryParent.parent);
  1024. }
  1025. else if (aryParent?.type == AstNode.AssignmentExpression) {
  1026. //xx.test = ()=>{...}
  1027. ret = AstHelper.GetCurNodeCallInfoByAssigmentExpress(ast, aryParent, aryParent.parent);
  1028. }
  1029. else if (aryParent?.type == AstNode.CallExpression) {
  1030. //test("",()=>{gTest = "bbb";});
  1031. ret = AstHelper.GetCurNodeCallInfoByCallExpression(ast, aryParent, aryParent.parent);
  1032. }
  1033. else if (aryParent?.type == AstNode.MemberExpression) {
  1034. //xx.test[()=>{...}]="aaa"
  1035. let memberNode = aryParent;
  1036. if (memberNode.object?.type == AstNode.Identifier) {
  1037. ret = {
  1038. name: memberNode.object.name,
  1039. member: "",
  1040. kind: "array",
  1041. node: memberNode,
  1042. };
  1043. }
  1044. else {
  1045. ret = {
  1046. name: memberNode.object.object.name,
  1047. member: memberNode.object.property.name,
  1048. kind: "property",
  1049. node: memberNode,
  1050. };
  1051. }
  1052. }
  1053. else {
  1054. ret = {
  1055. name: "=>",
  1056. member: "",
  1057. kind: "arrow",
  1058. node: arrowNode,
  1059. };
  1060. }
  1061. return ret;
  1062. }
  1063. //获取当前节点所在的类方法的调用信息
  1064. static GetCurNodeCallInfoByClassMember(ast, curNode, parent) {
  1065. let ret;
  1066. let methodNode = curNode;
  1067. if (methodNode) {
  1068. ret = {
  1069. name: methodNode.parent.parent.parent.id.name,
  1070. member: methodNode.key.name,
  1071. kind: "class",
  1072. node: methodNode
  1073. }
  1074. }
  1075. return ret;
  1076. }
  1077. //获取节点的调用关系
  1078. static GetNodeCallRel(ast, curNode, parent) {
  1079. //查找类
  1080. //查找函数定义
  1081. //查找函数表达式
  1082. //查找箭头表达式
  1083. //查找调用表达式
  1084. //查找赋值表达式
  1085. let callInfo;
  1086. let identifierName = curNode.name;
  1087. let findNode = AstHelper.FindParentNode(parent, AstNode.ClassDeclaration);
  1088. if (findNode) {
  1089. let memberNode = AstHelper.FindParentNodeByTypes(parent, AstHelper.M_ClassMember);
  1090. if (memberNode) {
  1091. //类成员中使用包括方法和属性
  1092. callInfo = AstHelper.GetCurNodeCallInfoByClassMember(ast, memberNode, memberNode.parent);
  1093. }
  1094. }
  1095. else {
  1096. findNode = AstHelper.FindParentNode(parent, AstNode.FunctionDeclaration);
  1097. if (findNode) {
  1098. //找到了调用的所在函数定义
  1099. //callInfo = AstHelper.GetCurNodeCallInfoByFuncDeclaration(ast, findNode, findNode.parent);
  1100. let isParVar = false;
  1101. if (identifierName) {
  1102. //当前identifier定义如果属于FunctionDeclaration参数则退出,如果是变量的identifier,在函数的参数中表示变量定义而不是引用
  1103. //如果是callexpression,则不可能出现在函数定义的参数中
  1104. findNode.params.forEach((x) => {
  1105. if (x == curNode) isParVar = true;
  1106. if (x.type == AstNode.Identifier && x.name == identifierName) isParVar = true;
  1107. if (x.type == AstNode.AssignmentPattern && x.left.name == identifierName) isParVar = true;
  1108. });
  1109. }
  1110. if (!isParVar) {
  1111. callInfo = AstHelper.GetCurNodeCallInfoByFuncDeclaration(ast, findNode, findNode.parent);
  1112. }
  1113. }
  1114. else {
  1115. findNode = AstHelper.FindParentNode(parent, AstNode.FunctionExpression);
  1116. if (findNode) {
  1117. if (findNode.parent.parent?.type == AstNode.CallExpression) {
  1118. //作为参数形式调用,如test("", function(){vField1 = "222"})
  1119. //$(function(){...});
  1120. callInfo = AstHelper.GetCurNodeCallInfoByCallExpression(ast, findNode.parent.parent, findNode.parent.parent.parent);
  1121. }
  1122. else {
  1123. callInfo = AstHelper.GetCurNodeCallInfoByFuncExpression(ast, findNode, findNode.parent);
  1124. }
  1125. }
  1126. else {
  1127. //查找箭头函数
  1128. findNode = AstHelper.FindParentNode(parent, AstNode.ArrowFunctionExpression);
  1129. if (findNode) {
  1130. if (findNode.parent.parent?.type == AstNode.CallExpression) {
  1131. callInfo = AstHelper.GetCurNodeCallInfoByCallExpression(ast, findNode.parent.parent, findNode.parent.parent.parent);
  1132. }
  1133. else {
  1134. callInfo = AstHelper.GetCurNodeCallInfoByArrowFunc(ast, findNode, findNode.parent);
  1135. }
  1136. }
  1137. else {
  1138. //查找调用函数
  1139. findNode = AstHelper.FindParentNode(parent, AstNode.CallExpression);
  1140. if (findNode) {
  1141. callInfo = AstHelper.GetCurNodeCallInfoByCallExpression(ast, findNode, findNode.parent);
  1142. }
  1143. else {
  1144. findNode = AstHelper.FindParentNode(parent, AstNode.AssignmentExpression);
  1145. if (findNode) {
  1146. callInfo = AstHelper.GetCurNodeCallInfoByAssigmentExpress(ast, findNode, findNode.parent);
  1147. }
  1148. else {
  1149. //直接在脚本中调用
  1150. callInfo = {
  1151. name: "",
  1152. kind: "immediately",
  1153. start: curNode.start,
  1154. end: curNode.end,
  1155. node: curNode,
  1156. }
  1157. }
  1158. }
  1159. }
  1160. }
  1161. }
  1162. }
  1163. return callInfo;
  1164. }
  1165. }
  1166. //函数解析插件
  1167. class AstParserPlugin {
  1168. _parser;
  1169. _option;
  1170. RegInfo = {
  1171. Parser: "",//配置解析挂接的ast节点类型,AstNode中所定义的节点
  1172. CallRelation: "",//配置调用关系解析挂接的ast节点类型,AstNode中所定义的节点
  1173. }
  1174. constructor(parser, option) {
  1175. this._parser = parser;
  1176. this._option = option;
  1177. }
  1178. //Parser(ast, pName, curNode, parent, des, depth){ }//配置对应节点的解析处理逻辑
  1179. //Checked(){}//配置解析对象的清理和二次分析处理
  1180. //CallRelation(){}//配置对应的调用关系逻辑
  1181. get Result() {
  1182. return {};
  1183. }
  1184. }
  1185. class AstParserPlugin_Func extends AstParserPlugin {
  1186. RegInfo = {
  1187. Parse: AstNode.FunctionDeclaration,
  1188. CallRelation: AstNode.CallExpression,
  1189. }
  1190. _funcList = [];
  1191. constructor(parser, option) {
  1192. super(parser, option);
  1193. }
  1194. #WriteCallRel(ast, curNode, funcDefine, callInfo) {
  1195. if (funcDefine && callInfo) {
  1196. let called = funcDefine.called.find((c) => c.file == ast.file && c.name == callInfo.name && c.member == callInfo.member && c.kind == callInfo.kind);
  1197. if (called) {
  1198. //找到相同的调用
  1199. called.pos.push({
  1200. start: curNode.start,
  1201. end: curNode.end
  1202. })
  1203. }
  1204. else {
  1205. //在相同的对象中
  1206. funcDefine.called.push({
  1207. file: ast.file,
  1208. name: callInfo.name,
  1209. member: callInfo.member,
  1210. kind: callInfo.kind,
  1211. start: callInfo.node.start,
  1212. end: callInfo.node.end,
  1213. ext: callInfo.ext,
  1214. pos: [{ start: curNode.start, end: curNode.end }],
  1215. path: AstHelper.GetParentPath(ast, callInfo.node),//查询引用的父级信息
  1216. });
  1217. }
  1218. }
  1219. }
  1220. #GetFuncCodeAnalyze(ast, funcNode) {
  1221. //风险类别动态js代码执行,
  1222. //ele.innerHTML,ele.outerHTML, $("xxx").html(),eval(...),new Function()
  1223. let riskType = [];
  1224. let isIgnoreCatch = false;
  1225. let isHtmlModify = false;
  1226. let isLoopHtmlModify = false;
  1227. let option = {
  1228. identifier: [function (ast, pName, curNode, parent, des, depth) {
  1229. //if (AstHelper.RiskCodes.indexOf(curNode.name) >= 0) {
  1230. switch (curNode.name) {
  1231. case "Function": {
  1232. if (parent.type == "NewExpression") {
  1233. if (riskType.indexOf(curNode.name) < 0) riskType.push(curNode.name);
  1234. }
  1235. break;
  1236. }
  1237. case "eval": {
  1238. if (parent.type == "CallExpression") {
  1239. if (riskType.indexOf(curNode.name) < 0) riskType.push(curNode.name);
  1240. }
  1241. break;
  1242. }
  1243. case "append":
  1244. case "appendTo":
  1245. case "prepend":
  1246. case "prependTo":
  1247. case "after":
  1248. case "insertAfter":
  1249. case "before":
  1250. case "insertBefore":
  1251. case "html":
  1252. {
  1253. if (pName == "property") {
  1254. let pre = AstHelper.GetCallPrePath(parent);
  1255. if (pre.substring(0, 1) == "$") {
  1256. isHtmlModify = true;
  1257. //判断父级是否为循环语句
  1258. if (!isLoopHtmlModify) {
  1259. let loopNode = AstHelper.FindParentNodeByTypes(parent, AstHelper.M_LoopBlockStatements, funcNode);
  1260. if (loopNode) {
  1261. isLoopHtmlModify = true;
  1262. }
  1263. }
  1264. }
  1265. }
  1266. break;
  1267. }
  1268. case "innerHTML":
  1269. case "outerHTML":
  1270. {
  1271. if (pName == "property" && parent.type == AstNode.MemberExpression) {
  1272. isHtmlModify = true;
  1273. //判断父级是否为循环语句
  1274. if (!isLoopHtmlModify) {
  1275. let loopNode = AstHelper.FindParentNodeByTypes(parent, AstHelper.M_LoopBlockStatements, funcNode);
  1276. if (loopNode) {
  1277. isLoopHtmlModify = true;
  1278. }
  1279. }
  1280. }
  1281. break;
  1282. }
  1283. }
  1284. //}
  1285. }],
  1286. catchclause: [function (ast, pName, curNode, parent, des, depth) {
  1287. if (curNode.body) {
  1288. if (curNode.body.body.length <= 0) {
  1289. //let fNode = AstHelper.FindParentNodeByTypes(parent, [AstNode.FunctionExpression, AstNode.ArrowFunctionExpression, AstNode.CallExpression, AstNode.FunctionDeclaration], funcNode);
  1290. let fNode = AstHelper.FindParentNode(parent, AstNode.FunctionDeclaration, funcNode);
  1291. if (fNode == funcNode) isIgnoreCatch = true;
  1292. }
  1293. }
  1294. }]
  1295. /*
  1296. returnstatement: function (ast, pName, curNode, parent, des, depth) {
  1297. let pBlock = AstHelper.FindParentNodeByTypes(parent, [AstNode.FunctionExpression, AstNode.CallExpression, AstNode.ArrowFunctionExpression, AstNode.FunctionDeclaration]);
  1298. if (pBlock && pBlock != funcNode) {
  1299. //
  1300. }
  1301. else {
  1302. //
  1303. }
  1304. }*/
  1305. }
  1306. AstHelper.TraverseNode(ast, funcNode, null, option, false);
  1307. return {
  1308. risk: riskType,//是否有代码注入风险即动态js脚本执行,riskType则是js脚本执行方式,eval,或function
  1309. isIgnoreCatch: isIgnoreCatch,//是否有异常被忽略
  1310. isHtmlModify: isHtmlModify,//是否有HTML修改
  1311. isLoopHtmlModify: isLoopHtmlModify,//是否在循环中操作HTML
  1312. }
  1313. }
  1314. #FuncBaseCheck(funcList) {
  1315. let _this = this;
  1316. funcList.forEach((fItem) => {
  1317. //位置和所在文件名不同,则可能存在名称冲突
  1318. let otherDefine = funcList.find((x) => x.name == fItem.name && (x.start != fItem.start || x.file != fItem.file));
  1319. fItem.isConflict = false;
  1320. if (otherDefine) {
  1321. fItem.isConflict = true;
  1322. }
  1323. //判断是否空的函数定义
  1324. fItem.isNullFunc = false;
  1325. if (fItem.defineNode.body.body?.length <= 0) {
  1326. fItem.isNullFunc = true;
  1327. }
  1328. //获取函数中代码的最大深度
  1329. let depth = AstHelper.GetNodeDepth(fItem.defineNode, 0);
  1330. if (depth > 20) {
  1331. fItem.codeDepth = ">20";
  1332. }
  1333. else if (depth > 15) {
  1334. fItem.codeDepth = ">15";
  1335. }
  1336. else if (depth > 10) {
  1337. fItem.codeDepth = ">10";
  1338. }
  1339. else if (depth > 5) {
  1340. fItem.codeDepth = ">5";
  1341. }
  1342. else {
  1343. fItem.codeDepth = "";
  1344. }
  1345. let codeAnalyze = _this.#GetFuncCodeAnalyze(fItem.ast, fItem.defineNode);
  1346. fItem.risk = codeAnalyze?.risk;
  1347. fItem.isIgnoreCatch = codeAnalyze?.isIgnoreCatch;
  1348. fItem.isHtmlModify = codeAnalyze?.isHtmlModify;
  1349. fItem.isLoopHtmlModify = codeAnalyze?.isLoopHtmlModify;
  1350. });
  1351. }
  1352. //解析节点
  1353. Parse(ast, pName, curNode, parent, des, depth) {
  1354. let id = curNode.id;
  1355. if (id.type == AstNode.Identifier) {
  1356. let des = AstHelper.GetFuncNodeDes(curNode);
  1357. //判断是否在对应的块下面定义的函数
  1358. //如function Init(){ function getData(){...} },getData则表示内部子函数
  1359. let isGlobal = true;
  1360. let blockNode = AstHelper.FindParentNode(parent, AstNode.BlockStatement);
  1361. if (blockNode) {
  1362. isGlobal = AstHelper.FindParentNodeByTypes(blockNode, AstHelper.M_BlockStatements) ? false : true;
  1363. }
  1364. this._funcList.push({
  1365. file: ast.file,
  1366. range: isGlobal ? "global" : "sub",
  1367. class: "",
  1368. isAsync: curNode.async,
  1369. name: id.name,
  1370. des: des,
  1371. start: curNode.start,
  1372. end: curNode.end,
  1373. ast: ast,
  1374. defineNode: curNode,
  1375. idNode: curNode.id,
  1376. called: [],//保存被那些地方调用
  1377. //refers:[],//保存引用了那些函数
  1378. path: AstHelper.GetParentPath(ast, curNode),
  1379. //sameName:[],
  1380. });
  1381. }
  1382. }
  1383. //检查及分析节点
  1384. Checked() {
  1385. //函数基本检查,命名冲突,是否空函数等
  1386. this.#FuncBaseCheck(this._funcList);
  1387. }
  1388. //关系分析
  1389. CallRelation(ast, pName, curNode, parent, des, depth) {
  1390. let callee = curNode.callee;
  1391. let funcDefine;
  1392. let callInfo;
  1393. if (callee.type == AstNode.Identifier) {
  1394. //是一个函数调用,如xx();
  1395. funcDefine = this._funcList.find((x) => x.name == callee.name);
  1396. if (funcDefine) {
  1397. callInfo = AstHelper.GetNodeCallRel(ast, curNode, parent);
  1398. //判断是否存在嵌套使用
  1399. let pFuncDesc = AstHelper.FindParentNode(parent, AstNode.FunctionDeclaration, funcDefine.parent);
  1400. if (funcDefine.isNested != true) {
  1401. if (pFuncDesc && pFuncDesc.id?.name == funcDefine.name) {
  1402. funcDefine.isNested = true;
  1403. }
  1404. else {
  1405. funcDefine.isNested = false;
  1406. }
  1407. }
  1408. this.#WriteCallRel(ast, curNode, funcDefine, callInfo);
  1409. }
  1410. else {
  1411. //根据调用参数名称去匹配
  1412. if (curNode.arguments?.length > 0) {
  1413. curNode.arguments.forEach((arg) => {
  1414. funcDefine = this._funcList.find((x) => x.name == arg.name);
  1415. if (funcDefine) {
  1416. callInfo = AstHelper.GetCurNodeCallInfoByCallExpression(ast, curNode, parent);
  1417. this.#WriteCallRel(ast, arg, funcDefine, callInfo);
  1418. }
  1419. });
  1420. }
  1421. }
  1422. }
  1423. else {
  1424. //根据参数去匹配名称
  1425. if (callee.arguments?.length > 0) {
  1426. callee.arguments.forEach((arg) => {
  1427. funcDefine = this._funcList.find((x) => x.name == arg.name);
  1428. if (funcDefine) {
  1429. callInfo = AstHelper.GetCurNodeCallInfoByCallExpression(ast, curNode, parent);
  1430. this.#WriteCallRel(ast, arg, funcDefine, callInfo);
  1431. }
  1432. });
  1433. }
  1434. }
  1435. }
  1436. //结果获取
  1437. get Result() {
  1438. return {
  1439. AllFuncList: this._funcList,
  1440. FuncList: function (fileName) {
  1441. return this.AllFuncList.filter((x) => x.file == fileName);
  1442. },
  1443. }
  1444. }
  1445. }
  1446. window.AstParserPlugin_Func = AstParserPlugin_Func;
  1447. class AstParserPlugin_GlobalVar extends AstParserPlugin {
  1448. RegInfo = {
  1449. Parse: AstNode.Identifier,
  1450. CallRelation: AstNode.Identifier,
  1451. };
  1452. _globalVarList = [];
  1453. _tempVars = [];
  1454. _assignNode = [];
  1455. constructor(parser, option) {
  1456. super(parser, option)
  1457. }
  1458. //检查变量名称是否存在冲突
  1459. #VarBaseCheck(globalVars, tempVars) {
  1460. //检查全局变量名是否存在冲突或重复定义
  1461. globalVars.forEach((gVar) => {
  1462. let varDefine = tempVars.find((cVar) => cVar.varName == gVar.name && cVar.defineNode != gVar.defineNode);
  1463. gVar.isConflict = false;
  1464. if (varDefine) {
  1465. //存在命名冲突
  1466. gVar.isConflict = true;
  1467. }
  1468. let globalDefine = globalVars.find((cVar) => cVar.name == gVar.name && cVar.defineNode != gVar.defineNode);
  1469. gVar.isRepetDefine = false;
  1470. if (globalDefine) {
  1471. //存在重复定义
  1472. gVar.isRepetDefine = true;
  1473. }
  1474. });
  1475. }
  1476. //重新梳理全局变量
  1477. #CullGlobalVar(ret, vars, assignNode) {
  1478. if (assignNode.length > 0) {
  1479. assignNode.forEach((n) => {
  1480. let varNode = vars.filter(x => x.varName == n.node.name);
  1481. let isSameDomain = false;//判断复制表达式是否落在变量定义所在的范围内,是否相同作用域,如果是则不是全局变量
  1482. varNode.some((v) => {
  1483. if (AstHelper.IsInDomainNode(n.parent, v.domainNode)) {
  1484. //变量使用与变量定义的作用域相同
  1485. isSameDomain = true;
  1486. return true;
  1487. }
  1488. });
  1489. if (!isSameDomain) {
  1490. //没有找到定义或作用范围不同
  1491. let gVarDefine = ret.find(x => x.name == n.node.name);
  1492. if (gVarDefine) {
  1493. //找到了全局变量的定义
  1494. let assignmentType = AstHelper.AssignmentTypeConvert(n.parent, n.parent.right);
  1495. if (assignmentType) {
  1496. if (gVarDefine.types.indexOf(assignmentType) < 0) gVarDefine.types.push(assignmentType);
  1497. }
  1498. }
  1499. else {
  1500. let expressionStatement = AstHelper.FindParentNode(n.parent, AstNode.ExpressionStatement);
  1501. let defTypes = [];
  1502. let assignmentType = AstHelper.AssignmentTypeConvert(n.parent, n.parent.right);
  1503. if (assignmentType) {
  1504. defTypes.push(assignmentType);
  1505. }
  1506. ret.push(
  1507. {
  1508. file: n.file,
  1509. name: n.node.name,
  1510. defWay: "",
  1511. des: expressionStatement ? AstHelper.GetVarNodeDes(expressionStatement) : "",
  1512. start: n.parent.start,
  1513. end: n.parent.end,
  1514. defineNode: n.node,
  1515. idNode: n.idNode,
  1516. types: defTypes,
  1517. called: [],
  1518. }
  1519. );
  1520. }
  1521. }
  1522. });
  1523. }
  1524. //查找没有使用申明方式定义变量的元素
  1525. //let notDeclareds = ret.filter((x) => !x.defWay);
  1526. for (let i = ret.length - 1; i >= 0; i--) {
  1527. //该变量是否在其他文件中有定义
  1528. let unDefVar = ret[i];
  1529. if (!unDefVar.defWay) {
  1530. //没有申明定义方式
  1531. let findDef = ret.find((x) => x.file != unDefVar.file && x.defWay);
  1532. if (findDef) {
  1533. //当在其他文件中有定义类别时,则移除当前的定义
  1534. ret.splice(i, 1);
  1535. }
  1536. }
  1537. }
  1538. }
  1539. Parse(ast, pName, curNode, parent, des, depth) {
  1540. //对象处于函数参数的定义中,如function xx(p1,p2){}
  1541. if (parent == parent.parent?.params) {
  1542. //参数变量
  1543. this._tempVars.push({
  1544. file: ast.file,
  1545. varName: curNode.name,
  1546. defineNode: curNode,
  1547. domainNode: parent.parent,
  1548. });
  1549. return;
  1550. }
  1551. else if (parent.parent == parent.parent.parent.params) {
  1552. //示例:function test(isFailed = false){}
  1553. this._tempVars.push({
  1554. file: ast.file,
  1555. varName: curNode.name,
  1556. defineNode: curNode,
  1557. domainNode: parent.parent.parent
  1558. });
  1559. return;
  1560. }
  1561. //对象处于函数调用的arguments中,如xx(p1,p2);
  1562. if (parent == parent.parent?.arguments) return;
  1563. if (parent?.type == AstNode.MemberExpression) {
  1564. //如果节点类型为成员表达式,则判断是否为window成员
  1565. //window.xx = "";
  1566. //globalThis.xx="";
  1567. //if (window.getSelection){}//IfStatement
  1568. if (pName == "property" && parent.parent?.type == AstNode.AssignmentExpression && (parent.object.name == "window" || parent.object.name == "globalThis")) {
  1569. if (AstHelper.M_GlobalVar.has(curNode.name)) return;
  1570. //如果已经存在相同的windows变量,则直接退出
  1571. let fWindowVarNode = this._globalVarList.find((x) => x.name == curNode.name);
  1572. if (fWindowVarNode) return;
  1573. let defTypes = [];
  1574. let assignmentType = AstHelper.AssignmentTypeConvert(parent.parent, parent.parent.right);
  1575. if (assignmentType) {
  1576. defTypes.push(assignmentType);
  1577. }
  1578. this._globalVarList.push(
  1579. {
  1580. file: ast.file,
  1581. name: curNode.name,
  1582. defWay: parent.object.name,
  1583. des: "",
  1584. start: parent.start,
  1585. end: parent.end,
  1586. ast: ast,
  1587. defineNode: curNode,
  1588. idNode: curNode,
  1589. types: defTypes,
  1590. called: [],
  1591. }
  1592. );
  1593. }
  1594. }
  1595. else if (parent?.type == AstNode.AssignmentExpression) {
  1596. //a=6;
  1597. //let varDeclaration = findParentNode(parent);
  1598. if (pName == "right") return;
  1599. this._assignNode.push({
  1600. file: ast.file,
  1601. node: curNode,
  1602. parent: parent,
  1603. idNode: curNode,
  1604. });
  1605. }
  1606. else if (parent?.type == AstNode.VariableDeclarator) {
  1607. if (pName == "init") return; //var xx = 12;
  1608. let blockNode = AstHelper.FindParentNode(parent, AstNode.BlockStatement);
  1609. if (blockNode) {
  1610. let findNode = AstHelper.FindParentNodeByTypes(blockNode, AstHelper.M_BlockStatements);
  1611. if (findNode) {
  1612. this._tempVars.push({
  1613. file: ast.file,
  1614. varName: curNode.name,
  1615. defineNode: curNode,
  1616. domainNode: findNode,
  1617. });
  1618. }
  1619. else {
  1620. let varDesNode = AstHelper.FindParentNode(parent, AstNode.VariableDeclaration);
  1621. let des = "";
  1622. if (varDesNode) des = AstHelper.GetVarNodeDes(varDesNode);
  1623. let defTypes = [];
  1624. let assignmentType = AstHelper.AssignmentTypeConvert(parent, parent.init);
  1625. if (assignmentType) {
  1626. defTypes.push(assignmentType);
  1627. }
  1628. this._globalVarList.push(
  1629. {
  1630. file: ast.file,
  1631. name: curNode.name,
  1632. defWay: varDesNode.kind,
  1633. des: des,
  1634. start: parent.start,
  1635. end: parent.end,
  1636. ast: ast,
  1637. defineNode: curNode,
  1638. idNode: curNode,
  1639. types: defTypes,
  1640. called: [],
  1641. }
  1642. );
  1643. }
  1644. }
  1645. else {
  1646. let varDesNode = AstHelper.FindParentNode(parent, AstNode.VariableDeclaration);
  1647. let des = "";
  1648. if (varDesNode) des = AstHelper.GetVarNodeDes(varDesNode);
  1649. let defTypes = [];
  1650. let assignmentType = AstHelper.AssignmentTypeConvert(parent, parent.init);
  1651. if (assignmentType) {
  1652. defTypes.push(assignmentType);
  1653. }
  1654. //不在block块中的语句
  1655. this._globalVarList.push(
  1656. {
  1657. file: ast.file,
  1658. name: curNode.name,
  1659. defWay: varDesNode.kind,
  1660. des: des,
  1661. start: parent.start,
  1662. end: parent.end,
  1663. ast: ast,
  1664. defineNode: curNode,
  1665. idNode: curNode,
  1666. types: defTypes,
  1667. called: [],
  1668. }
  1669. );
  1670. }
  1671. }
  1672. else {
  1673. //debugger;
  1674. }
  1675. }
  1676. Checked() {
  1677. this.#CullGlobalVar(this._globalVarList, this._tempVars, this._assignNode);
  1678. //变量基本检查,检查名称冲突及重复定义
  1679. this.#VarBaseCheck(this._globalVarList, this._tempVars);
  1680. }
  1681. CallRelation(ast, pName, curNode, parent, des, depth) {
  1682. //函数定义不参与变量的使用判断
  1683. if (parent.type == AstNode.FunctionDeclaration) return;
  1684. if (parent.type == AstNode.MemberExpression) return;
  1685. if (parent.type == AstNode.ObjectProperty) return;
  1686. let identifierName = curNode.name;
  1687. let findVar = this._globalVarList.find((x) => x.name == identifierName);
  1688. let callInfo;
  1689. if (findVar) {
  1690. //全局变量被使用(先排除重名变量和函数)
  1691. if (findVar.idNode == curNode) return;
  1692. //检查最近的范围内,是否有重名变量定义...
  1693. let curTempVars = this._tempVars.filter((x) => x.file == ast.file && x.varName == identifierName);//在当前ast文件中查找和全局变量名称重名的定义
  1694. for (let i = 0; i <= curTempVars.length - 1; i++) {
  1695. //
  1696. if (AstHelper.IsInDomainNode(parent, curTempVars[i].domainNode)) return;
  1697. }
  1698. callInfo = AstHelper.GetNodeCallRel(ast, curNode, parent);
  1699. if (callInfo) {
  1700. let called = findVar.called.find((c) => c.file == ast.file && c.name == callInfo.name && c.member == callInfo.member && c.kind == callInfo.kind);
  1701. //检查是否涉及到变量的值类型改变
  1702. let isAssignment = false;
  1703. if ((pName == "left" && parent.type == AstNode.AssignmentExpression) || (pName == "property" && parent.type == AstNode.MemberExpression)) {
  1704. isAssignment = true;
  1705. let valType = AstHelper.AssignmentTypeConvert(parent, parent.right);
  1706. if (valType && findVar.types.indexOf(valType) < 0) {
  1707. findVar.types.push(valType);
  1708. }
  1709. findVar.isValTypeChange = false;
  1710. if (findVar.types.length > 1) {
  1711. findVar.isValTypeChange = true;
  1712. }
  1713. }
  1714. if (called) {
  1715. //找到相同的调用
  1716. called.pos.push({
  1717. start: curNode.start,
  1718. end: curNode.end,
  1719. });
  1720. }
  1721. else {
  1722. //在相同的对象中
  1723. findVar.called.push({
  1724. file: ast.file,
  1725. name: callInfo.name,
  1726. member: callInfo.member,
  1727. kind: callInfo.kind,
  1728. isAssignment: isAssignment,
  1729. start: callInfo.node.start,
  1730. end: callInfo.node.end,
  1731. ext: callInfo.ext,
  1732. pos: [{
  1733. start: curNode.start,
  1734. end: curNode.end,
  1735. }],
  1736. path: AstHelper.GetParentPath(ast, callInfo.node),//查询当前调用的父级信息
  1737. });
  1738. }
  1739. }
  1740. }
  1741. }
  1742. get Result() {
  1743. return {
  1744. AllGlobalVarList: this._globalVarList,
  1745. GlobalVarList: function (fileName) {
  1746. return this.AllGlobalVarList.filter((x) => x.file == fileName);
  1747. },
  1748. }
  1749. }
  1750. }
  1751. window.AstParserPlugin_GlobalVar = AstParserPlugin_GlobalVar;
  1752. /**
  1753. * 当前主要过滤使用原生的getElementBy...和jquery相关调用的选择器形式
  1754. * 如:document.getElementById("name")及$("#name")的相关使用解析
  1755. */
  1756. class AstParserPlugin_Selector extends AstParserPlugin {
  1757. SelectorType = {
  1758. ID: "ID选择器",
  1759. Name: "名称选择器",
  1760. Classes: "类选择器",
  1761. Label: "标签选择器",
  1762. Undefined: "未能识别",
  1763. }
  1764. RegInfo = {
  1765. Parse: AstNode.Identifier,
  1766. //CallRelation: AstNode.FunctionDeclaration,
  1767. }
  1768. _selectorList = [];
  1769. constructor(parser, option) {
  1770. super(parser, option)
  1771. }
  1772. #GetSelectorType(callMethod, selectorPar) {
  1773. if (callMethod != "$" && callMethod != "querySelector" && callMethod != "querySelectorAll") {
  1774. switch (callMethod) {
  1775. case "getElementById": {
  1776. return this.SelectorType.ID;
  1777. }
  1778. case "getElementsByName": {
  1779. return this.SelectorType.Name;
  1780. }
  1781. case "getElementsByClassName": {
  1782. return this.SelectorType.Classes;
  1783. }
  1784. case "getElementsByTagName": {
  1785. return this.SelectorType.Label;
  1786. }
  1787. }
  1788. }
  1789. else {
  1790. let filterContext = selectorPar.parContext;
  1791. /*
  1792. if (filterContext.replaceAll(" ", "").substring(0, 2) == "'<" &&
  1793. filterContext.replaceAll(" ", "").slice(-2) == ">'") {
  1794. //$("<div>...</div>")
  1795. return "";
  1796. }*/
  1797. if (selectorPar.parType != "值" && selectorPar.parType != "表达式") {
  1798. if (selectorPar.parType == "模板字符") {
  1799. let f = filterContext.replaceAll(" ", "").substring(0, 2);
  1800. if (f == "`#") {
  1801. return this.SelectorType.ID;
  1802. }
  1803. else if (f == "`.") {
  1804. return this.SelectorType.Classes;
  1805. }
  1806. else if (f == "`$") {
  1807. return this.SelectorType.Undefined;
  1808. }
  1809. else {
  1810. if (filterContext.indexOf("[name=") > 0) {
  1811. return this.SelectorType.Name;
  1812. }
  1813. else {
  1814. return this.SelectorType.Label;
  1815. }
  1816. }
  1817. }
  1818. else {
  1819. return this.SelectorType.Undefined;
  1820. }
  1821. }
  1822. else {
  1823. let f = filterContext.replaceAll(" ", "").substring(0, 2);
  1824. if (f == "'#") {
  1825. return this.SelectorType.ID;
  1826. }
  1827. else if (f == "'.") {
  1828. return this.SelectorType.Classes;
  1829. }
  1830. else {
  1831. if (filterContext.indexOf("[name=") > 0) {
  1832. return this.SelectorType.Name;
  1833. }
  1834. else {
  1835. return this.SelectorType.Label;
  1836. }
  1837. }
  1838. }
  1839. }
  1840. }
  1841. //选择器接口调用方式
  1842. _SelectorInterface = new Map([["getElementById", null], ["getElementsByName", null], ["getElementsByClassName", null], ["getElementsByTagName", null],
  1843. ["getElementsByTagNameNS", null], ["querySelector", null], ["querySelectorAll", null]]);
  1844. //从Identifier节点获取调用的名称是否满足要求
  1845. Parse(ast, pName, curNode, parent, des, depth) {
  1846. if ((pName == "property" && this._SelectorInterface.has(curNode.name))
  1847. || (pName == "callee" && curNode.name == "$" && parent.arguments[0].type != AstNode.FunctionExpression)) {
  1848. //js原生选择器和$选择器
  1849. let callNode = AstHelper.FindParentNode(parent, AstNode.CallExpression);
  1850. if (callNode) {
  1851. let selectorPar = "";
  1852. let firstParNode = callNode.arguments[0];
  1853. if (callNode.arguments.length > 0) selectorPar = AstHelper.GetParasString(firstParNode);
  1854. if (!selectorPar.parContext) return;
  1855. //读取选择器类别
  1856. let selectWay = this.#GetSelectorType(curNode.name, selectorPar);
  1857. if (!selectWay) return;
  1858. this._selectorList.push({
  1859. file: ast.file,
  1860. selectorWay: selectWay,
  1861. selectorCall: (curNode.name == "$") ? "jquery" : curNode.name,
  1862. start: callNode.start,
  1863. end: callNode.end,
  1864. ast: ast,
  1865. defineNode: callNode,
  1866. expressionNode: AstHelper.FindParentNode(callNode.parent, AstNode.ExpressionStatement),//整个表达式节点
  1867. selectorFilter: selectorPar.parContext,
  1868. des: AstHelper.GetVarNodeDes(callNode),
  1869. });
  1870. }
  1871. }
  1872. }
  1873. //获取操作类型
  1874. #GetMemberCallType(selectorNode) {
  1875. let ret = {
  1876. kind: "未能识别",
  1877. kindDes: '',
  1878. }
  1879. if (!selectorNode) return ret;
  1880. //获取成员的事件配置参数,如$("#name").on("change", function(){...})
  1881. let getMemberArgs = function () {
  1882. let eventName = "";
  1883. if (selectorParent.parent?.type == AstNode.CallExpression) {
  1884. let arg0 = selectorParent.parent.arguments[0];
  1885. if (arg0.type == AstNode.Identifier) {
  1886. eventName = arg0.name;
  1887. }
  1888. else {
  1889. eventName = arg0.value;
  1890. }
  1891. }
  1892. return eventName;
  1893. }
  1894. let selectorParent = selectorNode.parent;
  1895. if (selectorParent?.type == AstNode.MemberExpression) {
  1896. //判断父级是否属于成员调用
  1897. let propName = selectorParent.property.name;
  1898. switch (propName) {
  1899. case "append":
  1900. case "appendTo":
  1901. case "prepend":
  1902. case "prependTo":
  1903. case "after":
  1904. case "insertAfter":
  1905. case "before":
  1906. case "insertBefore":
  1907. {
  1908. ret.kind = "新增元素";
  1909. return ret;
  1910. }
  1911. case "html":
  1912. {
  1913. if (selectorParent.parent.arguments.length > 0) {
  1914. ret.kind = "修改元素";
  1915. }
  1916. else {
  1917. ret.kind = "获取属性/元素";
  1918. }
  1919. return ret;
  1920. }
  1921. case "get":
  1922. case "find":
  1923. case "not":
  1924. case "children":
  1925. case "parents":
  1926. case "has":
  1927. case "parents":
  1928. case "parentsUntil":
  1929. case "closest":
  1930. {
  1931. ret.kind = "获取属性/元素";
  1932. return ret;
  1933. }
  1934. case "remove":
  1935. case "removeChild":
  1936. case "empty":
  1937. case "detach":
  1938. case "unwrap":
  1939. {
  1940. ret.kind = "移除元素";
  1941. return ret;
  1942. }
  1943. case "attr":
  1944. case "prop": {
  1945. if (selectorParent.parent.arguments.length > 1) {
  1946. ret.kind = "修改属性";
  1947. }
  1948. else {
  1949. ret.kind = "获取属性/元素";
  1950. }
  1951. return ret;
  1952. }
  1953. case "text":
  1954. case "val":
  1955. case "width":
  1956. case "height":
  1957. case "scrollTop": {
  1958. if (selectorParent.parent.arguments.length > 0) {
  1959. ret.kind = "修改属性";
  1960. }
  1961. else {
  1962. ret.kind = "获取属性/元素";
  1963. }
  1964. return ret;
  1965. }
  1966. case "removeAttr":
  1967. {
  1968. ret.kind = "修改属性";
  1969. return ret;
  1970. }
  1971. case "css":
  1972. {
  1973. if (selectorParent.parent.arguments?.length == 1) {
  1974. if (selectorParent.parent.arguments[0].type == AstNode.ObjectExpression) {
  1975. ret.kind = "修改样式";
  1976. let props = [];
  1977. for (let prop of selectorParent.parent.arguments[0].properties) {
  1978. props.push({
  1979. name: (prop.key.type == AstNode.Identifier) ? prop.key.name : prop.key.value,
  1980. defineNode: prop,
  1981. });
  1982. }
  1983. ret.kindDes = props;
  1984. }
  1985. else {
  1986. ret.kind = "获取属性/元素";
  1987. }
  1988. }
  1989. else {
  1990. ret.kind = "修改样式";
  1991. let propName = "";
  1992. if (selectorParent.parent.arguments[0].type == AstNode.Identifier) {
  1993. propName = selectorParent.parent.arguments[0].name;
  1994. }
  1995. else {
  1996. propName = selectorParent.parent.arguments[0].value;
  1997. }
  1998. ret.kindDes = [
  1999. {
  2000. name: propName,
  2001. defineNode: selectorParent.parent.arguments[0],
  2002. }
  2003. ]
  2004. }
  2005. return ret;
  2006. }
  2007. case "hide":
  2008. case "show":
  2009. case "classList":
  2010. case "addClass":
  2011. case "removeClass":
  2012. {
  2013. ret.kind = "修改样式"
  2014. return ret;
  2015. }
  2016. case "on":
  2017. case "bind":
  2018. case "live": {
  2019. ret.kind = "事件处理";
  2020. //获取事件名称
  2021. ret.kindDes = getMemberArgs() ?? "";
  2022. return ret;
  2023. }
  2024. case "change":
  2025. case "click":
  2026. case "dblclick":
  2027. case "mousedown":
  2028. case "mouseup":
  2029. case "mouseover":
  2030. case "mouseout":
  2031. case "mousemove":
  2032. case "mouseenter":
  2033. case "mouseleave":
  2034. case "focus":
  2035. case "blur":
  2036. case "keydown":
  2037. case "keyup":
  2038. case "keypress":
  2039. case "drag":
  2040. case "dragstart":
  2041. case "dragend":
  2042. case "dragover":
  2043. case "drop":
  2044. case "error":
  2045. case "scroll":
  2046. {
  2047. ret.kind = "事件处理";
  2048. ret.kindDes = propName;
  2049. /*暂不区分事件绑定和事件执行
  2050. if (selectorParent.parent.arguments?.length > 0) {
  2051. }
  2052. else {
  2053. }
  2054. */
  2055. return ret;
  2056. }
  2057. case "addEventListener":
  2058. {
  2059. ret.kind = "事件处理";
  2060. //获取事件名称
  2061. ret.kindDes = getMemberArgs() ?? "";
  2062. return ret;
  2063. }
  2064. case "off":
  2065. case "removeEventListener":
  2066. {
  2067. ret.kind = "事件处理";
  2068. return ret;
  2069. }
  2070. default: {
  2071. ret.kind = "获取属性/元素";
  2072. return ret;
  2073. }
  2074. }
  2075. }
  2076. else {
  2077. //既不在赋值表达式,也不在变量定义中,同时不是成员调用,则可能作为了方法的调用参数
  2078. ret.kind = "获取属性/元素";
  2079. return ret;
  2080. }
  2081. }
  2082. _selectorParentNodeType = new Map([[AstNode.AssignmentExpression, null], [AstNode.VariableDeclarator, null], [AstNode.ObjectProperty, null], [AstNode.ExpressionStatement, null]]);
  2083. #SelectorBaseCheck() {
  2084. for (let selector of this._selectorList) {
  2085. //查找selector所在函数
  2086. let funcNode = AstHelper.FindParentNode(selector.defineNode.parent, AstNode.FunctionDeclaration);
  2087. if (funcNode) {
  2088. selector.funcInfo = AstHelper.GetCurNodeCallInfoByFuncDeclaration(selector.ast, funcNode, funcNode.parent);
  2089. }
  2090. else {
  2091. funcNode = AstHelper.FindParentNode(selector.defineNode.parent, AstNode.FunctionExpression);
  2092. if (funcNode) {
  2093. selector.funcInfo = AstHelper.GetCurNodeCallInfoByFuncExpression(selector.ast, funcNode, funcNode.parent);
  2094. }
  2095. else {
  2096. selector.funcInfo = {
  2097. name: "全局",
  2098. member: "",
  2099. kind: "global",
  2100. node: null
  2101. }
  2102. }
  2103. }
  2104. //查找selector操作类别
  2105. let funcExpressionNode = AstHelper.FindParentNodeByTypes(selector.defineNode.parent, AstHelper.M_FuncExpression);
  2106. //todo:跟踪选择器对应变量的使用情况...
  2107. let k = {};
  2108. //查找最先满足条件的父级节点
  2109. let findNode = AstHelper.FindParentNodeByTypes(selector.defineNode.parent, this._selectorParentNodeType, funcExpressionNode);
  2110. if (findNode == null) {
  2111. k = this.#GetMemberCallType(selector.defineNode);
  2112. }
  2113. else if (findNode.type == AstNode.AssignmentExpression) {
  2114. if (AstHelper.IsInDomainNode(selector.defineNode, findNode.right, findNode)) {
  2115. //在赋值表达式的右侧出现
  2116. //xxx=$("#con").width() + 100;
  2117. k.kind = "获取属性/元素";
  2118. }
  2119. else {
  2120. //如果是在左侧,则需要判断是否作为调用的参数出现
  2121. let isPars = AstHelper.IsInParasNode(selector.defineNode, findNode);
  2122. if (isPars) {
  2123. //getObj($("#name").val()).name = "...";
  2124. k.kind = "获取属性/元素";
  2125. }
  2126. else {
  2127. //判断是否onchange类似赋值处理...
  2128. if (AstHelper.IsArrayEleNode(selector.defineNode, findNode)) {
  2129. //xxx.info[$("#name").val()] = "..."
  2130. k.kind = "获取属性/元素";
  2131. } else {
  2132. if (findNode.left.property?.name.indexOf("on") == 0) {
  2133. //document.getElementBy...("").onclick = ()=>{...}
  2134. k.kindDes = findNode.left.property.name.replace("on", "");
  2135. k.kind = "事件处理";
  2136. }
  2137. else {
  2138. if (selector.defineNode.parent.property.name == "style") {
  2139. //document.getElementBy...("").style.width = "100px;"
  2140. k.kind = "修改样式";
  2141. }
  2142. else {
  2143. k.kind = "修改属性";
  2144. }
  2145. }
  2146. }
  2147. }
  2148. }
  2149. }
  2150. else if (findNode.type == AstNode.VariableDeclarator) {
  2151. //let xx = $("#name").val();
  2152. //let xx = data[$("#name").val()];
  2153. k.kind = "获取属性/元素";
  2154. }
  2155. else if (findNode.type == AstNode.ObjectProperty) {
  2156. //{aa: $("#name").val()}
  2157. k.kind = "获取属性/元素";
  2158. }
  2159. else if (findNode.type == AstNode.ExpressionStatement) {
  2160. if (AstHelper.IsInParasNode(selector.defineNode, findNode)) {
  2161. //call($("#name").val(),"...");
  2162. k.kind = "获取属性/元素";
  2163. }
  2164. else {
  2165. if (findNode.expression.type == AstNode.AwaitExpression) {
  2166. //这种情况可能不存在,兼容处理...
  2167. if (findNode.expression.argument.callee.type == AstNode.MemberExpression) {
  2168. k = this.#GetMemberCallType(findNode.expression.argument.callee.property);
  2169. }
  2170. else {
  2171. k = this.#GetMemberCallType(findNode.expression.argument.callee);
  2172. }
  2173. }
  2174. else if (findNode.expression.type == AstNode.LogicalExpression) {
  2175. //isOk && $("#name").on("click",function(){...})
  2176. if (findNode.expression.right.callee?.type == AstNode.MemberExpression) {
  2177. k = this.#GetMemberCallType(findNode.expression.right.callee.property);
  2178. }
  2179. else {
  2180. k.kind = "获取属性/元素";
  2181. }
  2182. }
  2183. else {
  2184. //$("#name").change(function () { ...})
  2185. k = this.#GetMemberCallType(findNode.expression.callee.property);
  2186. }
  2187. }
  2188. }
  2189. else {
  2190. debugger;
  2191. console.log("SelectorBaseCheck中发现未能识别的节点类型" + findNode.type);
  2192. }
  2193. selector.kind = k.kind;
  2194. selector.kindDes = k.kindDes;
  2195. }
  2196. }
  2197. Checked() {
  2198. this.#SelectorBaseCheck();
  2199. }
  2200. get Result() {
  2201. return {
  2202. AllSelectorList: this._selectorList,
  2203. SelectorList: function (fileName) {
  2204. return this.AllSelectorList.filter((x) => x.file == fileName);
  2205. },
  2206. }
  2207. }
  2208. }
  2209. window.AstParserPlugin_Selector = AstParserPlugin_Selector;
  2210. class AstParser {
  2211. _ast = [];//_ast可以是当对象,也可以是一个ast的数组 {type:"File",start:"0",end:"", program:"",file:""}
  2212. _regPlugins = [];
  2213. constructor() {
  2214. }
  2215. /**
  2216. * Ast属性
  2217. */
  2218. get Ast() {
  2219. return this._ast;
  2220. }
  2221. /**
  2222. * 创建解析对象
  2223. * @param {any} ast
  2224. * @returns
  2225. */
  2226. static CreateParser(ast) {
  2227. let parser = new AstParser();
  2228. parser.Insert(ast);
  2229. return parser;
  2230. }
  2231. /**
  2232. * 清除ast
  2233. */
  2234. Clear() {
  2235. this._ast = [];
  2236. }
  2237. Insert(ast, fileName) {
  2238. if (!ast) return;
  2239. if (fileName) ast.file = fileName;
  2240. ast.root = true;
  2241. this._ast.push(ast);// = ast;
  2242. }
  2243. /**
  2244. * 查找指定函数引用
  2245. * @param {any} funcName
  2246. */
  2247. GetFuncCall(funcName) {
  2248. let ret = [];
  2249. let option = {
  2250. callExp: (ast, pName, curNode, parent, des, depth) => {
  2251. let callee = curNode.callee;
  2252. if (callee.type == AstNode.Identifier) {//MemberExpression //对象成员调用
  2253. //函数调用
  2254. if (callee.name == funcName) {
  2255. let curInfo = AstHelper.GetCurNodeCallInfoByCallExpression(ast, curNode, parent);
  2256. ret.push({
  2257. file: ast.file,
  2258. class: "",
  2259. name: funcName,
  2260. start: curInfo.start,
  2261. end: curInfo.end,
  2262. node: curInfo.node,
  2263. called: [],
  2264. })
  2265. }
  2266. }
  2267. }
  2268. }
  2269. this.#StartAstParser(option);
  2270. return ret;
  2271. }
  2272. /**
  2273. * 查找指定函数的定义
  2274. * @param {any} funcName
  2275. */
  2276. GetFuncDefine(funcName) {
  2277. let ret = [];
  2278. let option = {
  2279. funcDec: (ast, pName, curNode, parent, des, depth) => {
  2280. let id = curNode.id;
  2281. if (id.name == funcName) {
  2282. let curInfo = AstHelper.GetCurNodeCallInfoByFuncDeclaration(ast, curNode, parent);
  2283. ret.push({
  2284. file: ast.file,
  2285. class: "",
  2286. name: funcName,
  2287. start: curInfo.start,
  2288. end: curInfo.end,
  2289. node: curInfo.node,
  2290. called: [],
  2291. })
  2292. }
  2293. }
  2294. }
  2295. this.#StartAstParser(option);
  2296. return ret;
  2297. }
  2298. /**
  2299. * 开始ast解析
  2300. * @param {any} option
  2301. */
  2302. #StartAstParser(option, useBuffer) {
  2303. if (Array.isArray(this._ast)) {
  2304. //如果是数组,则查找多个ast
  2305. this._ast.forEach((astItem) => {
  2306. let curAst = astItem.ast ?? astItem;
  2307. curAst.file = astItem.file;
  2308. curAst.root = true;
  2309. AstHelper.TraverseNode(curAst, curAst, null, option, 0, useBuffer);
  2310. });
  2311. }
  2312. else {
  2313. let curAst = this._ast.ast ?? this._ast;
  2314. curAst.file = this._ast.file;
  2315. curAst.root = true;
  2316. AstHelper.TraverseNode(this._ast.ast ?? this._ast, this._ast.ast ?? this._ast, null, option, 0, useBuffer);
  2317. }
  2318. }
  2319. //克隆对象
  2320. #DeepClone(obj) {
  2321. if (obj === null || typeof obj !== 'object') {
  2322. return obj;
  2323. }
  2324. let clone = Array.isArray(obj) ? [] : {};
  2325. for (let key in obj) {
  2326. if (key == "parent" || key == "idNode" || key == "defineNode") continue;
  2327. if (obj.hasOwnProperty(key)) {
  2328. clone[key] = this.#DeepClone(obj[key]);
  2329. }
  2330. }
  2331. return clone;
  2332. }
  2333. /**
  2334. * 解析子对象列表
  2335. * @param {any} subNode
  2336. * @param {any} option
  2337. */
  2338. AnalyzeSubNodeObjectList(subNode, option) {
  2339. let parseNode = this.#DeepClone(subNode);
  2340. let subParser = AstParser.CreateParser();
  2341. subParser.Insert(parseNode);
  2342. if (!option) option = {
  2343. func: {
  2344. enabled: true,//解析函数
  2345. analyzeCallRel: true,//解析调用关系
  2346. },
  2347. globalVar: {
  2348. enabled: true,//解析变量
  2349. analyzeCallRel: true,//解析调用关系
  2350. },
  2351. selector: {//选择器解析
  2352. enabled: true,
  2353. }
  2354. }
  2355. return subParser.AnalyzeObjectList(option);
  2356. }
  2357. #ConfigAnalyzePlugin(analyzeOption, type, plugin) {
  2358. let regInfo = plugin.RegInfo;
  2359. switch (regInfo[type]) {
  2360. case AstNode.Program: {
  2361. if (!analyzeOption.program) analyzeOption.program = [];
  2362. analyzeOption.program.push(plugin[type].bind(plugin));
  2363. break;
  2364. }
  2365. case AstNode.ExpressionStatement: {
  2366. if (!analyzeOption.expression) analyzeOption.expression = [];
  2367. analyzeOption.expression.push(plugin[type].bind(plugin));
  2368. break;
  2369. }
  2370. case AstNode.CallExpression: {
  2371. if (!analyzeOption.callExp) analyzeOption.callExp = [];
  2372. analyzeOption.callExp.push(plugin[type].bind(plugin));
  2373. break;
  2374. }
  2375. case AstNode.FunctionExpression: {
  2376. if (!analyzeOption.funcExp) analyzeOption.funcExp = [];
  2377. analyzeOption.funcExp.push(plugin[type].bind(plugin));
  2378. break;
  2379. }
  2380. case AstNode.BlockStatement: {
  2381. if (!analyzeOption.block) analyzeOption.block = [];
  2382. analyzeOption.block.push(plugin[type].bind(plugin));
  2383. break;
  2384. }
  2385. case AstNode.FunctionDeclaration: {
  2386. if (!analyzeOption.funcDec) analyzeOption.funcDec = [];
  2387. analyzeOption.funcDec.push(plugin[type].bind(plugin));
  2388. break;
  2389. }
  2390. case AstNode.Identifier: {
  2391. if (!analyzeOption.identifier) analyzeOption.identifier = [];
  2392. analyzeOption.identifier.push(plugin[type].bind(plugin));
  2393. break;
  2394. }
  2395. case AstNode.ReturnStatement: {
  2396. if (!analyzeOption.returnstatement) analyzeOption.returnstatement = [];
  2397. analyzeOption.returnstatement.push(plugin[type].bind(plugin));
  2398. break;
  2399. }
  2400. case AstNode.CatchClause: {
  2401. if (!analyzeOption.catchclause) analyzeOption.catchclause = [];
  2402. analyzeOption.catchclause.push(plugin[type].bind(plugin));
  2403. break;
  2404. }
  2405. }
  2406. }
  2407. //注册插件
  2408. RegPlugin(pluginName, option) {
  2409. this._regPlugins.push({
  2410. name: pluginName,
  2411. option: option,
  2412. });
  2413. }
  2414. //清除插件
  2415. ClearPlugin() {
  2416. this._regPlugins = [];
  2417. }
  2418. //获取所有对象列表
  2419. AnalyzeObjectList(useBuffer) {
  2420. //{funcOption:{enabled:true},varOption:{enabled:true}}
  2421. console.time("AnalyzeObjectList");
  2422. let anaOption = {};
  2423. let parserPluginObj = [];
  2424. if (useBuffer == undefined) useBuffer = true;
  2425. //配置默认插件
  2426. if (this._regPlugins.length <= 0) {
  2427. this._regPlugins = [
  2428. { name: "AstParserPlugin_Func", option: null },
  2429. { name: "AstParserPlugin_GlobalVar", option: null },
  2430. { name: "AstParserPlugin_Selector", option: null },
  2431. ];
  2432. }
  2433. //实例化插件
  2434. for (let pluginInfo of this._regPlugins) {
  2435. parserPluginObj.push(new window[pluginInfo.name](this, pluginInfo.option));
  2436. }
  2437. //挂接插件接口
  2438. parserPluginObj.forEach((x) => {
  2439. this.#ConfigAnalyzePlugin(anaOption, "Parse", x);
  2440. });
  2441. //获取到对应的对象
  2442. this.#StartAstParser(anaOption, useBuffer);
  2443. anaOption = {};
  2444. parserPluginObj.forEach((x) => {
  2445. if (x.Checked) x.Checked();
  2446. });
  2447. anaOption = {};
  2448. //挂接插件接口
  2449. parserPluginObj.forEach((x) => {
  2450. this.#ConfigAnalyzePlugin(anaOption, "CallRelation", x);
  2451. });
  2452. //获取调用关系
  2453. this.#StartAstParser(anaOption, useBuffer);
  2454. console.timeEnd("AnalyzeObjectList");
  2455. //配置返回结果
  2456. let ret = {};
  2457. parserPluginObj.forEach((x) => {
  2458. ret = Object.assign({}, ret, x.Result);
  2459. });
  2460. parserPluginObj = null;
  2461. return ret;
  2462. }
  2463. /**
  2464. * 查找所有函数列表
  2465. */
  2466. GetAllFuncList(isFindCallRel) {
  2467. return this.AnalyzeObjectList({
  2468. func: {
  2469. enabled: true,
  2470. analyzeCallRel: isFindCallRel
  2471. }
  2472. });
  2473. }
  2474. /**
  2475. * 查找所有全局变量
  2476. */
  2477. GetAllGlobalVarList(isFindCallRel) {
  2478. return this.AnalyzeObjectList({
  2479. globalVar: {
  2480. enabled: true,
  2481. analyzeCallRel: isFindCallRel
  2482. }
  2483. });
  2484. }
  2485. /**
  2486. * 查找全局变量的引用
  2487. */
  2488. GetGlobalVarCall(varName) {
  2489. }
  2490. /**
  2491. * 查找所有类列表
  2492. */
  2493. GetAllClassList() {
  2494. }
  2495. /**
  2496. * 查找类成员列表
  2497. * @param {any} className
  2498. */
  2499. GetClassMemberList(className) {
  2500. }
  2501. /**
  2502. * 查找对象类成员定义
  2503. */
  2504. GetClassMemberDefine(className, memberName) {
  2505. }
  2506. /**
  2507. * 查找类成员引用
  2508. * @param {any} className
  2509. * @param {any} memberName
  2510. */
  2511. GetClassMemberCall(className, memberName) {
  2512. }
  2513. }