astparser.js 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892
  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. if (ArrayisArray(aryParent)) aryParent = aryParent.parent;
  1020. if (aryParent?.type == AstNode.VariableDeclarator) {
  1021. //let xx = ()=>{...}
  1022. ret = AstHelper.GetCurNodeCallInfoByVariableDeclarator(ast, aryParent, aryParent.parent);
  1023. }
  1024. else if (aryParent?.type == AstNode.AssignmentExpression) {
  1025. //xx.test = ()=>{...}
  1026. ret = AstHelper.GetCurNodeCallInfoByAssigmentExpress(ast, aryParent, aryParent.parent);
  1027. }
  1028. else if (aryParent?.type == AstNode.CallExpression) {
  1029. //test("",()=>{gTest = "bbb";});
  1030. ret = AstHelper.GetCurNodeCallInfoByCallExpression(ast, aryParent, aryParent.parent);
  1031. }
  1032. else if (aryParent?.type == AstNode.MemberExpression) {
  1033. //xx.test[()=>{...}]="aaa"
  1034. let memberNode = aryParent;
  1035. if (memberNode.object?.type == AstNode.Identifier) {
  1036. ret = {
  1037. name: memberNode.object.name,
  1038. member: "",
  1039. kind: "array",
  1040. node: memberNode,
  1041. };
  1042. }
  1043. else {
  1044. ret = {
  1045. name: memberNode.object.object.name,
  1046. member: memberNode.object.property.name,
  1047. kind: "property",
  1048. node: memberNode,
  1049. };
  1050. }
  1051. }
  1052. else {
  1053. ret = {
  1054. name: "=>",
  1055. member: "",
  1056. kind: "arrow",
  1057. node: arrowNode,
  1058. };
  1059. }
  1060. return ret;
  1061. }
  1062. //获取当前节点所在的类方法的调用信息
  1063. static GetCurNodeCallInfoByClassMember(ast, curNode, parent) {
  1064. let ret;
  1065. let methodNode = curNode;
  1066. if (methodNode) {
  1067. ret = {
  1068. name: methodNode.parent.parent.parent.id.name,
  1069. member: methodNode.key.name,
  1070. kind: "class",
  1071. node: methodNode
  1072. }
  1073. }
  1074. return ret;
  1075. }
  1076. //获取节点的调用关系
  1077. static GetNodeCallRel(ast, curNode, parent) {
  1078. //查找类
  1079. //查找函数定义
  1080. //查找函数表达式
  1081. //查找箭头表达式
  1082. //查找调用表达式
  1083. //查找赋值表达式
  1084. let callInfo;
  1085. let identifierName = curNode.name;
  1086. let findNode = AstHelper.FindParentNode(parent, AstNode.ClassDeclaration);
  1087. if (findNode) {
  1088. let memberNode = AstHelper.FindParentNodeByTypes(parent, AstHelper.M_ClassMember);
  1089. if (memberNode) {
  1090. //类成员中使用包括方法和属性
  1091. callInfo = AstHelper.GetCurNodeCallInfoByClassMember(ast, memberNode, memberNode.parent);
  1092. }
  1093. }
  1094. else {
  1095. findNode = AstHelper.FindParentNode(parent, AstNode.FunctionDeclaration);
  1096. if (findNode) {
  1097. //找到了调用的所在函数定义
  1098. //callInfo = AstHelper.GetCurNodeCallInfoByFuncDeclaration(ast, findNode, findNode.parent);
  1099. let isParVar = false;
  1100. if (identifierName) {
  1101. //当前identifier定义如果属于FunctionDeclaration参数则退出,如果是变量的identifier,在函数的参数中表示变量定义而不是引用
  1102. //如果是callexpression,则不可能出现在函数定义的参数中
  1103. findNode.params.forEach((x) => {
  1104. if (x == curNode) isParVar = true;
  1105. if (x.type == AstNode.Identifier && x.name == identifierName) isParVar = true;
  1106. if (x.type == AstNode.AssignmentPattern && x.left.name == identifierName) isParVar = true;
  1107. });
  1108. }
  1109. if (!isParVar) {
  1110. callInfo = AstHelper.GetCurNodeCallInfoByFuncDeclaration(ast, findNode, findNode.parent);
  1111. }
  1112. }
  1113. else {
  1114. findNode = AstHelper.FindParentNode(parent, AstNode.FunctionExpression);
  1115. if (findNode) {
  1116. if (findNode.parent.parent?.type == AstNode.CallExpression) {
  1117. //作为参数形式调用,如test("", function(){vField1 = "222"})
  1118. //$(function(){...});
  1119. callInfo = AstHelper.GetCurNodeCallInfoByCallExpression(ast, findNode.parent.parent, findNode.parent.parent.parent);
  1120. }
  1121. else {
  1122. callInfo = AstHelper.GetCurNodeCallInfoByFuncExpression(ast, findNode, findNode.parent);
  1123. }
  1124. }
  1125. else {
  1126. //查找箭头函数
  1127. findNode = AstHelper.FindParentNode(parent, AstNode.ArrowFunctionExpression);
  1128. if (findNode) {
  1129. if (findNode.parent.parent?.type == AstNode.CallExpression) {
  1130. callInfo = AstHelper.GetCurNodeCallInfoByCallExpression(ast, findNode.parent.parent, findNode.parent.parent.parent);
  1131. }
  1132. else {
  1133. callInfo = AstHelper.GetCurNodeCallInfoByArrowFunc(ast, findNode, findNode.parent);
  1134. }
  1135. }
  1136. else {
  1137. //查找调用函数
  1138. findNode = AstHelper.FindParentNode(parent, AstNode.CallExpression);
  1139. if (findNode) {
  1140. callInfo = AstHelper.GetCurNodeCallInfoByCallExpression(ast, findNode, findNode.parent);
  1141. }
  1142. else {
  1143. findNode = AstHelper.FindParentNode(parent, AstNode.AssignmentExpression);
  1144. if (findNode) {
  1145. callInfo = AstHelper.GetCurNodeCallInfoByAssigmentExpress(ast, findNode, findNode.parent);
  1146. }
  1147. else {
  1148. //直接在脚本中调用
  1149. callInfo = {
  1150. name: "",
  1151. kind: "immediately",
  1152. start: curNode.start,
  1153. end: curNode.end,
  1154. node: curNode,
  1155. }
  1156. }
  1157. }
  1158. }
  1159. }
  1160. }
  1161. }
  1162. return callInfo;
  1163. }
  1164. }
  1165. //函数解析插件
  1166. class AstParserPlugin {
  1167. _parser;
  1168. _option;
  1169. RegInfo = {
  1170. Parser: "",//配置解析挂接的ast节点类型,AstNode中所定义的节点
  1171. CallRelation: "",//配置调用关系解析挂接的ast节点类型,AstNode中所定义的节点
  1172. }
  1173. constructor(parser, option) {
  1174. this._parser = parser;
  1175. this._option = option;
  1176. }
  1177. //Parser(ast, pName, curNode, parent, des, depth){ }//配置对应节点的解析处理逻辑
  1178. //Checked(){}//配置解析对象的清理和二次分析处理
  1179. //CallRelation(){}//配置对应的调用关系逻辑
  1180. get Result() {
  1181. return {};
  1182. }
  1183. }
  1184. class AstParserPlugin_Func extends AstParserPlugin {
  1185. RegInfo = {
  1186. Parse: AstNode.FunctionDeclaration,
  1187. CallRelation: AstNode.CallExpression,
  1188. }
  1189. _funcList = [];
  1190. constructor(parser, option) {
  1191. super(parser, option);
  1192. }
  1193. #WriteCallRel(ast, curNode, funcDefine, callInfo) {
  1194. if (funcDefine && callInfo) {
  1195. let called = funcDefine.called.find((c) => c.file == ast.file && c.name == callInfo.name && c.member == callInfo.member && c.kind == callInfo.kind);
  1196. if (called) {
  1197. //找到相同的调用
  1198. called.pos.push({
  1199. start: curNode.start,
  1200. end: curNode.end
  1201. })
  1202. }
  1203. else {
  1204. //在相同的对象中
  1205. funcDefine.called.push({
  1206. file: ast.file,
  1207. name: callInfo.name,
  1208. member: callInfo.member,
  1209. kind: callInfo.kind,
  1210. start: callInfo.node.start,
  1211. end: callInfo.node.end,
  1212. ext: callInfo.ext,
  1213. pos: [{ start: curNode.start, end: curNode.end }],
  1214. path: AstHelper.GetParentPath(ast, callInfo.node),//查询引用的父级信息
  1215. });
  1216. }
  1217. }
  1218. }
  1219. #GetFuncCodeAnalyze(ast, funcNode) {
  1220. //风险类别动态js代码执行,
  1221. //ele.innerHTML,ele.outerHTML, $("xxx").html(),eval(...),new Function()
  1222. let riskType = [];
  1223. let isIgnoreCatch = false;
  1224. let isHtmlModify = false;
  1225. let isLoopHtmlModify = false;
  1226. let option = {
  1227. identifier: [function (ast, pName, curNode, parent, des, depth) {
  1228. //if (AstHelper.RiskCodes.indexOf(curNode.name) >= 0) {
  1229. switch (curNode.name) {
  1230. case "Function": {
  1231. if (parent.type == "NewExpression") {
  1232. if (riskType.indexOf(curNode.name) < 0) riskType.push(curNode.name);
  1233. }
  1234. break;
  1235. }
  1236. case "eval": {
  1237. if (parent.type == "CallExpression") {
  1238. if (riskType.indexOf(curNode.name) < 0) riskType.push(curNode.name);
  1239. }
  1240. break;
  1241. }
  1242. case "append":
  1243. case "appendTo":
  1244. case "prepend":
  1245. case "prependTo":
  1246. case "after":
  1247. case "insertAfter":
  1248. case "before":
  1249. case "insertBefore":
  1250. case "html":
  1251. {
  1252. if (pName == "property") {
  1253. let pre = AstHelper.GetCallPrePath(parent);
  1254. if (pre.substring(0, 1) == "$") {
  1255. isHtmlModify = true;
  1256. //判断父级是否为循环语句
  1257. if (!isLoopHtmlModify) {
  1258. let loopNode = AstHelper.FindParentNodeByTypes(parent, AstHelper.M_LoopBlockStatements, funcNode);
  1259. if (loopNode) {
  1260. isLoopHtmlModify = true;
  1261. }
  1262. }
  1263. }
  1264. }
  1265. break;
  1266. }
  1267. case "innerHTML":
  1268. case "outerHTML":
  1269. {
  1270. if (pName == "property" && parent.type == AstNode.MemberExpression) {
  1271. isHtmlModify = true;
  1272. //判断父级是否为循环语句
  1273. if (!isLoopHtmlModify) {
  1274. let loopNode = AstHelper.FindParentNodeByTypes(parent, AstHelper.M_LoopBlockStatements, funcNode);
  1275. if (loopNode) {
  1276. isLoopHtmlModify = true;
  1277. }
  1278. }
  1279. }
  1280. break;
  1281. }
  1282. }
  1283. //}
  1284. }],
  1285. catchclause: [function (ast, pName, curNode, parent, des, depth) {
  1286. if (curNode.body) {
  1287. if (curNode.body.body.length <= 0) {
  1288. //let fNode = AstHelper.FindParentNodeByTypes(parent, [AstNode.FunctionExpression, AstNode.ArrowFunctionExpression, AstNode.CallExpression, AstNode.FunctionDeclaration], funcNode);
  1289. let fNode = AstHelper.FindParentNode(parent, AstNode.FunctionDeclaration, funcNode);
  1290. if (fNode == funcNode) isIgnoreCatch = true;
  1291. }
  1292. }
  1293. }]
  1294. /*
  1295. returnstatement: function (ast, pName, curNode, parent, des, depth) {
  1296. let pBlock = AstHelper.FindParentNodeByTypes(parent, [AstNode.FunctionExpression, AstNode.CallExpression, AstNode.ArrowFunctionExpression, AstNode.FunctionDeclaration]);
  1297. if (pBlock && pBlock != funcNode) {
  1298. //
  1299. }
  1300. else {
  1301. //
  1302. }
  1303. }*/
  1304. }
  1305. AstHelper.TraverseNode(ast, funcNode, null, option, false);
  1306. return {
  1307. risk: riskType,//是否有代码注入风险即动态js脚本执行,riskType则是js脚本执行方式,eval,或function
  1308. isIgnoreCatch: isIgnoreCatch,//是否有异常被忽略
  1309. isHtmlModify: isHtmlModify,//是否有HTML修改
  1310. isLoopHtmlModify: isLoopHtmlModify,//是否在循环中操作HTML
  1311. }
  1312. }
  1313. #FuncBaseCheck(funcList) {
  1314. let _this = this;
  1315. funcList.forEach((fItem) => {
  1316. //位置和所在文件名不同,则可能存在名称冲突
  1317. let otherDefine = funcList.find((x) => x.name == fItem.name && (x.start != fItem.start || x.file != fItem.file));
  1318. fItem.isConflict = false;
  1319. if (otherDefine) {
  1320. fItem.isConflict = true;
  1321. }
  1322. //判断是否空的函数定义
  1323. fItem.isNullFunc = false;
  1324. if (fItem.defineNode.body.body?.length <= 0) {
  1325. fItem.isNullFunc = true;
  1326. }
  1327. //获取函数中代码的最大深度
  1328. let depth = AstHelper.GetNodeDepth(fItem.defineNode, 0);
  1329. if (depth > 20) {
  1330. fItem.codeDepth = ">20";
  1331. }
  1332. else if (depth > 15) {
  1333. fItem.codeDepth = ">15";
  1334. }
  1335. else if (depth > 10) {
  1336. fItem.codeDepth = ">10";
  1337. }
  1338. else if (depth > 5) {
  1339. fItem.codeDepth = ">5";
  1340. }
  1341. else {
  1342. fItem.codeDepth = "";
  1343. }
  1344. let codeAnalyze = _this.#GetFuncCodeAnalyze(fItem.ast, fItem.defineNode);
  1345. fItem.risk = codeAnalyze?.risk;
  1346. fItem.isIgnoreCatch = codeAnalyze?.isIgnoreCatch;
  1347. fItem.isHtmlModify = codeAnalyze?.isHtmlModify;
  1348. fItem.isLoopHtmlModify = codeAnalyze?.isLoopHtmlModify;
  1349. });
  1350. }
  1351. //解析节点
  1352. Parse(ast, pName, curNode, parent, des, depth) {
  1353. let id = curNode.id;
  1354. if (id.type == AstNode.Identifier) {
  1355. let des = AstHelper.GetFuncNodeDes(curNode);
  1356. //判断是否在对应的块下面定义的函数
  1357. //如function Init(){ function getData(){...} },getData则表示内部子函数
  1358. let isGlobal = true;
  1359. let blockNode = AstHelper.FindParentNode(parent, AstNode.BlockStatement);
  1360. if (blockNode) {
  1361. isGlobal = AstHelper.FindParentNodeByTypes(blockNode, AstHelper.M_BlockStatements) ? false : true;
  1362. }
  1363. this._funcList.push({
  1364. file: ast.file,
  1365. range: isGlobal ? "global" : "sub",
  1366. class: "",
  1367. isAsync: curNode.async,
  1368. name: id.name,
  1369. des: des,
  1370. start: curNode.start,
  1371. end: curNode.end,
  1372. ast: ast,
  1373. defineNode: curNode,
  1374. idNode: curNode.id,
  1375. called: [],//保存被那些地方调用
  1376. //refers:[],//保存引用了那些函数
  1377. path: AstHelper.GetParentPath(ast, curNode),
  1378. //sameName:[],
  1379. });
  1380. }
  1381. }
  1382. //检查及分析节点
  1383. Checked() {
  1384. //函数基本检查,命名冲突,是否空函数等
  1385. this.#FuncBaseCheck(this._funcList);
  1386. }
  1387. //关系分析
  1388. CallRelation(ast, pName, curNode, parent, des, depth) {
  1389. let callee = curNode.callee;
  1390. let funcDefine;
  1391. let callInfo;
  1392. if (callee.type == AstNode.Identifier) {
  1393. //是一个函数调用,如xx();
  1394. funcDefine = this._funcList.find((x) => x.name == callee.name);
  1395. if (funcDefine) {
  1396. callInfo = AstHelper.GetNodeCallRel(ast, curNode, parent);
  1397. //判断是否存在嵌套使用
  1398. let pFuncDesc = AstHelper.FindParentNode(parent, AstNode.FunctionDeclaration, funcDefine.parent);
  1399. if (funcDefine.isNested != true) {
  1400. if (pFuncDesc && pFuncDesc.id?.name == funcDefine.name) {
  1401. funcDefine.isNested = true;
  1402. }
  1403. else {
  1404. funcDefine.isNested = false;
  1405. }
  1406. }
  1407. this.#WriteCallRel(ast, curNode, funcDefine, callInfo);
  1408. }
  1409. else {
  1410. //根据调用参数名称去匹配
  1411. if (curNode.arguments?.length > 0) {
  1412. curNode.arguments.forEach((arg) => {
  1413. funcDefine = this._funcList.find((x) => x.name == arg.name);
  1414. if (funcDefine) {
  1415. callInfo = AstHelper.GetCurNodeCallInfoByCallExpression(ast, curNode, parent);
  1416. this.#WriteCallRel(ast, arg, funcDefine, callInfo);
  1417. }
  1418. });
  1419. }
  1420. }
  1421. }
  1422. else {
  1423. //根据参数去匹配名称
  1424. if (callee.arguments?.length > 0) {
  1425. callee.arguments.forEach((arg) => {
  1426. funcDefine = this._funcList.find((x) => x.name == arg.name);
  1427. if (funcDefine) {
  1428. callInfo = AstHelper.GetCurNodeCallInfoByCallExpression(ast, curNode, parent);
  1429. this.#WriteCallRel(ast, arg, funcDefine, callInfo);
  1430. }
  1431. });
  1432. }
  1433. }
  1434. }
  1435. //结果获取
  1436. get Result() {
  1437. return {
  1438. AllFuncList: this._funcList,
  1439. FuncList: function (fileName) {
  1440. return this.AllFuncList.filter((x) => x.file == fileName);
  1441. },
  1442. }
  1443. }
  1444. }
  1445. window.AstParserPlugin_Func = AstParserPlugin_Func;
  1446. class AstParserPlugin_GlobalVar extends AstParserPlugin {
  1447. RegInfo = {
  1448. Parse: AstNode.Identifier,
  1449. CallRelation: AstNode.Identifier,
  1450. };
  1451. _globalVarList = [];
  1452. _tempVars = [];
  1453. _assignNode = [];
  1454. constructor(parser, option) {
  1455. super(parser, option)
  1456. }
  1457. //检查变量名称是否存在冲突
  1458. #VarBaseCheck(globalVars, tempVars) {
  1459. //检查全局变量名是否存在冲突或重复定义
  1460. globalVars.forEach((gVar) => {
  1461. let varDefine = tempVars.find((cVar) => cVar.varName == gVar.name && cVar.defineNode != gVar.defineNode);
  1462. gVar.isConflict = false;
  1463. if (varDefine) {
  1464. //存在命名冲突
  1465. gVar.isConflict = true;
  1466. }
  1467. let globalDefine = globalVars.find((cVar) => cVar.name == gVar.name && cVar.defineNode != gVar.defineNode);
  1468. gVar.isRepetDefine = false;
  1469. if (globalDefine) {
  1470. //存在重复定义
  1471. gVar.isRepetDefine = true;
  1472. }
  1473. });
  1474. }
  1475. //重新梳理全局变量
  1476. #CullGlobalVar(ret, vars, assignNode) {
  1477. if (assignNode.length > 0) {
  1478. assignNode.forEach((n) => {
  1479. let varNode = vars.filter(x => x.varName == n.node.name);
  1480. let isSameDomain = false;//判断复制表达式是否落在变量定义所在的范围内,是否相同作用域,如果是则不是全局变量
  1481. varNode.some((v) => {
  1482. if (AstHelper.IsInDomainNode(n.parent, v.domainNode)) {
  1483. //变量使用与变量定义的作用域相同
  1484. isSameDomain = true;
  1485. return true;
  1486. }
  1487. });
  1488. if (!isSameDomain) {
  1489. //没有找到定义或作用范围不同
  1490. let gVarDefine = ret.find(x => x.name == n.node.name);
  1491. if (gVarDefine) {
  1492. //找到了全局变量的定义
  1493. let assignmentType = AstHelper.AssignmentTypeConvert(n.parent, n.parent.right);
  1494. if (assignmentType) {
  1495. if (gVarDefine.types.indexOf(assignmentType) < 0) gVarDefine.types.push(assignmentType);
  1496. }
  1497. }
  1498. else {
  1499. let expressionStatement = AstHelper.FindParentNode(n.parent, AstNode.ExpressionStatement);
  1500. let defTypes = [];
  1501. let assignmentType = AstHelper.AssignmentTypeConvert(n.parent, n.parent.right);
  1502. if (assignmentType) {
  1503. defTypes.push(assignmentType);
  1504. }
  1505. ret.push(
  1506. {
  1507. file: n.file,
  1508. name: n.node.name,
  1509. defWay: "",
  1510. des: expressionStatement ? AstHelper.GetVarNodeDes(expressionStatement) : "",
  1511. start: n.parent.start,
  1512. end: n.parent.end,
  1513. defineNode: n.node,
  1514. idNode: n.idNode,
  1515. types: defTypes,
  1516. called: [],
  1517. }
  1518. );
  1519. }
  1520. }
  1521. });
  1522. }
  1523. //查找没有使用申明方式定义变量的元素
  1524. //let notDeclareds = ret.filter((x) => !x.defWay);
  1525. for (let i = ret.length - 1; i >= 0; i--) {
  1526. //该变量是否在其他文件中有定义
  1527. let unDefVar = ret[i];
  1528. if (!unDefVar.defWay) {
  1529. //没有申明定义方式
  1530. let findDef = ret.find((x) => x.file != unDefVar.file && x.defWay);
  1531. if (findDef) {
  1532. //当在其他文件中有定义类别时,则移除当前的定义
  1533. ret.splice(i, 1);
  1534. }
  1535. }
  1536. }
  1537. }
  1538. Parse(ast, pName, curNode, parent, des, depth) {
  1539. //对象处于函数参数的定义中,如function xx(p1,p2){}
  1540. if (parent == parent.parent?.params) {
  1541. //参数变量
  1542. this._tempVars.push({
  1543. file: ast.file,
  1544. varName: curNode.name,
  1545. defineNode: curNode,
  1546. domainNode: parent.parent,
  1547. });
  1548. return;
  1549. }
  1550. else if (parent.parent == parent.parent.parent.params) {
  1551. //示例:function test(isFailed = false){}
  1552. this._tempVars.push({
  1553. file: ast.file,
  1554. varName: curNode.name,
  1555. defineNode: curNode,
  1556. domainNode: parent.parent.parent
  1557. });
  1558. return;
  1559. }
  1560. //对象处于函数调用的arguments中,如xx(p1,p2);
  1561. if (parent == parent.parent?.arguments) return;
  1562. if (parent?.type == AstNode.MemberExpression) {
  1563. //如果节点类型为成员表达式,则判断是否为window成员
  1564. //window.xx = "";
  1565. //globalThis.xx="";
  1566. //if (window.getSelection){}//IfStatement
  1567. if (pName == "property" && parent.parent?.type == AstNode.AssignmentExpression && (parent.object.name == "window" || parent.object.name == "globalThis")) {
  1568. if (AstHelper.M_GlobalVar.has(curNode.name)) return;
  1569. //如果已经存在相同的windows变量,则直接退出
  1570. let fWindowVarNode = this._globalVarList.find((x) => x.name == curNode.name);
  1571. if (fWindowVarNode) return;
  1572. let defTypes = [];
  1573. let assignmentType = AstHelper.AssignmentTypeConvert(parent.parent, parent.parent.right);
  1574. if (assignmentType) {
  1575. defTypes.push(assignmentType);
  1576. }
  1577. this._globalVarList.push(
  1578. {
  1579. file: ast.file,
  1580. name: curNode.name,
  1581. defWay: parent.object.name,
  1582. des: "",
  1583. start: parent.start,
  1584. end: parent.end,
  1585. ast: ast,
  1586. defineNode: curNode,
  1587. idNode: curNode,
  1588. types: defTypes,
  1589. called: [],
  1590. }
  1591. );
  1592. }
  1593. }
  1594. else if (parent?.type == AstNode.AssignmentExpression) {
  1595. //a=6;
  1596. //let varDeclaration = findParentNode(parent);
  1597. if (pName == "right") return;
  1598. this._assignNode.push({
  1599. file: ast.file,
  1600. node: curNode,
  1601. parent: parent,
  1602. idNode: curNode,
  1603. });
  1604. }
  1605. else if (parent?.type == AstNode.VariableDeclarator) {
  1606. if (pName == "init") return; //var xx = 12;
  1607. let blockNode = AstHelper.FindParentNode(parent, AstNode.BlockStatement);
  1608. if (blockNode) {
  1609. let findNode = AstHelper.FindParentNodeByTypes(blockNode, AstHelper.M_BlockStatements);
  1610. if (findNode) {
  1611. this._tempVars.push({
  1612. file: ast.file,
  1613. varName: curNode.name,
  1614. defineNode: curNode,
  1615. domainNode: findNode,
  1616. });
  1617. }
  1618. else {
  1619. let varDesNode = AstHelper.FindParentNode(parent, AstNode.VariableDeclaration);
  1620. let des = "";
  1621. if (varDesNode) des = AstHelper.GetVarNodeDes(varDesNode);
  1622. let defTypes = [];
  1623. let assignmentType = AstHelper.AssignmentTypeConvert(parent, parent.init);
  1624. if (assignmentType) {
  1625. defTypes.push(assignmentType);
  1626. }
  1627. this._globalVarList.push(
  1628. {
  1629. file: ast.file,
  1630. name: curNode.name,
  1631. defWay: varDesNode.kind,
  1632. des: des,
  1633. start: parent.start,
  1634. end: parent.end,
  1635. ast: ast,
  1636. defineNode: curNode,
  1637. idNode: curNode,
  1638. types: defTypes,
  1639. called: [],
  1640. }
  1641. );
  1642. }
  1643. }
  1644. else {
  1645. let varDesNode = AstHelper.FindParentNode(parent, AstNode.VariableDeclaration);
  1646. let des = "";
  1647. if (varDesNode) des = AstHelper.GetVarNodeDes(varDesNode);
  1648. let defTypes = [];
  1649. let assignmentType = AstHelper.AssignmentTypeConvert(parent, parent.init);
  1650. if (assignmentType) {
  1651. defTypes.push(assignmentType);
  1652. }
  1653. //不在block块中的语句
  1654. this._globalVarList.push(
  1655. {
  1656. file: ast.file,
  1657. name: curNode.name,
  1658. defWay: varDesNode.kind,
  1659. des: des,
  1660. start: parent.start,
  1661. end: parent.end,
  1662. ast: ast,
  1663. defineNode: curNode,
  1664. idNode: curNode,
  1665. types: defTypes,
  1666. called: [],
  1667. }
  1668. );
  1669. }
  1670. }
  1671. else {
  1672. //debugger;
  1673. }
  1674. }
  1675. Checked() {
  1676. this.#CullGlobalVar(this._globalVarList, this._tempVars, this._assignNode);
  1677. //变量基本检查,检查名称冲突及重复定义
  1678. this.#VarBaseCheck(this._globalVarList, this._tempVars);
  1679. }
  1680. CallRelation(ast, pName, curNode, parent, des, depth) {
  1681. //函数定义不参与变量的使用判断
  1682. if (parent.type == AstNode.FunctionDeclaration) return;
  1683. if (parent.type == AstNode.MemberExpression) return;
  1684. if (parent.type == AstNode.ObjectProperty) return;
  1685. let identifierName = curNode.name;
  1686. let findVar = this._globalVarList.find((x) => x.name == identifierName);
  1687. let callInfo;
  1688. if (findVar) {
  1689. //全局变量被使用(先排除重名变量和函数)
  1690. if (findVar.idNode == curNode) return;
  1691. //检查最近的范围内,是否有重名变量定义...
  1692. let curTempVars = this._tempVars.filter((x) => x.file == ast.file && x.varName == identifierName);//在当前ast文件中查找和全局变量名称重名的定义
  1693. for (let i = 0; i <= curTempVars.length - 1; i++) {
  1694. //
  1695. if (AstHelper.IsInDomainNode(parent, curTempVars[i].domainNode)) return;
  1696. }
  1697. callInfo = AstHelper.GetNodeCallRel(ast, curNode, parent);
  1698. if (callInfo) {
  1699. let called = findVar.called.find((c) => c.file == ast.file && c.name == callInfo.name && c.member == callInfo.member && c.kind == callInfo.kind);
  1700. //检查是否涉及到变量的值类型改变
  1701. let isAssignment = false;
  1702. if ((pName == "left" && parent.type == AstNode.AssignmentExpression) || (pName == "property" && parent.type == AstNode.MemberExpression)) {
  1703. isAssignment = true;
  1704. let valType = AstHelper.AssignmentTypeConvert(parent, parent.right);
  1705. if (valType && findVar.types.indexOf(valType) < 0) {
  1706. findVar.types.push(valType);
  1707. }
  1708. findVar.isValTypeChange = false;
  1709. if (findVar.types.length > 1) {
  1710. findVar.isValTypeChange = true;
  1711. }
  1712. }
  1713. if (called) {
  1714. //找到相同的调用
  1715. called.pos.push({
  1716. start: curNode.start,
  1717. end: curNode.end,
  1718. });
  1719. }
  1720. else {
  1721. //在相同的对象中
  1722. findVar.called.push({
  1723. file: ast.file,
  1724. name: callInfo.name,
  1725. member: callInfo.member,
  1726. kind: callInfo.kind,
  1727. isAssignment: isAssignment,
  1728. start: callInfo.node.start,
  1729. end: callInfo.node.end,
  1730. ext: callInfo.ext,
  1731. pos: [{
  1732. start: curNode.start,
  1733. end: curNode.end,
  1734. }],
  1735. path: AstHelper.GetParentPath(ast, callInfo.node),//查询当前调用的父级信息
  1736. });
  1737. }
  1738. }
  1739. }
  1740. }
  1741. get Result() {
  1742. return {
  1743. AllGlobalVarList: this._globalVarList,
  1744. GlobalVarList: function (fileName) {
  1745. return this.AllGlobalVarList.filter((x) => x.file == fileName);
  1746. },
  1747. }
  1748. }
  1749. }
  1750. window.AstParserPlugin_GlobalVar = AstParserPlugin_GlobalVar;
  1751. /**
  1752. * 当前主要过滤使用原生的getElementBy...和jquery相关调用的选择器形式
  1753. * 如:document.getElementById("name")及$("#name")的相关使用解析
  1754. */
  1755. class AstParserPlugin_Selector extends AstParserPlugin {
  1756. SelectorType = {
  1757. ID: "ID选择器",
  1758. Name: "名称选择器",
  1759. Classes: "类选择器",
  1760. Label: "标签选择器",
  1761. Undefined: "未能识别",
  1762. }
  1763. RegInfo = {
  1764. Parse: AstNode.Identifier,
  1765. //CallRelation: AstNode.FunctionDeclaration,
  1766. }
  1767. _selectorList = [];
  1768. constructor(parser, option) {
  1769. super(parser, option)
  1770. }
  1771. #GetSelectorType(callMethod, selectorPar) {
  1772. if (callMethod != "$" && callMethod != "querySelector" && callMethod != "querySelectorAll") {
  1773. switch (callMethod) {
  1774. case "getElementById": {
  1775. return this.SelectorType.ID;
  1776. }
  1777. case "getElementsByName": {
  1778. return this.SelectorType.Name;
  1779. }
  1780. case "getElementsByClassName": {
  1781. return this.SelectorType.Classes;
  1782. }
  1783. case "getElementsByTagName": {
  1784. return this.SelectorType.Label;
  1785. }
  1786. }
  1787. }
  1788. else {
  1789. let filterContext = selectorPar.parContext;
  1790. /*
  1791. if (filterContext.replaceAll(" ", "").substring(0, 2) == "'<" &&
  1792. filterContext.replaceAll(" ", "").slice(-2) == ">'") {
  1793. //$("<div>...</div>")
  1794. return "";
  1795. }*/
  1796. if (selectorPar.parType != "值" && selectorPar.parType != "表达式") {
  1797. if (selectorPar.parType == "模板字符") {
  1798. let f = filterContext.replaceAll(" ", "").substring(0, 2);
  1799. if (f == "`#") {
  1800. return this.SelectorType.ID;
  1801. }
  1802. else if (f == "`.") {
  1803. return this.SelectorType.Classes;
  1804. }
  1805. else if (f == "`$") {
  1806. return this.SelectorType.Undefined;
  1807. }
  1808. else {
  1809. if (filterContext.indexOf("[name=") > 0) {
  1810. return this.SelectorType.Name;
  1811. }
  1812. else {
  1813. return this.SelectorType.Label;
  1814. }
  1815. }
  1816. }
  1817. else {
  1818. return this.SelectorType.Undefined;
  1819. }
  1820. }
  1821. else {
  1822. let f = filterContext.replaceAll(" ", "").substring(0, 2);
  1823. if (f == "'#") {
  1824. return this.SelectorType.ID;
  1825. }
  1826. else if (f == "'.") {
  1827. return this.SelectorType.Classes;
  1828. }
  1829. else {
  1830. if (filterContext.indexOf("[name=") > 0) {
  1831. return this.SelectorType.Name;
  1832. }
  1833. else {
  1834. return this.SelectorType.Label;
  1835. }
  1836. }
  1837. }
  1838. }
  1839. }
  1840. //选择器接口调用方式
  1841. _SelectorInterface = new Map([["getElementById", null], ["getElementsByName", null], ["getElementsByClassName", null], ["getElementsByTagName", null],
  1842. ["getElementsByTagNameNS", null], ["querySelector", null], ["querySelectorAll", null]]);
  1843. //从Identifier节点获取调用的名称是否满足要求
  1844. Parse(ast, pName, curNode, parent, des, depth) {
  1845. if ((pName == "property" && this._SelectorInterface.has(curNode.name))
  1846. || (pName == "callee" && curNode.name == "$" && parent.arguments[0].type != AstNode.FunctionExpression)) {
  1847. //js原生选择器和$选择器
  1848. let callNode = AstHelper.FindParentNode(parent, AstNode.CallExpression);
  1849. if (callNode) {
  1850. let selectorPar = "";
  1851. let firstParNode = callNode.arguments[0];
  1852. if (callNode.arguments.length > 0) selectorPar = AstHelper.GetParasString(firstParNode);
  1853. if (!selectorPar.parContext) return;
  1854. //读取选择器类别
  1855. let selectWay = this.#GetSelectorType(curNode.name, selectorPar);
  1856. if (!selectWay) return;
  1857. this._selectorList.push({
  1858. file: ast.file,
  1859. selectorWay: selectWay,
  1860. selectorCall: (curNode.name == "$") ? "jquery" : curNode.name,
  1861. start: callNode.start,
  1862. end: callNode.end,
  1863. ast: ast,
  1864. defineNode: callNode,
  1865. expressionNode: AstHelper.FindParentNode(callNode.parent, AstNode.ExpressionStatement),//整个表达式节点
  1866. selectorFilter: selectorPar.parContext,
  1867. des: AstHelper.GetVarNodeDes(callNode),
  1868. });
  1869. }
  1870. }
  1871. }
  1872. //获取操作类型
  1873. #GetMemberCallType(selectorNode) {
  1874. let ret = {
  1875. kind: "未能识别",
  1876. kindDes: '',
  1877. }
  1878. if (!selectorNode) return ret;
  1879. //获取成员的事件配置参数,如$("#name").on("change", function(){...})
  1880. let getMemberArgs = function () {
  1881. let eventName = "";
  1882. if (selectorParent.parent?.type == AstNode.CallExpression) {
  1883. let arg0 = selectorParent.parent.arguments[0];
  1884. if (arg0.type == AstNode.Identifier) {
  1885. eventName = arg0.name;
  1886. }
  1887. else {
  1888. eventName = arg0.value;
  1889. }
  1890. }
  1891. return eventName;
  1892. }
  1893. let selectorParent = selectorNode.parent;
  1894. if (selectorParent?.type == AstNode.MemberExpression) {
  1895. //判断父级是否属于成员调用
  1896. let propName = selectorParent.property.name;
  1897. switch (propName) {
  1898. case "append":
  1899. case "appendTo":
  1900. case "prepend":
  1901. case "prependTo":
  1902. case "after":
  1903. case "insertAfter":
  1904. case "before":
  1905. case "insertBefore":
  1906. {
  1907. ret.kind = "新增元素";
  1908. return ret;
  1909. }
  1910. case "html":
  1911. {
  1912. if (selectorParent.parent.arguments.length > 0) {
  1913. ret.kind = "修改元素";
  1914. }
  1915. else {
  1916. ret.kind = "获取属性/元素";
  1917. }
  1918. return ret;
  1919. }
  1920. case "get":
  1921. case "find":
  1922. case "not":
  1923. case "children":
  1924. case "parents":
  1925. case "has":
  1926. case "parents":
  1927. case "parentsUntil":
  1928. case "closest":
  1929. {
  1930. ret.kind = "获取属性/元素";
  1931. return ret;
  1932. }
  1933. case "remove":
  1934. case "removeChild":
  1935. case "empty":
  1936. case "detach":
  1937. case "unwrap":
  1938. {
  1939. ret.kind = "移除元素";
  1940. return ret;
  1941. }
  1942. case "attr":
  1943. case "prop": {
  1944. if (selectorParent.parent.arguments.length > 1) {
  1945. ret.kind = "修改属性";
  1946. }
  1947. else {
  1948. ret.kind = "获取属性/元素";
  1949. }
  1950. return ret;
  1951. }
  1952. case "text":
  1953. case "val":
  1954. case "width":
  1955. case "height":
  1956. case "scrollTop": {
  1957. if (selectorParent.parent.arguments.length > 0) {
  1958. ret.kind = "修改属性";
  1959. }
  1960. else {
  1961. ret.kind = "获取属性/元素";
  1962. }
  1963. return ret;
  1964. }
  1965. case "removeAttr":
  1966. {
  1967. ret.kind = "修改属性";
  1968. return ret;
  1969. }
  1970. case "css":
  1971. {
  1972. if (selectorParent.parent.arguments?.length == 1) {
  1973. if (selectorParent.parent.arguments[0].type == AstNode.ObjectExpression) {
  1974. ret.kind = "修改样式";
  1975. }
  1976. else {
  1977. ret.kind = "获取属性/元素";
  1978. }
  1979. }
  1980. else {
  1981. ret.kind = "修改样式";
  1982. }
  1983. return ret;
  1984. }
  1985. case "hide":
  1986. case "show":
  1987. case "classList":
  1988. case "addClass":
  1989. case "removeClass":
  1990. {
  1991. ret.kind = "修改样式"
  1992. return ret;
  1993. }
  1994. case "on":
  1995. case "bind":
  1996. case "live": {
  1997. ret.kind = "事件处理";
  1998. //获取事件名称
  1999. ret.kindDes = getMemberArgs() ?? "";
  2000. return ret;
  2001. }
  2002. case "change":
  2003. case "click":
  2004. case "dblclick":
  2005. case "mousedown":
  2006. case "mouseup":
  2007. case "mouseover":
  2008. case "mouseout":
  2009. case "mousemove":
  2010. case "mouseenter":
  2011. case "mouseleave":
  2012. case "focus":
  2013. case "blur":
  2014. case "keydown":
  2015. case "keyup":
  2016. case "keypress":
  2017. case "drag":
  2018. case "dragstart":
  2019. case "dragend":
  2020. case "dragover":
  2021. case "drop":
  2022. case "error":
  2023. case "scroll":
  2024. {
  2025. ret.kind = "事件处理";
  2026. ret.kindDes = propName;
  2027. /*暂不区分事件绑定和事件执行
  2028. if (selectorParent.parent.arguments?.length > 0) {
  2029. }
  2030. else {
  2031. }
  2032. */
  2033. return ret;
  2034. }
  2035. case "addEventListener":
  2036. {
  2037. ret.kind = "事件处理";
  2038. //获取事件名称
  2039. ret.kindDes = getMemberArgs() ?? "";
  2040. return ret;
  2041. }
  2042. case "off":
  2043. case "removeEventListener":
  2044. {
  2045. ret.kind = "事件处理";
  2046. return ret;
  2047. }
  2048. default: {
  2049. ret.kind = "获取属性/元素";
  2050. return ret;
  2051. }
  2052. }
  2053. }
  2054. else {
  2055. //既不在赋值表达式,也不在变量定义中,同时不是成员调用,则可能作为了方法的调用参数
  2056. ret.kind = "获取属性/元素";
  2057. return ret;
  2058. }
  2059. }
  2060. _selectorParentNodeType = new Map([[AstNode.AssignmentExpression, null], [AstNode.VariableDeclarator, null], [AstNode.ObjectProperty, null], [AstNode.ExpressionStatement, null]]);
  2061. #SelectorBaseCheck() {
  2062. for (let selector of this._selectorList) {
  2063. //查找selector所在函数
  2064. let funcNode = AstHelper.FindParentNode(selector.defineNode.parent, AstNode.FunctionDeclaration);
  2065. if (funcNode) {
  2066. selector.funcInfo = AstHelper.GetCurNodeCallInfoByFuncDeclaration(selector.ast, funcNode, funcNode.parent);
  2067. }
  2068. else {
  2069. funcNode = AstHelper.FindParentNode(selector.defineNode.parent, AstNode.FunctionExpression);
  2070. if (funcNode) {
  2071. selector.funcInfo = AstHelper.GetCurNodeCallInfoByFuncExpression(selector.ast, funcNode, funcNode.parent);
  2072. }
  2073. else {
  2074. selector.funcInfo = {
  2075. name: "全局",
  2076. member: "",
  2077. kind: "global",
  2078. node: null
  2079. }
  2080. }
  2081. }
  2082. //查找selector操作类别
  2083. let funcExpressionNode = AstHelper.FindParentNodeByTypes(selector.defineNode.parent, AstHelper.M_FuncExpression);
  2084. //todo:跟踪选择器对应变量的使用情况...
  2085. let k = {};
  2086. //查找最先满足条件的父级节点
  2087. let findNode = AstHelper.FindParentNodeByTypes(selector.defineNode.parent, this._selectorParentNodeType, funcExpressionNode);
  2088. if (findNode == null) {
  2089. k = this.#GetMemberCallType(selector.defineNode);
  2090. }
  2091. else if (findNode.type == AstNode.AssignmentExpression) {
  2092. if (AstHelper.IsInDomainNode(selector.defineNode, findNode.right, findNode)) {
  2093. //在赋值表达式的右侧出现
  2094. //xxx=$("#con").width() + 100;
  2095. k.kind = "获取属性/元素";
  2096. }
  2097. else {
  2098. //如果是在左侧,则需要判断是否作为调用的参数出现
  2099. let isPars = AstHelper.IsInParasNode(selector.defineNode, findNode);
  2100. if (isPars) {
  2101. //getObj($("#name").val()).name = "...";
  2102. k.kind = "获取属性/元素";
  2103. }
  2104. else {
  2105. //判断是否onchange类似赋值处理...
  2106. if (AstHelper.IsArrayEleNode(selector.defineNode, findNode)) {
  2107. //xxx.info[$("#name").val()] = "..."
  2108. k.kind = "获取属性/元素";
  2109. } else {
  2110. if (findNode.left.property?.name.indexOf("on") == 0) {
  2111. //document.getElementBy...("").onclick = ()=>{...}
  2112. k.kindDes = findNode.left.property.name.replace("on", "");
  2113. k.kind = "事件处理";
  2114. }
  2115. else {
  2116. if (selector.defineNode.parent.property.name == "style") {
  2117. //document.getElementBy...("").style.width = "100px;"
  2118. k.kind = "修改样式";
  2119. }
  2120. else {
  2121. k.kind = "修改属性";
  2122. }
  2123. }
  2124. }
  2125. }
  2126. }
  2127. }
  2128. else if (findNode.type == AstNode.VariableDeclarator) {
  2129. //let xx = $("#name").val();
  2130. //let xx = data[$("#name").val()];
  2131. k.kind = "获取属性/元素";
  2132. }
  2133. else if (findNode.type == AstNode.ObjectProperty) {
  2134. //{aa: $("#name").val()}
  2135. k.kind = "获取属性/元素";
  2136. }
  2137. else if (findNode.type == AstNode.ExpressionStatement) {
  2138. if (AstHelper.IsInParasNode(selector.defineNode, findNode)) {
  2139. //call($("#name").val(),"...");
  2140. k.kind = "获取属性/元素";
  2141. }
  2142. else {
  2143. if (findNode.expression.type == AstNode.AwaitExpression) {
  2144. //这种情况可能不存在,兼容处理...
  2145. if (findNode.expression.argument.callee.type == AstNode.MemberExpression) {
  2146. k = this.#GetMemberCallType(findNode.expression.argument.callee.property);
  2147. }
  2148. else {
  2149. k = this.#GetMemberCallType(findNode.expression.argument.callee);
  2150. }
  2151. }
  2152. else if (findNode.expression.type == AstNode.LogicalExpression) {
  2153. //isOk && $("#name").on("click",function(){...})
  2154. if (findNode.expression.right.callee?.type == AstNode.MemberExpression) {
  2155. k = this.#GetMemberCallType(findNode.expression.right.callee.property);
  2156. }
  2157. else {
  2158. k.kind = "获取属性/元素";
  2159. }
  2160. }
  2161. else {
  2162. //$("#name").change(function () { ...})
  2163. k = this.#GetMemberCallType(findNode.expression.callee.property);
  2164. }
  2165. }
  2166. }
  2167. else {
  2168. debugger;
  2169. console.log("SelectorBaseCheck中发现未能识别的节点类型" + findNode.type);
  2170. }
  2171. selector.kind = k.kind;
  2172. selector.kindDes = k.kindDes;
  2173. }
  2174. }
  2175. Checked() {
  2176. this.#SelectorBaseCheck();
  2177. }
  2178. get Result() {
  2179. return {
  2180. AllSelectorList: this._selectorList,
  2181. SelectorList: function (fileName) {
  2182. return this.AllSelectorList.filter((x) => x.file == fileName);
  2183. },
  2184. }
  2185. }
  2186. }
  2187. window.AstParserPlugin_Selector = AstParserPlugin_Selector;
  2188. class AstParser {
  2189. _ast = [];//_ast可以是当对象,也可以是一个ast的数组 {type:"File",start:"0",end:"", program:"",file:""}
  2190. _regPlugins = [];
  2191. constructor() {
  2192. }
  2193. /**
  2194. * Ast属性
  2195. */
  2196. get Ast() {
  2197. return this._ast;
  2198. }
  2199. /**
  2200. * 创建解析对象
  2201. * @param {any} ast
  2202. * @returns
  2203. */
  2204. static CreateParser(ast) {
  2205. let parser = new AstParser();
  2206. parser.Insert(ast);
  2207. return parser;
  2208. }
  2209. /**
  2210. * 清除ast
  2211. */
  2212. Clear() {
  2213. this._ast = [];
  2214. }
  2215. Insert(ast, fileName) {
  2216. if (!ast) return;
  2217. if (fileName) ast.file = fileName;
  2218. ast.root = true;
  2219. this._ast.push(ast);// = ast;
  2220. }
  2221. /**
  2222. * 查找指定函数引用
  2223. * @param {any} funcName
  2224. */
  2225. GetFuncCall(funcName) {
  2226. let ret = [];
  2227. let option = {
  2228. callExp: (ast, pName, curNode, parent, des, depth) => {
  2229. let callee = curNode.callee;
  2230. if (callee.type == AstNode.Identifier) {//MemberExpression //对象成员调用
  2231. //函数调用
  2232. if (callee.name == funcName) {
  2233. let curInfo = AstHelper.GetCurNodeCallInfoByCallExpression(ast, curNode, parent);
  2234. ret.push({
  2235. file: ast.file,
  2236. class: "",
  2237. name: funcName,
  2238. start: curInfo.start,
  2239. end: curInfo.end,
  2240. node: curInfo.node,
  2241. called: [],
  2242. })
  2243. }
  2244. }
  2245. }
  2246. }
  2247. this.#StartAstParser(option);
  2248. return ret;
  2249. }
  2250. /**
  2251. * 查找指定函数的定义
  2252. * @param {any} funcName
  2253. */
  2254. GetFuncDefine(funcName) {
  2255. let ret = [];
  2256. let option = {
  2257. funcDec: (ast, pName, curNode, parent, des, depth) => {
  2258. let id = curNode.id;
  2259. if (id.name == funcName) {
  2260. let curInfo = AstHelper.GetCurNodeCallInfoByFuncDeclaration(ast, curNode, parent);
  2261. ret.push({
  2262. file: ast.file,
  2263. class: "",
  2264. name: funcName,
  2265. start: curInfo.start,
  2266. end: curInfo.end,
  2267. node: curInfo.node,
  2268. called: [],
  2269. })
  2270. }
  2271. }
  2272. }
  2273. this.#StartAstParser(option);
  2274. return ret;
  2275. }
  2276. /**
  2277. * 开始ast解析
  2278. * @param {any} option
  2279. */
  2280. #StartAstParser(option, useBuffer) {
  2281. if (Array.isArray(this._ast)) {
  2282. //如果是数组,则查找多个ast
  2283. this._ast.forEach((astItem) => {
  2284. let curAst = astItem.ast ?? astItem;
  2285. curAst.file = astItem.file;
  2286. curAst.root = true;
  2287. AstHelper.TraverseNode(curAst, curAst, null, option, 0, useBuffer);
  2288. });
  2289. }
  2290. else {
  2291. let curAst = this._ast.ast ?? this._ast;
  2292. curAst.file = this._ast.file;
  2293. curAst.root = true;
  2294. AstHelper.TraverseNode(this._ast.ast ?? this._ast, this._ast.ast ?? this._ast, null, option, 0, useBuffer);
  2295. }
  2296. }
  2297. //克隆对象
  2298. #DeepClone(obj) {
  2299. if (obj === null || typeof obj !== 'object') {
  2300. return obj;
  2301. }
  2302. let clone = Array.isArray(obj) ? [] : {};
  2303. for (let key in obj) {
  2304. if (key == "parent" || key == "idNode" || key == "defineNode") continue;
  2305. if (obj.hasOwnProperty(key)) {
  2306. clone[key] = this.#DeepClone(obj[key]);
  2307. }
  2308. }
  2309. return clone;
  2310. }
  2311. /**
  2312. * 解析子对象列表
  2313. * @param {any} subNode
  2314. * @param {any} option
  2315. */
  2316. AnalyzeSubNodeObjectList(subNode, option) {
  2317. let parseNode = this.#DeepClone(subNode);
  2318. let subParser = AstParser.CreateParser();
  2319. subParser.Insert(parseNode);
  2320. if (!option) option = {
  2321. func: {
  2322. enabled: true,//解析函数
  2323. analyzeCallRel: true,//解析调用关系
  2324. },
  2325. globalVar: {
  2326. enabled: true,//解析变量
  2327. analyzeCallRel: true,//解析调用关系
  2328. },
  2329. selector: {//选择器解析
  2330. enabled: true,
  2331. }
  2332. }
  2333. return subParser.AnalyzeObjectList(option);
  2334. }
  2335. #ConfigAnalyzePlugin(analyzeOption, type, plugin) {
  2336. let regInfo = plugin.RegInfo;
  2337. switch (regInfo[type]) {
  2338. case AstNode.Program: {
  2339. if (!analyzeOption.program) analyzeOption.program = [];
  2340. analyzeOption.program.push(plugin[type].bind(plugin));
  2341. break;
  2342. }
  2343. case AstNode.ExpressionStatement: {
  2344. if (!analyzeOption.expression) analyzeOption.expression = [];
  2345. analyzeOption.expression.push(plugin[type].bind(plugin));
  2346. break;
  2347. }
  2348. case AstNode.CallExpression: {
  2349. if (!analyzeOption.callExp) analyzeOption.callExp = [];
  2350. analyzeOption.callExp.push(plugin[type].bind(plugin));
  2351. break;
  2352. }
  2353. case AstNode.FunctionExpression: {
  2354. if (!analyzeOption.funcExp) analyzeOption.funcExp = [];
  2355. analyzeOption.funcExp.push(plugin[type].bind(plugin));
  2356. break;
  2357. }
  2358. case AstNode.BlockStatement: {
  2359. if (!analyzeOption.block) analyzeOption.block = [];
  2360. analyzeOption.block.push(plugin[type].bind(plugin));
  2361. break;
  2362. }
  2363. case AstNode.FunctionDeclaration: {
  2364. if (!analyzeOption.funcDec) analyzeOption.funcDec = [];
  2365. analyzeOption.funcDec.push(plugin[type].bind(plugin));
  2366. break;
  2367. }
  2368. case AstNode.Identifier: {
  2369. if (!analyzeOption.identifier) analyzeOption.identifier = [];
  2370. analyzeOption.identifier.push(plugin[type].bind(plugin));
  2371. break;
  2372. }
  2373. case AstNode.ReturnStatement: {
  2374. if (!analyzeOption.returnstatement) analyzeOption.returnstatement = [];
  2375. analyzeOption.returnstatement.push(plugin[type].bind(plugin));
  2376. break;
  2377. }
  2378. case AstNode.CatchClause: {
  2379. if (!analyzeOption.catchclause) analyzeOption.catchclause = [];
  2380. analyzeOption.catchclause.push(plugin[type].bind(plugin));
  2381. break;
  2382. }
  2383. }
  2384. }
  2385. //注册插件
  2386. RegPlugin(pluginName, option) {
  2387. this._regPlugins.push({
  2388. name: pluginName,
  2389. option: option,
  2390. });
  2391. }
  2392. //清除插件
  2393. ClearPlugin() {
  2394. this._regPlugins = [];
  2395. }
  2396. //获取所有对象列表
  2397. AnalyzeObjectList(useBuffer) {
  2398. //{funcOption:{enabled:true},varOption:{enabled:true}}
  2399. console.time("AnalyzeObjectList");
  2400. let anaOption = {};
  2401. let parserPluginObj = [];
  2402. if (useBuffer == undefined) useBuffer = true;
  2403. //配置默认插件
  2404. if (this._regPlugins.length <= 0) {
  2405. this._regPlugins = [
  2406. { name: "AstParserPlugin_Func", option: null },
  2407. { name: "AstParserPlugin_GlobalVar", option: null },
  2408. { name: "AstParserPlugin_Selector", option: null },
  2409. ];
  2410. }
  2411. //实例化插件
  2412. for (let pluginInfo of this._regPlugins) {
  2413. parserPluginObj.push(new window[pluginInfo.name](this, pluginInfo.option));
  2414. }
  2415. //挂接插件接口
  2416. parserPluginObj.forEach((x) => {
  2417. this.#ConfigAnalyzePlugin(anaOption, "Parse", x);
  2418. });
  2419. //获取到对应的对象
  2420. this.#StartAstParser(anaOption, useBuffer);
  2421. anaOption = {};
  2422. parserPluginObj.forEach((x) => {
  2423. if (x.Checked) x.Checked();
  2424. });
  2425. anaOption = {};
  2426. //挂接插件接口
  2427. parserPluginObj.forEach((x) => {
  2428. this.#ConfigAnalyzePlugin(anaOption, "CallRelation", x);
  2429. });
  2430. //获取调用关系
  2431. this.#StartAstParser(anaOption, useBuffer);
  2432. console.timeEnd("AnalyzeObjectList");
  2433. //配置返回结果
  2434. let ret = {};
  2435. parserPluginObj.forEach((x) => {
  2436. ret = Object.assign({}, ret, x.Result);
  2437. });
  2438. parserPluginObj = null;
  2439. return ret;
  2440. }
  2441. /**
  2442. * 查找所有函数列表
  2443. */
  2444. GetAllFuncList(isFindCallRel) {
  2445. return this.AnalyzeObjectList({
  2446. func: {
  2447. enabled: true,
  2448. analyzeCallRel: isFindCallRel
  2449. }
  2450. });
  2451. }
  2452. /**
  2453. * 查找所有全局变量
  2454. */
  2455. GetAllGlobalVarList(isFindCallRel) {
  2456. return this.AnalyzeObjectList({
  2457. globalVar: {
  2458. enabled: true,
  2459. analyzeCallRel: isFindCallRel
  2460. }
  2461. });
  2462. }
  2463. /**
  2464. * 查找全局变量的引用
  2465. */
  2466. GetGlobalVarCall(varName) {
  2467. }
  2468. /**
  2469. * 查找所有类列表
  2470. */
  2471. GetAllClassList() {
  2472. }
  2473. /**
  2474. * 查找类成员列表
  2475. * @param {any} className
  2476. */
  2477. GetClassMemberList(className) {
  2478. }
  2479. /**
  2480. * 查找对象类成员定义
  2481. */
  2482. GetClassMemberDefine(className, memberName) {
  2483. }
  2484. /**
  2485. * 查找类成员引用
  2486. * @param {any} className
  2487. * @param {any} memberName
  2488. */
  2489. GetClassMemberCall(className, memberName) {
  2490. }
  2491. }