astparser.js 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917
  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. let props = [];
  1976. for (let prop of selectorParent.parent.arguments[0].properties) {
  1977. props.push({
  1978. name: (prop.key.type == AstNode.Identifier) ? prop.key.name : prop.key.value,
  1979. defineNode: prop,
  1980. });
  1981. }
  1982. ret.kindDes = props;
  1983. }
  1984. else {
  1985. ret.kind = "获取属性/元素";
  1986. }
  1987. }
  1988. else {
  1989. ret.kind = "修改样式";
  1990. let propName = "";
  1991. if (selectorParent.parent.arguments[0].type == AstNode.Identifier) {
  1992. propName = selectorParent.parent.arguments[0].name;
  1993. }
  1994. else {
  1995. propName = selectorParent.parent.arguments[0].value;
  1996. }
  1997. ret.kindDes = [
  1998. {
  1999. name: propName,
  2000. defineNode: selectorParent.parent.arguments[0],
  2001. }
  2002. ]
  2003. }
  2004. return ret;
  2005. }
  2006. case "hide":
  2007. case "show":
  2008. case "classList":
  2009. case "addClass":
  2010. case "removeClass":
  2011. {
  2012. ret.kind = "修改样式"
  2013. return ret;
  2014. }
  2015. case "on":
  2016. case "bind":
  2017. case "live": {
  2018. ret.kind = "事件处理";
  2019. //获取事件名称
  2020. ret.kindDes = getMemberArgs() ?? "";
  2021. return ret;
  2022. }
  2023. case "change":
  2024. case "click":
  2025. case "dblclick":
  2026. case "mousedown":
  2027. case "mouseup":
  2028. case "mouseover":
  2029. case "mouseout":
  2030. case "mousemove":
  2031. case "mouseenter":
  2032. case "mouseleave":
  2033. case "focus":
  2034. case "blur":
  2035. case "keydown":
  2036. case "keyup":
  2037. case "keypress":
  2038. case "drag":
  2039. case "dragstart":
  2040. case "dragend":
  2041. case "dragover":
  2042. case "drop":
  2043. case "error":
  2044. case "scroll":
  2045. {
  2046. ret.kind = "事件处理";
  2047. ret.kindDes = propName;
  2048. /*暂不区分事件绑定和事件执行
  2049. if (selectorParent.parent.arguments?.length > 0) {
  2050. }
  2051. else {
  2052. }
  2053. */
  2054. return ret;
  2055. }
  2056. case "addEventListener":
  2057. {
  2058. ret.kind = "事件处理";
  2059. //获取事件名称
  2060. ret.kindDes = getMemberArgs() ?? "";
  2061. return ret;
  2062. }
  2063. case "off":
  2064. case "removeEventListener":
  2065. {
  2066. ret.kind = "事件处理";
  2067. return ret;
  2068. }
  2069. default: {
  2070. ret.kind = "获取属性/元素";
  2071. return ret;
  2072. }
  2073. }
  2074. }
  2075. else {
  2076. //既不在赋值表达式,也不在变量定义中,同时不是成员调用,则可能作为了方法的调用参数
  2077. ret.kind = "获取属性/元素";
  2078. return ret;
  2079. }
  2080. }
  2081. _selectorParentNodeType = new Map([[AstNode.AssignmentExpression, null], [AstNode.VariableDeclarator, null], [AstNode.ObjectProperty, null], [AstNode.ExpressionStatement, null]]);
  2082. #SelectorBaseCheck() {
  2083. for (let selector of this._selectorList) {
  2084. //查找selector所在函数
  2085. let funcNode = AstHelper.FindParentNode(selector.defineNode.parent, AstNode.FunctionDeclaration);
  2086. if (funcNode) {
  2087. selector.funcInfo = AstHelper.GetCurNodeCallInfoByFuncDeclaration(selector.ast, funcNode, funcNode.parent);
  2088. }
  2089. else {
  2090. funcNode = AstHelper.FindParentNode(selector.defineNode.parent, AstNode.FunctionExpression);
  2091. if (funcNode) {
  2092. selector.funcInfo = AstHelper.GetCurNodeCallInfoByFuncExpression(selector.ast, funcNode, funcNode.parent);
  2093. }
  2094. else {
  2095. selector.funcInfo = {
  2096. name: "全局",
  2097. member: "",
  2098. kind: "global",
  2099. node: null
  2100. }
  2101. }
  2102. }
  2103. //查找selector操作类别
  2104. let funcExpressionNode = AstHelper.FindParentNodeByTypes(selector.defineNode.parent, AstHelper.M_FuncExpression);
  2105. //todo:跟踪选择器对应变量的使用情况...
  2106. let k = {};
  2107. //查找最先满足条件的父级节点
  2108. let findNode = AstHelper.FindParentNodeByTypes(selector.defineNode.parent, this._selectorParentNodeType, funcExpressionNode);
  2109. if (findNode == null) {
  2110. k = this.#GetMemberCallType(selector.defineNode);
  2111. }
  2112. else if (findNode.type == AstNode.AssignmentExpression) {
  2113. if (AstHelper.IsInDomainNode(selector.defineNode, findNode.right, findNode)) {
  2114. //在赋值表达式的右侧出现
  2115. //xxx=$("#con").width() + 100;
  2116. k.kind = "获取属性/元素";
  2117. }
  2118. else {
  2119. //如果是在左侧,则需要判断是否作为调用的参数出现
  2120. let isPars = AstHelper.IsInParasNode(selector.defineNode, findNode);
  2121. if (isPars) {
  2122. //getObj($("#name").val()).name = "...";
  2123. k.kind = "获取属性/元素";
  2124. }
  2125. else {
  2126. //判断是否onchange类似赋值处理...
  2127. if (AstHelper.IsArrayEleNode(selector.defineNode, findNode)) {
  2128. //xxx.info[$("#name").val()] = "..."
  2129. k.kind = "获取属性/元素";
  2130. } else {
  2131. if (findNode.left.property?.name.indexOf("on") == 0) {
  2132. //document.getElementBy...("").onclick = ()=>{...}
  2133. k.kindDes = findNode.left.property.name.replace("on", "");
  2134. k.kind = "事件处理";
  2135. }
  2136. else {
  2137. if (selector.defineNode.parent.property.name == "style") {
  2138. //document.getElementBy...("").style.width = "100px;"
  2139. k.kind = "修改样式";
  2140. }
  2141. else {
  2142. k.kind = "修改属性";
  2143. }
  2144. }
  2145. }
  2146. }
  2147. }
  2148. }
  2149. else if (findNode.type == AstNode.VariableDeclarator) {
  2150. //let xx = $("#name").val();
  2151. //let xx = data[$("#name").val()];
  2152. k.kind = "获取属性/元素";
  2153. }
  2154. else if (findNode.type == AstNode.ObjectProperty) {
  2155. //{aa: $("#name").val()}
  2156. k.kind = "获取属性/元素";
  2157. }
  2158. else if (findNode.type == AstNode.ExpressionStatement) {
  2159. if (AstHelper.IsInParasNode(selector.defineNode, findNode)) {
  2160. //call($("#name").val(),"...");
  2161. k.kind = "获取属性/元素";
  2162. }
  2163. else {
  2164. if (findNode.expression.type == AstNode.AwaitExpression) {
  2165. //这种情况可能不存在,兼容处理...
  2166. if (findNode.expression.argument.callee.type == AstNode.MemberExpression) {
  2167. k = this.#GetMemberCallType(findNode.expression.argument.callee.property);
  2168. }
  2169. else {
  2170. k = this.#GetMemberCallType(findNode.expression.argument.callee);
  2171. }
  2172. }
  2173. else if (findNode.expression.type == AstNode.LogicalExpression) {
  2174. //isOk && $("#name").on("click",function(){...})
  2175. if (findNode.expression.right.callee?.type == AstNode.MemberExpression) {
  2176. k = this.#GetMemberCallType(findNode.expression.right.callee.property);
  2177. }
  2178. else {
  2179. k.kind = "获取属性/元素";
  2180. }
  2181. }
  2182. else {
  2183. //$("#name").change(function () { ...})
  2184. k = this.#GetMemberCallType(findNode.expression.callee.property);
  2185. }
  2186. }
  2187. }
  2188. else {
  2189. debugger;
  2190. console.log("SelectorBaseCheck中发现未能识别的节点类型" + findNode.type);
  2191. }
  2192. selector.kind = k.kind;
  2193. selector.kindDes = k.kindDes;
  2194. }
  2195. }
  2196. Checked() {
  2197. this.#SelectorBaseCheck();
  2198. }
  2199. get Result() {
  2200. return {
  2201. AllSelectorList: this._selectorList,
  2202. SelectorList: function (fileName) {
  2203. return this.AllSelectorList.filter((x) => x.file == fileName);
  2204. },
  2205. }
  2206. }
  2207. }
  2208. window.AstParserPlugin_Selector = AstParserPlugin_Selector;
  2209. class AstParser {
  2210. _ast = [];//_ast可以是当对象,也可以是一个ast的数组 {type:"File",start:"0",end:"", program:"",file:""}
  2211. _regPlugins = [];
  2212. constructor() {
  2213. }
  2214. /**
  2215. * Ast属性
  2216. */
  2217. get Ast() {
  2218. return this._ast;
  2219. }
  2220. /**
  2221. * 创建解析对象
  2222. * @param {any} ast
  2223. * @returns
  2224. */
  2225. static CreateParser(ast) {
  2226. let parser = new AstParser();
  2227. parser.Insert(ast);
  2228. return parser;
  2229. }
  2230. /**
  2231. * 清除ast
  2232. */
  2233. Clear() {
  2234. this._ast = [];
  2235. }
  2236. Insert(ast, fileName) {
  2237. if (!ast) return;
  2238. if (fileName) ast.file = fileName;
  2239. ast.root = true;
  2240. this._ast.push(ast);// = ast;
  2241. }
  2242. /**
  2243. * 查找指定函数引用
  2244. * @param {any} funcName
  2245. */
  2246. GetFuncCall(funcName) {
  2247. let ret = [];
  2248. let option = {
  2249. callExp: (ast, pName, curNode, parent, des, depth) => {
  2250. let callee = curNode.callee;
  2251. if (callee.type == AstNode.Identifier) {//MemberExpression //对象成员调用
  2252. //函数调用
  2253. if (callee.name == funcName) {
  2254. let curInfo = AstHelper.GetCurNodeCallInfoByCallExpression(ast, curNode, parent);
  2255. ret.push({
  2256. file: ast.file,
  2257. class: "",
  2258. name: funcName,
  2259. start: curInfo.start,
  2260. end: curInfo.end,
  2261. node: curInfo.node,
  2262. called: [],
  2263. })
  2264. }
  2265. }
  2266. }
  2267. }
  2268. this.#StartAstParser(option);
  2269. return ret;
  2270. }
  2271. /**
  2272. * 查找指定函数的定义
  2273. * @param {any} funcName
  2274. */
  2275. GetFuncDefine(funcName) {
  2276. let ret = [];
  2277. let option = {
  2278. funcDec: (ast, pName, curNode, parent, des, depth) => {
  2279. let id = curNode.id;
  2280. if (id.name == funcName) {
  2281. let curInfo = AstHelper.GetCurNodeCallInfoByFuncDeclaration(ast, curNode, parent);
  2282. ret.push({
  2283. file: ast.file,
  2284. class: "",
  2285. name: funcName,
  2286. start: curInfo.start,
  2287. end: curInfo.end,
  2288. node: curInfo.node,
  2289. called: [],
  2290. })
  2291. }
  2292. }
  2293. }
  2294. this.#StartAstParser(option);
  2295. return ret;
  2296. }
  2297. /**
  2298. * 开始ast解析
  2299. * @param {any} option
  2300. */
  2301. #StartAstParser(option, useBuffer) {
  2302. if (Array.isArray(this._ast)) {
  2303. //如果是数组,则查找多个ast
  2304. this._ast.forEach((astItem) => {
  2305. let curAst = astItem.ast ?? astItem;
  2306. curAst.file = astItem.file;
  2307. curAst.root = true;
  2308. AstHelper.TraverseNode(curAst, curAst, null, option, 0, useBuffer);
  2309. });
  2310. }
  2311. else {
  2312. let curAst = this._ast.ast ?? this._ast;
  2313. curAst.file = this._ast.file;
  2314. curAst.root = true;
  2315. AstHelper.TraverseNode(this._ast.ast ?? this._ast, this._ast.ast ?? this._ast, null, option, 0, useBuffer);
  2316. }
  2317. }
  2318. //克隆对象
  2319. #DeepClone(obj) {
  2320. if (obj === null || typeof obj !== 'object') {
  2321. return obj;
  2322. }
  2323. let clone = Array.isArray(obj) ? [] : {};
  2324. for (let key in obj) {
  2325. if (key == "parent" || key == "idNode" || key == "defineNode") continue;
  2326. if (obj.hasOwnProperty(key)) {
  2327. clone[key] = this.#DeepClone(obj[key]);
  2328. }
  2329. }
  2330. return clone;
  2331. }
  2332. /**
  2333. * 解析子对象列表
  2334. * @param {any} subNode
  2335. * @param {any} option
  2336. */
  2337. AnalyzeSubNodeObjectList(subNode, option) {
  2338. let parseNode = this.#DeepClone(subNode);
  2339. let subParser = AstParser.CreateParser();
  2340. subParser.Insert(parseNode);
  2341. if (!option) option = {
  2342. func: {
  2343. enabled: true,//解析函数
  2344. analyzeCallRel: true,//解析调用关系
  2345. },
  2346. globalVar: {
  2347. enabled: true,//解析变量
  2348. analyzeCallRel: true,//解析调用关系
  2349. },
  2350. selector: {//选择器解析
  2351. enabled: true,
  2352. }
  2353. }
  2354. return subParser.AnalyzeObjectList(option);
  2355. }
  2356. #ConfigAnalyzePlugin(analyzeOption, type, plugin) {
  2357. let regInfo = plugin.RegInfo;
  2358. switch (regInfo[type]) {
  2359. case AstNode.Program: {
  2360. if (!analyzeOption.program) analyzeOption.program = [];
  2361. analyzeOption.program.push(plugin[type].bind(plugin));
  2362. break;
  2363. }
  2364. case AstNode.ExpressionStatement: {
  2365. if (!analyzeOption.expression) analyzeOption.expression = [];
  2366. analyzeOption.expression.push(plugin[type].bind(plugin));
  2367. break;
  2368. }
  2369. case AstNode.CallExpression: {
  2370. if (!analyzeOption.callExp) analyzeOption.callExp = [];
  2371. analyzeOption.callExp.push(plugin[type].bind(plugin));
  2372. break;
  2373. }
  2374. case AstNode.FunctionExpression: {
  2375. if (!analyzeOption.funcExp) analyzeOption.funcExp = [];
  2376. analyzeOption.funcExp.push(plugin[type].bind(plugin));
  2377. break;
  2378. }
  2379. case AstNode.BlockStatement: {
  2380. if (!analyzeOption.block) analyzeOption.block = [];
  2381. analyzeOption.block.push(plugin[type].bind(plugin));
  2382. break;
  2383. }
  2384. case AstNode.FunctionDeclaration: {
  2385. if (!analyzeOption.funcDec) analyzeOption.funcDec = [];
  2386. analyzeOption.funcDec.push(plugin[type].bind(plugin));
  2387. break;
  2388. }
  2389. case AstNode.Identifier: {
  2390. if (!analyzeOption.identifier) analyzeOption.identifier = [];
  2391. analyzeOption.identifier.push(plugin[type].bind(plugin));
  2392. break;
  2393. }
  2394. case AstNode.ReturnStatement: {
  2395. if (!analyzeOption.returnstatement) analyzeOption.returnstatement = [];
  2396. analyzeOption.returnstatement.push(plugin[type].bind(plugin));
  2397. break;
  2398. }
  2399. case AstNode.CatchClause: {
  2400. if (!analyzeOption.catchclause) analyzeOption.catchclause = [];
  2401. analyzeOption.catchclause.push(plugin[type].bind(plugin));
  2402. break;
  2403. }
  2404. }
  2405. }
  2406. //注册插件
  2407. RegPlugin(pluginName, option) {
  2408. this._regPlugins.push({
  2409. name: pluginName,
  2410. option: option,
  2411. });
  2412. }
  2413. //清除插件
  2414. ClearPlugin() {
  2415. this._regPlugins = [];
  2416. }
  2417. //获取所有对象列表
  2418. AnalyzeObjectList(useBuffer) {
  2419. //{funcOption:{enabled:true},varOption:{enabled:true}}
  2420. console.time("AnalyzeObjectList");
  2421. let anaOption = {};
  2422. let parserPluginObj = [];
  2423. if (useBuffer == undefined) useBuffer = true;
  2424. //配置默认插件
  2425. if (this._regPlugins.length <= 0) {
  2426. this._regPlugins = [
  2427. { name: "AstParserPlugin_Func", option: null },
  2428. { name: "AstParserPlugin_GlobalVar", option: null },
  2429. { name: "AstParserPlugin_Selector", option: null },
  2430. ];
  2431. }
  2432. //实例化插件
  2433. for (let pluginInfo of this._regPlugins) {
  2434. parserPluginObj.push(new window[pluginInfo.name](this, pluginInfo.option));
  2435. }
  2436. //挂接插件接口
  2437. parserPluginObj.forEach((x) => {
  2438. this.#ConfigAnalyzePlugin(anaOption, "Parse", x);
  2439. });
  2440. //获取到对应的对象
  2441. this.#StartAstParser(anaOption, useBuffer);
  2442. anaOption = {};
  2443. parserPluginObj.forEach((x) => {
  2444. if (x.Checked) x.Checked();
  2445. });
  2446. anaOption = {};
  2447. //挂接插件接口
  2448. parserPluginObj.forEach((x) => {
  2449. this.#ConfigAnalyzePlugin(anaOption, "CallRelation", x);
  2450. });
  2451. //获取调用关系
  2452. this.#StartAstParser(anaOption, useBuffer);
  2453. console.timeEnd("AnalyzeObjectList");
  2454. //配置返回结果
  2455. let ret = {};
  2456. parserPluginObj.forEach((x) => {
  2457. ret = Object.assign({}, ret, x.Result);
  2458. });
  2459. parserPluginObj = null;
  2460. return ret;
  2461. }
  2462. /**
  2463. * 查找所有函数列表
  2464. */
  2465. GetAllFuncList(isFindCallRel) {
  2466. return this.AnalyzeObjectList({
  2467. func: {
  2468. enabled: true,
  2469. analyzeCallRel: isFindCallRel
  2470. }
  2471. });
  2472. }
  2473. /**
  2474. * 查找所有全局变量
  2475. */
  2476. GetAllGlobalVarList(isFindCallRel) {
  2477. return this.AnalyzeObjectList({
  2478. globalVar: {
  2479. enabled: true,
  2480. analyzeCallRel: isFindCallRel
  2481. }
  2482. });
  2483. }
  2484. /**
  2485. * 查找全局变量的引用
  2486. */
  2487. GetGlobalVarCall(varName) {
  2488. }
  2489. /**
  2490. * 查找所有类列表
  2491. */
  2492. GetAllClassList() {
  2493. }
  2494. /**
  2495. * 查找类成员列表
  2496. * @param {any} className
  2497. */
  2498. GetClassMemberList(className) {
  2499. }
  2500. /**
  2501. * 查找对象类成员定义
  2502. */
  2503. GetClassMemberDefine(className, memberName) {
  2504. }
  2505. /**
  2506. * 查找类成员引用
  2507. * @param {any} className
  2508. * @param {any} memberName
  2509. */
  2510. GetClassMemberCall(className, memberName) {
  2511. }
  2512. }