版博士V2.0程序
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

chunk-VAMOGGAI.mjs 1.4 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // src/core/index.ts
  2. import {
  3. DEFINE_RENDER,
  4. MagicString,
  5. babelParse,
  6. getLang,
  7. getTransformResult,
  8. isCallOf,
  9. isFunctionType,
  10. walkAST
  11. } from "@vue-macros/common";
  12. function transformDefineRender(code, id) {
  13. if (!code.includes(DEFINE_RENDER))
  14. return;
  15. const lang = getLang(id);
  16. const program = babelParse(code, lang === "vue" ? "js" : lang);
  17. const nodes = [];
  18. walkAST(program, {
  19. enter(node, parent) {
  20. if (node.type !== "ExpressionStatement" || !isCallOf(node.expression, DEFINE_RENDER) || parent.type !== "BlockStatement")
  21. return;
  22. nodes.push({
  23. parent,
  24. node,
  25. arg: node.expression.arguments[0]
  26. });
  27. }
  28. });
  29. if (nodes.length === 0)
  30. return;
  31. const s = new MagicString(code);
  32. for (const { parent, node, arg } of nodes) {
  33. const returnStmt = parent.body.find(
  34. (node2) => node2.type === "ReturnStatement"
  35. );
  36. if (returnStmt)
  37. s.removeNode(returnStmt);
  38. const index = returnStmt ? returnStmt.start : parent.end - 1;
  39. const shouldAddFn = !isFunctionType(arg) && arg.type !== "Identifier";
  40. s.appendLeft(index, `return ${shouldAddFn ? "() => (" : ""}`);
  41. s.moveNode(arg, index);
  42. if (shouldAddFn)
  43. s.appendRight(index, `)`);
  44. s.remove(node.start, arg.start);
  45. s.remove(arg.end, node.end);
  46. }
  47. return getTransformResult(s, id);
  48. }
  49. export {
  50. transformDefineRender
  51. };