版博士V2.0程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/walker.js
  2. var WalkerBase = class {
  3. constructor() {
  4. this.should_skip = false;
  5. this.should_remove = false;
  6. this.replacement = null;
  7. this.context = {
  8. skip: () => this.should_skip = true,
  9. remove: () => this.should_remove = true,
  10. replace: (node) => this.replacement = node
  11. };
  12. }
  13. replace(parent, prop, index, node) {
  14. if (parent && prop) {
  15. if (index != null) {
  16. parent[prop][index] = node;
  17. } else {
  18. parent[prop] = node;
  19. }
  20. }
  21. }
  22. remove(parent, prop, index) {
  23. if (parent && prop) {
  24. if (index !== null && index !== void 0) {
  25. parent[prop].splice(index, 1);
  26. } else {
  27. delete parent[prop];
  28. }
  29. }
  30. }
  31. };
  32. // node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/sync.js
  33. var SyncWalker = class extends WalkerBase {
  34. constructor(enter, leave) {
  35. super();
  36. this.should_skip = false;
  37. this.should_remove = false;
  38. this.replacement = null;
  39. this.context = {
  40. skip: () => this.should_skip = true,
  41. remove: () => this.should_remove = true,
  42. replace: (node) => this.replacement = node
  43. };
  44. this.enter = enter;
  45. this.leave = leave;
  46. }
  47. visit(node, parent, prop, index) {
  48. if (node) {
  49. if (this.enter) {
  50. const _should_skip = this.should_skip;
  51. const _should_remove = this.should_remove;
  52. const _replacement = this.replacement;
  53. this.should_skip = false;
  54. this.should_remove = false;
  55. this.replacement = null;
  56. this.enter.call(this.context, node, parent, prop, index);
  57. if (this.replacement) {
  58. node = this.replacement;
  59. this.replace(parent, prop, index, node);
  60. }
  61. if (this.should_remove) {
  62. this.remove(parent, prop, index);
  63. }
  64. const skipped = this.should_skip;
  65. const removed = this.should_remove;
  66. this.should_skip = _should_skip;
  67. this.should_remove = _should_remove;
  68. this.replacement = _replacement;
  69. if (skipped)
  70. return node;
  71. if (removed)
  72. return null;
  73. }
  74. let key;
  75. for (key in node) {
  76. const value = node[key];
  77. if (value && typeof value === "object") {
  78. if (Array.isArray(value)) {
  79. const nodes = value;
  80. for (let i = 0; i < nodes.length; i += 1) {
  81. const item = nodes[i];
  82. if (isNode(item)) {
  83. if (!this.visit(item, node, key, i)) {
  84. i--;
  85. }
  86. }
  87. }
  88. } else if (isNode(value)) {
  89. this.visit(value, node, key, null);
  90. }
  91. }
  92. }
  93. if (this.leave) {
  94. const _replacement = this.replacement;
  95. const _should_remove = this.should_remove;
  96. this.replacement = null;
  97. this.should_remove = false;
  98. this.leave.call(this.context, node, parent, prop, index);
  99. if (this.replacement) {
  100. node = this.replacement;
  101. this.replace(parent, prop, index, node);
  102. }
  103. if (this.should_remove) {
  104. this.remove(parent, prop, index);
  105. }
  106. const removed = this.should_remove;
  107. this.replacement = _replacement;
  108. this.should_remove = _should_remove;
  109. if (removed)
  110. return null;
  111. }
  112. }
  113. return node;
  114. }
  115. };
  116. function isNode(value) {
  117. return value !== null && typeof value === "object" && "type" in value && typeof value.type === "string";
  118. }
  119. // node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/index.js
  120. function walk(ast, { enter, leave }) {
  121. const instance = new SyncWalker(enter, leave);
  122. return instance.visit(ast, null);
  123. }
  124. // src/index.ts
  125. import { isFunction as isFunction2 } from "@babel/types";
  126. // src/utils/babel.ts
  127. import { isFunction } from "@babel/types";
  128. import { parse } from "@babel/parser";
  129. var NEW_SCOPE = [
  130. "CatchClause",
  131. "ForInStatement",
  132. "ForOfStatement"
  133. ];
  134. var isNewScope = (node) => NEW_SCOPE.includes(node.type) || isFunction(node);
  135. function walkFunctionParams(node, onIdent) {
  136. for (const p of node.params) {
  137. for (const id of extractIdentifiers(p)) {
  138. onIdent(id);
  139. }
  140. }
  141. }
  142. function extractIdentifiers(param, nodes = []) {
  143. switch (param.type) {
  144. case "Identifier":
  145. nodes.push(param);
  146. break;
  147. case "MemberExpression": {
  148. let object = param;
  149. while (object.type === "MemberExpression") {
  150. object = object.object;
  151. }
  152. nodes.push(object);
  153. break;
  154. }
  155. case "ObjectPattern":
  156. for (const prop of param.properties) {
  157. if (prop.type === "RestElement") {
  158. extractIdentifiers(prop.argument, nodes);
  159. } else {
  160. extractIdentifiers(prop.value, nodes);
  161. }
  162. }
  163. break;
  164. case "ArrayPattern":
  165. param.elements.forEach((element) => {
  166. if (element)
  167. extractIdentifiers(element, nodes);
  168. });
  169. break;
  170. case "RestElement":
  171. extractIdentifiers(param.argument, nodes);
  172. break;
  173. case "AssignmentPattern":
  174. extractIdentifiers(param.left, nodes);
  175. break;
  176. }
  177. return nodes;
  178. }
  179. function babelParse(code, filename, parserPlugins = []) {
  180. const plugins = parserPlugins || [];
  181. if (filename) {
  182. if (/\.tsx?$/.test(filename))
  183. plugins.push("typescript");
  184. if (filename.endsWith("x"))
  185. plugins.push("jsx");
  186. }
  187. const ast = parse(code, {
  188. sourceType: "module",
  189. plugins
  190. });
  191. return ast;
  192. }
  193. function walkVariableDeclaration(stmt, register) {
  194. if (stmt.declare)
  195. return;
  196. for (const decl of stmt.declarations) {
  197. for (const id of extractIdentifiers(decl.id)) {
  198. register(id);
  199. }
  200. }
  201. }
  202. function walkNewIdentifier(node, register) {
  203. if (node.type === "ExportNamedDeclaration" && node.declaration) {
  204. node = node.declaration;
  205. }
  206. if (node.type === "VariableDeclaration") {
  207. walkVariableDeclaration(node, register);
  208. } else if (node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") {
  209. if (node.declare || !node.id)
  210. return;
  211. register(node.id);
  212. } else if (node.type === "ExportNamedDeclaration" && node.declaration && node.declaration.type === "VariableDeclaration") {
  213. walkVariableDeclaration(node.declaration, register);
  214. }
  215. }
  216. // src/index.ts
  217. var walk2 = (code, walkHooks, { filename, parserPlugins } = {}) => {
  218. const ast = babelParse(code, filename, parserPlugins);
  219. walkAST(ast.program, walkHooks);
  220. return ast;
  221. };
  222. var walkAST = (node, { enter, leave, enterAfter, leaveAfter }) => {
  223. let currentScope = {};
  224. const scopeStack = [currentScope];
  225. const ast = Array.isArray(node) ? { type: "Program", body: node } : node;
  226. walk(ast, {
  227. enter(node2, parent, ...args) {
  228. const { scopeCtx, walkerCtx, isSkip, isRemoved, getNode } = getHookContext(this, node2, [parent, ...args]);
  229. enter == null ? void 0 : enter.call({ ...scopeCtx(), ...walkerCtx }, node2);
  230. node2 = getNode();
  231. if (!isSkip() && !isRemoved()) {
  232. enterNode(node2, parent);
  233. enterAfter == null ? void 0 : enterAfter.call(scopeCtx(), node2);
  234. }
  235. },
  236. leave(node2, parent, ...args) {
  237. const { scopeCtx, walkerCtx, isSkip, isRemoved, getNode } = getHookContext(this, node2, [parent, ...args]);
  238. leave == null ? void 0 : leave.call({ ...scopeCtx(), ...walkerCtx }, node2);
  239. node2 = getNode();
  240. if (!isSkip() && !isRemoved()) {
  241. leaveNode(node2, parent);
  242. leaveAfter == null ? void 0 : leaveAfter.call(scopeCtx(), node2);
  243. }
  244. }
  245. });
  246. function getHookContext(ctx, node2, [parent, key, index]) {
  247. const scopeCtx = () => ({
  248. parent,
  249. key,
  250. index,
  251. scope: scopeStack.reduce((prev, curr) => ({ ...prev, ...curr }), {}),
  252. scopes: scopeStack,
  253. level: scopeStack.length
  254. });
  255. let isSkip = false;
  256. let isRemoved = false;
  257. let newNode = node2;
  258. const walkerCtx = {
  259. skip() {
  260. isSkip = true;
  261. ctx.skip();
  262. },
  263. replace(node3) {
  264. newNode = node3;
  265. },
  266. remove() {
  267. isRemoved = true;
  268. }
  269. };
  270. return {
  271. scopeCtx,
  272. walkerCtx,
  273. isSkip: () => isSkip,
  274. isRemoved: () => isRemoved,
  275. getNode: () => newNode
  276. };
  277. }
  278. function enterNode(node2, parent) {
  279. if (isNewScope(node2) || node2.type === "BlockStatement" && !isNewScope(parent))
  280. scopeStack.push(currentScope = {});
  281. if (isFunction2(node2)) {
  282. walkFunctionParams(node2, registerBinding);
  283. } else if (node2.type === "CatchClause" && node2.param && node2.param.type === "Identifier")
  284. registerBinding(node2.param);
  285. if (node2.type === "BlockStatement" || node2.type === "Program") {
  286. for (const stmt of node2.body) {
  287. if (stmt.type === "VariableDeclaration" && stmt.kind === "var") {
  288. walkVariableDeclaration(stmt, registerBinding);
  289. } else if (stmt.type === "FunctionDeclaration" && stmt.id) {
  290. registerBinding(stmt.id);
  291. }
  292. }
  293. }
  294. }
  295. function leaveNode(node2, parent) {
  296. if (isNewScope(node2) || node2.type === "BlockStatement" && !isNewScope(parent)) {
  297. scopeStack.pop();
  298. currentScope = scopeStack[scopeStack.length - 1];
  299. }
  300. walkNewIdentifier(node2, registerBinding);
  301. }
  302. function registerBinding(id) {
  303. if (currentScope) {
  304. currentScope[id.name] = id;
  305. } else {
  306. error(
  307. "registerBinding called without active scope, something is wrong.",
  308. id
  309. );
  310. }
  311. }
  312. function error(msg, node2) {
  313. const e = new Error(msg);
  314. e.node = node2;
  315. throw e;
  316. }
  317. };
  318. var getRootScope = (nodes) => {
  319. const scope = {};
  320. for (const node of nodes) {
  321. walkNewIdentifier(node, (id) => {
  322. scope[id.name] = id;
  323. });
  324. }
  325. return scope;
  326. };
  327. export {
  328. babelParse,
  329. extractIdentifiers,
  330. getRootScope,
  331. isNewScope,
  332. walk2 as walk,
  333. walkAST,
  334. walkFunctionParams,
  335. walkNewIdentifier,
  336. walkVariableDeclaration
  337. };