版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

63 lines
2.2 KiB

  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/core/index.ts
  2. var _common = require('@vue-macros/common');
  3. function transformExportProps(code, id) {
  4. var _a;
  5. const { scriptSetup, getSetupAst } = _common.parseSFC.call(void 0, code, id);
  6. if (!scriptSetup)
  7. return;
  8. const offset = scriptSetup.loc.start.offset;
  9. const s = new (0, _common.MagicString)(code);
  10. const props = {};
  11. let hasDefineProps = false;
  12. function walkVariableDeclarator(decl) {
  13. if (decl.id.type !== "Identifier") {
  14. throw new Error("Only support identifier in export props");
  15. } else if (decl.init && (_common.isCallOf.call(void 0, decl.init, _common.DEFINE_PROPS) || _common.isCallOf.call(void 0, decl.init, _common.WITH_DEFAULTS))) {
  16. hasDefineProps = true;
  17. return;
  18. }
  19. const name = decl.id.name;
  20. const type = decl.id.typeAnnotation ? s.sliceNode(decl.id.typeAnnotation, { offset }) : ": any";
  21. const defaultValue = decl.init ? s.sliceNode(decl.init, { offset }) : void 0;
  22. props[name] = { type, defaultValue };
  23. }
  24. const setupAst = getSetupAst();
  25. for (const stmt of setupAst.body) {
  26. if (stmt.type === "ExportNamedDeclaration" && ((_a = stmt.declaration) == null ? void 0 : _a.type) === "VariableDeclaration") {
  27. for (const decl of stmt.declaration.declarations) {
  28. walkVariableDeclarator(decl);
  29. }
  30. s.removeNode(stmt, { offset });
  31. } else if (_common.isCallOf.call(void 0, stmt, _common.DEFINE_PROPS) || _common.isCallOf.call(void 0, stmt, _common.WITH_DEFAULTS)) {
  32. hasDefineProps = true;
  33. }
  34. }
  35. if (Object.keys(props).length === 0)
  36. return;
  37. else if (hasDefineProps)
  38. throw new Error("Don't support export props mixed with defineProps");
  39. let codegen = "";
  40. let destructureString = "";
  41. for (const [name, { type, defaultValue }] of Object.entries(props)) {
  42. codegen += ` ${name}${defaultValue ? "?" : ""}${type},
  43. `;
  44. destructureString += ` ${name}${defaultValue ? ` = ${defaultValue}` : ""},`;
  45. }
  46. codegen = `const {${destructureString} } = defineProps<{
  47. ${codegen}}>()`;
  48. s.prependLeft(offset, `
  49. ${codegen}`);
  50. return _common.getTransformResult.call(void 0, s, id);
  51. }
  52. exports.transformExportProps = transformExportProps;