版博士V2.0程序
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

378 řádky
11 KiB

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