jsTree.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. window.ShowASTTree = function (data, container) {
  2. container.innerHTML = "";
  3. let fragment = document.createDocumentFragment();
  4. let ul = document.createElement("ul");
  5. fragment.appendChild(ul);
  6. CreateTree(ul, data);
  7. container.appendChild(fragment);
  8. container.onclick = function (e) {
  9. let target = e.target;
  10. if (target.tagName == "SPAN") {
  11. let child = target.parentNode.querySelector("ul");
  12. if (child) {
  13. if (child.style.display == "none") {
  14. child.style.display = "block";
  15. target.parentNode.classList.remove("collapse-node");
  16. target.parentNode.classList.add("expand-node");
  17. } else {
  18. child.style.display = "none";
  19. target.parentNode.classList.remove("expand-node");
  20. target.parentNode.classList.add("collapse-node");
  21. }
  22. }
  23. }
  24. };
  25. }
  26. function scrollTo(ele) {
  27. $("#astJson")[0].scrollTo(0, 0);
  28. let eInfo = ele.getClientRects()
  29. let pInfo = $("#astJson")[0].getClientRects();
  30. $("#astJson")[0].scrollTo(eInfo[0].x - pInfo[0].x, eInfo[0].y - pInfo[0].y)
  31. }
  32. // 节点的翻译
  33. window.typeDescriptions = {
  34. Program: '程序',
  35. VariableDeclaration: '变量声明',
  36. VariableDeclarator: '变量声明符',
  37. Identifier: '标识符',
  38. Literal: '字面量',
  39. FunctionDeclaration: '函数声明',
  40. FunctionExpression: '函数表达式',
  41. ArrowFunctionExpression: '箭头函数表达式',
  42. BlockStatement: '块语句',
  43. ExpressionStatement: '表达式语句',
  44. IfStatement: '条件语句',
  45. SwitchStatement: '选择语句',
  46. SwitchCase: '选择语句块',
  47. WhileStatement: '循环语句',
  48. DoWhileStatement: '循环语句',
  49. ForStatement: '循环语句',
  50. ForInStatement: '循环语句',
  51. ForOfStatement: '循环语句',
  52. BreakStatement: '跳出语句',
  53. ContinueStatement: '继续语句',
  54. ReturnStatement: '返回语句',
  55. ThrowStatement: '抛出异常语句',
  56. TryStatement: '异常捕获语句',
  57. CatchClause: '异常捕获块',
  58. FinallyClause: '最终执行块',
  59. WithStatement: 'with语句',
  60. LabeledStatement: '标签语句',
  61. DebuggerStatement: '调试语句',
  62. EmptyStatement: '空语句',
  63. ThisExpression: 'this表达式',
  64. ArrayExpression: '数组表达式',
  65. ObjectExpression: '对象表达式',
  66. Property: '属性',
  67. UnaryExpression: '一元表达式',
  68. BinaryExpression: '二元表达式',
  69. LogicalExpression: '逻辑表达式',
  70. AssignmentExpression: '赋值表达式',
  71. UpdateExpression: '更新表达式',
  72. MemberExpression: '成员表达式',
  73. ConditionalExpression: '条件表达式',
  74. CallExpression: '调用表达式',
  75. NewExpression: 'new表达式',
  76. SequenceExpression: '序列表达式',
  77. TemplateLiteral: '模板文字',
  78. TaggedTemplateExpression: '标记模板文字表达式',
  79. TemplateElement: '模板元素',
  80. SpreadElement: '扩展元素',
  81. ObjectPattern: '对象模式',
  82. ArrayPattern: '数组模式',
  83. RestElement: '剩余元素',
  84. AssignmentPattern: '赋值模式',
  85. ClassDeclaration: '类声明',
  86. ClassExpression: '类表达式',
  87. ClassBody: '类主体',
  88. MethodDefinition: '方法定义',
  89. ImportDeclaration: '导入声明',
  90. ImportSpecifier: '导入说明符',
  91. ImportDefaultSpecifier: '默认导入说明符',
  92. ImportNamespaceSpecifier: '命名空间导入说明符',
  93. ExportNamedDeclaration: '导出声明',
  94. ExportDefaultDeclaration: '默认导出声明',
  95. ExportAllDeclaration: '全部导出声明',
  96. Super: 'super关键字',
  97. MetaProperty: '元属性',
  98. AwaitExpression: 'await表达式',
  99. YieldExpression: 'yield表达式',
  100. JSXElement: 'JSX元素',
  101. JSXFragment: 'JSX片段',
  102. JSXOpeningElement: 'JSX打开元素',
  103. JSXClosingElement: 'JSX关闭元素',
  104. JSXAttribute: 'JSX属性',
  105. JSXText: 'JSX文本',
  106. JSXExpressionContainer: 'JSX表达式容器',
  107. JSXSpreadAttribute: 'JSX扩展属性',
  108. JSXOpeningFragment: 'JSX打开片段',
  109. JSXClosingFragment: 'JSX关闭片段',
  110. ChainExpression: '链式表达式',
  111. ImportExpression: '导入表达式',
  112. OptionalMemberExpression: '可选成员表达式',
  113. OptionalCallExpression: '可选调用表达式',
  114. PrivateIdentifier: '私有标识符',
  115. StringLiteral: '字符串字面量',
  116. NumericLiteral: '数字字面量',
  117. BooleanLiteral: '布尔字面量',
  118. NullLiteral: 'null字面量',
  119. PrivateName: '私有名称',
  120. BigIntLiteral: '大整数字面量',
  121. RegExpLiteral: '正则表达式字面量',
  122. ArrowParameterPlaceHolder: '箭头参数占位符',
  123. };
  124. function CreateTree(parentContainer, data) {
  125. for (let key in data) {
  126. let item = data[key];
  127. if (key == "loc") continue;
  128. if (key == "start") continue;
  129. if (key == "end") continue;
  130. if (key == "extra") continue;
  131. if (key == "directives") continue;
  132. if (key == "__clone") continue;
  133. if (key == "iid") continue;
  134. if (key == "parent") continue;
  135. if (key == "trailingComments") continue;
  136. if (key == "leadingComments") continue;
  137. if (item) {
  138. if (typeof item === 'object') {
  139. let li = document.createElement("li");
  140. li.classList.add("parent-node");
  141. let showText = "";
  142. if (Array.isArray(data)) {
  143. if (window.typeDescriptions[item.type]) {
  144. showText = `${item.type}(${window.typeDescriptions[item.type]})`;
  145. } else {
  146. showText = item.type;
  147. }
  148. } else {
  149. showText = key;
  150. if (item.type) {
  151. showText = `${key}:${item.type}(${window.typeDescriptions[item.type]})`;
  152. }
  153. }
  154. if (!showText) continue
  155. li.innerHTML = `<span class="tree-icon" id="${item.iid}"></span><span class="node-text">${showText}</span>`;
  156. let ul = document.createElement("ul");
  157. parentContainer.appendChild(li);
  158. li.appendChild(ul);
  159. CreateTree(ul, item);
  160. } else {
  161. let li = document.createElement("li");
  162. let showText = "";
  163. if (key == "type") {
  164. showText = `${key + ":" + item}(${window.typeDescriptions[item]})`;
  165. } else {
  166. showText = key + ":" + item;
  167. }
  168. if (!showText) continue
  169. li.innerHTML = `<span class="tree-icon"></span><span class="node-text">${showText}</span>`;
  170. li.classList.add("last-node");
  171. parentContainer.appendChild(li);
  172. }
  173. }
  174. }
  175. }