版博士V2.0程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

350 rader
10 KiB

  1. import {
  2. isTs
  3. } from "./chunk-X26ALPRY.mjs";
  4. import {
  5. REGEX_LANG_JSX
  6. } from "./chunk-TDSRBKSQ.mjs";
  7. // src/ast.ts
  8. import { babelParse as _babelParse, walkIdentifiers } from "@vue/compiler-sfc";
  9. // ../../node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/walker.js
  10. var WalkerBase = class {
  11. constructor() {
  12. this.should_skip = false;
  13. this.should_remove = false;
  14. this.replacement = null;
  15. this.context = {
  16. skip: () => this.should_skip = true,
  17. remove: () => this.should_remove = true,
  18. replace: (node) => this.replacement = node
  19. };
  20. }
  21. /**
  22. * @template {Node} Parent
  23. * @param {Parent | null | undefined} parent
  24. * @param {keyof Parent | null | undefined} prop
  25. * @param {number | null | undefined} index
  26. * @param {Node} node
  27. */
  28. replace(parent, prop, index, node) {
  29. if (parent && prop) {
  30. if (index != null) {
  31. parent[prop][index] = node;
  32. } else {
  33. parent[prop] = node;
  34. }
  35. }
  36. }
  37. /**
  38. * @template {Node} Parent
  39. * @param {Parent | null | undefined} parent
  40. * @param {keyof Parent | null | undefined} prop
  41. * @param {number | null | undefined} index
  42. */
  43. remove(parent, prop, index) {
  44. if (parent && prop) {
  45. if (index !== null && index !== void 0) {
  46. parent[prop].splice(index, 1);
  47. } else {
  48. delete parent[prop];
  49. }
  50. }
  51. }
  52. };
  53. // ../../node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/sync.js
  54. var SyncWalker = class extends WalkerBase {
  55. /**
  56. *
  57. * @param {SyncHandler} [enter]
  58. * @param {SyncHandler} [leave]
  59. */
  60. constructor(enter, leave) {
  61. super();
  62. this.should_skip = false;
  63. this.should_remove = false;
  64. this.replacement = null;
  65. this.context = {
  66. skip: () => this.should_skip = true,
  67. remove: () => this.should_remove = true,
  68. replace: (node) => this.replacement = node
  69. };
  70. this.enter = enter;
  71. this.leave = leave;
  72. }
  73. /**
  74. * @template {Node} Parent
  75. * @param {Node} node
  76. * @param {Parent | null} parent
  77. * @param {keyof Parent} [prop]
  78. * @param {number | null} [index]
  79. * @returns {Node | null}
  80. */
  81. visit(node, parent, prop, index) {
  82. if (node) {
  83. if (this.enter) {
  84. const _should_skip = this.should_skip;
  85. const _should_remove = this.should_remove;
  86. const _replacement = this.replacement;
  87. this.should_skip = false;
  88. this.should_remove = false;
  89. this.replacement = null;
  90. this.enter.call(this.context, node, parent, prop, index);
  91. if (this.replacement) {
  92. node = this.replacement;
  93. this.replace(parent, prop, index, node);
  94. }
  95. if (this.should_remove) {
  96. this.remove(parent, prop, index);
  97. }
  98. const skipped = this.should_skip;
  99. const removed = this.should_remove;
  100. this.should_skip = _should_skip;
  101. this.should_remove = _should_remove;
  102. this.replacement = _replacement;
  103. if (skipped)
  104. return node;
  105. if (removed)
  106. return null;
  107. }
  108. let key;
  109. for (key in node) {
  110. const value = node[key];
  111. if (value && typeof value === "object") {
  112. if (Array.isArray(value)) {
  113. const nodes = (
  114. /** @type {Array<unknown>} */
  115. value
  116. );
  117. for (let i = 0; i < nodes.length; i += 1) {
  118. const item = nodes[i];
  119. if (isNode(item)) {
  120. if (!this.visit(item, node, key, i)) {
  121. i--;
  122. }
  123. }
  124. }
  125. } else if (isNode(value)) {
  126. this.visit(value, node, key, null);
  127. }
  128. }
  129. }
  130. if (this.leave) {
  131. const _replacement = this.replacement;
  132. const _should_remove = this.should_remove;
  133. this.replacement = null;
  134. this.should_remove = false;
  135. this.leave.call(this.context, node, parent, prop, index);
  136. if (this.replacement) {
  137. node = this.replacement;
  138. this.replace(parent, prop, index, node);
  139. }
  140. if (this.should_remove) {
  141. this.remove(parent, prop, index);
  142. }
  143. const removed = this.should_remove;
  144. this.replacement = _replacement;
  145. this.should_remove = _should_remove;
  146. if (removed)
  147. return null;
  148. }
  149. }
  150. return node;
  151. }
  152. };
  153. function isNode(value) {
  154. return value !== null && typeof value === "object" && "type" in value && typeof value.type === "string";
  155. }
  156. // ../../node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/index.js
  157. function walk(ast, { enter, leave }) {
  158. const instance = new SyncWalker(enter, leave);
  159. return instance.visit(ast, null);
  160. }
  161. // src/ast.ts
  162. function babelParse(code, lang, options = {}) {
  163. const plugins = [...options.plugins || []];
  164. if (isTs(lang)) {
  165. plugins.push(["typescript", { dts: lang === "dts" }]);
  166. if (REGEX_LANG_JSX.test(lang))
  167. plugins.push("jsx");
  168. if (!plugins.includes("decorators"))
  169. plugins.push("decorators-legacy");
  170. } else {
  171. plugins.push("jsx");
  172. }
  173. const { program } = _babelParse(code, {
  174. sourceType: "module",
  175. plugins,
  176. ...options
  177. });
  178. return program;
  179. }
  180. function isCallOf(node, test) {
  181. return !!(node && node.type === "CallExpression" && node.callee.type === "Identifier" && (typeof test === "string" ? node.callee.name === test : Array.isArray(test) ? test.includes(node.callee.name) : test(node.callee.name)));
  182. }
  183. function checkInvalidScopeReference(node, method, setupBindings) {
  184. if (!node)
  185. return;
  186. walkIdentifiers(node, (id) => {
  187. if (setupBindings.includes(id.name))
  188. throw new SyntaxError(
  189. `\`${method}()\` in <script setup> cannot reference locally declared variables (${id.name}) because it will be hoisted outside of the setup() function.`
  190. );
  191. });
  192. }
  193. function isStaticExpression(node, options = {}) {
  194. var _a;
  195. const { magicComment, fn, object, objectMethod, array, unary } = options;
  196. if (magicComment && ((_a = node.leadingComments) == null ? void 0 : _a.some(
  197. (comment) => comment.value.trim() === magicComment
  198. )))
  199. return true;
  200. else if (fn && isFunctionType(node))
  201. return true;
  202. switch (node.type) {
  203. case "UnaryExpression":
  204. return !!unary && isStaticExpression(node.argument, options);
  205. case "LogicalExpression":
  206. case "BinaryExpression":
  207. return isStaticExpression(node.left, options) && isStaticExpression(node.right, options);
  208. case "ConditionalExpression":
  209. return isStaticExpression(node.test, options) && isStaticExpression(node.consequent, options) && isStaticExpression(node.alternate, options);
  210. case "SequenceExpression":
  211. case "TemplateLiteral":
  212. return node.expressions.every((expr) => isStaticExpression(expr, options));
  213. case "ArrayExpression":
  214. return !!array && node.elements.every(
  215. (element) => element && isStaticExpression(element, options)
  216. );
  217. case "ObjectExpression":
  218. return !!object && node.properties.every((prop) => {
  219. if (prop.type === "SpreadElement") {
  220. return prop.argument.type === "ObjectExpression" && isStaticExpression(prop.argument, options);
  221. } else if (!isLiteralType(prop.key) && prop.computed) {
  222. return false;
  223. } else if (prop.type === "ObjectProperty" && !isStaticExpression(prop.value, options)) {
  224. return false;
  225. }
  226. if (prop.type === "ObjectMethod" && !objectMethod) {
  227. return false;
  228. }
  229. return true;
  230. });
  231. case "ParenthesizedExpression":
  232. case "TSNonNullExpression":
  233. case "TSAsExpression":
  234. case "TSTypeAssertion":
  235. return isStaticExpression(node.expression, options);
  236. }
  237. if (isLiteralType(node))
  238. return true;
  239. return false;
  240. }
  241. function isLiteralType(node) {
  242. return node.type.endsWith("Literal");
  243. }
  244. function resolveTemplateLiteral(node) {
  245. return node.quasis.reduce((prev, curr, idx) => {
  246. if (node.expressions[idx]) {
  247. return prev + curr.value.cooked + resolveLiteral(node.expressions[idx]);
  248. }
  249. return prev + curr.value.cooked;
  250. }, "");
  251. }
  252. function resolveLiteral(node) {
  253. switch (node.type) {
  254. case "TemplateLiteral":
  255. return resolveTemplateLiteral(node);
  256. case "NullLiteral":
  257. return null;
  258. case "BigIntLiteral":
  259. return BigInt(node.value);
  260. case "RegExpLiteral":
  261. return new RegExp(node.pattern, node.flags);
  262. case "BooleanLiteral":
  263. case "NumericLiteral":
  264. case "StringLiteral":
  265. return node.value;
  266. }
  267. return void 0;
  268. }
  269. function isStaticObjectKey(node) {
  270. return node.properties.every((prop) => {
  271. if (prop.type === "SpreadElement") {
  272. return prop.argument.type === "ObjectExpression" && isStaticObjectKey(prop.argument);
  273. }
  274. return !prop.computed || isLiteralType(prop.key);
  275. });
  276. }
  277. function resolveObjectExpression(node) {
  278. const maps = {};
  279. for (const property of node.properties) {
  280. if (property.type === "SpreadElement") {
  281. if (property.argument.type !== "ObjectExpression")
  282. return void 0;
  283. Object.assign(maps, resolveObjectExpression(property.argument));
  284. } else {
  285. const key = resolveObjectKey(property.key, property.computed, false);
  286. maps[key] = property;
  287. }
  288. }
  289. return maps;
  290. }
  291. function resolveObjectKey(node, computed = false, raw = true) {
  292. switch (node.type) {
  293. case "StringLiteral":
  294. case "NumericLiteral":
  295. return raw ? node.extra.raw : node.value;
  296. case "Identifier":
  297. if (!computed)
  298. return raw ? `'${node.name}'` : node.name;
  299. default:
  300. throw new SyntaxError(`Unexpected node type: ${node.type}`);
  301. }
  302. }
  303. function walkAST(node, options) {
  304. return walk(node, options);
  305. }
  306. function isFunctionType(node) {
  307. return /Function(?:Expression|Declaration)$|Method$/.test(node.type);
  308. }
  309. var TS_NODE_TYPES = [
  310. "TSAsExpression",
  311. // foo as number
  312. "TSTypeAssertion",
  313. // (<number>foo)
  314. "TSNonNullExpression",
  315. // foo!
  316. "TSInstantiationExpression",
  317. // foo<string>
  318. "TSSatisfiesExpression"
  319. // foo satisfies T
  320. ];
  321. function unwrapTSNode(node) {
  322. if (TS_NODE_TYPES.includes(node.type)) {
  323. return unwrapTSNode(node.expression);
  324. } else {
  325. return node;
  326. }
  327. }
  328. export {
  329. babelParse,
  330. isCallOf,
  331. checkInvalidScopeReference,
  332. isStaticExpression,
  333. isLiteralType,
  334. resolveTemplateLiteral,
  335. resolveLiteral,
  336. isStaticObjectKey,
  337. resolveObjectExpression,
  338. resolveObjectKey,
  339. walkAST,
  340. isFunctionType,
  341. TS_NODE_TYPES,
  342. unwrapTSNode
  343. };