版博士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.
 
 
 
 

114 lines
3.4 KiB

  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/core/utils.ts
  2. var _common = require('@vue-macros/common');
  3. function filterMacro(stmts) {
  4. return stmts.map((raw) => {
  5. let node = raw;
  6. if (raw.type === "ExpressionStatement")
  7. node = raw.expression;
  8. return _common.isCallOf.call(void 0, node, _common.DEFINE_OPTIONS) ? node : void 0;
  9. }).filter((node) => !!node);
  10. }
  11. function hasPropsOrEmits(node) {
  12. return node.properties.some(
  13. (prop) => (prop.type === "ObjectProperty" || prop.type === "ObjectMethod") && prop.key.type === "Identifier" && (prop.key.name === "props" || prop.key.name === "emits")
  14. );
  15. }
  16. // src/core/transform.ts
  17. var _astwalkerscope = require('ast-walker-scope');
  18. function transformDefineOptions(code, id) {
  19. if (!code.includes(_common.DEFINE_OPTIONS))
  20. return;
  21. const sfc = _common.parseSFC.call(void 0, code, id);
  22. if (!sfc.scriptSetup)
  23. return;
  24. const { scriptSetup, getSetupAst, getScriptAst } = sfc;
  25. const setupOffset = scriptSetup.loc.start.offset;
  26. const setupAst = getSetupAst();
  27. const nodes = filterMacro(setupAst.body);
  28. if (nodes.length === 0) {
  29. return;
  30. } else if (nodes.length > 1)
  31. throw new SyntaxError(`duplicate ${_common.DEFINE_OPTIONS}() call`);
  32. const scriptAst = getScriptAst();
  33. if (scriptAst)
  34. checkDefaultExport(scriptAst.body);
  35. const setupBindings = getIdentifiers(setupAst.body);
  36. const s = new (0, _common.MagicString)(code);
  37. const [node] = nodes;
  38. const [arg] = node.arguments;
  39. if (arg) {
  40. const normalScript = _common.addNormalScript.call(void 0, sfc, s);
  41. const scriptOffset = normalScript.start();
  42. _common.importHelperFn.call(void 0, s, scriptOffset, "defineComponent", "vue");
  43. s.appendLeft(
  44. scriptOffset,
  45. `
  46. export default /*#__PURE__*/ ${_common.HELPER_PREFIX}defineComponent(`
  47. );
  48. if (arg.type === "ObjectExpression" && hasPropsOrEmits(arg))
  49. throw new SyntaxError(
  50. `${_common.DEFINE_OPTIONS}() please use defineProps or defineEmits instead.`
  51. );
  52. _common.checkInvalidScopeReference.call(void 0, arg, _common.DEFINE_OPTIONS, setupBindings);
  53. s.moveNode(arg, scriptOffset, { offset: setupOffset });
  54. s.remove(setupOffset + node.start, setupOffset + arg.start);
  55. s.remove(setupOffset + arg.end, setupOffset + node.end);
  56. s.appendRight(scriptOffset, ");");
  57. normalScript.end();
  58. } else {
  59. s.removeNode(node, { offset: setupOffset });
  60. }
  61. return _common.getTransformResult.call(void 0, s, id);
  62. }
  63. var checkDefaultExport = (stmts) => {
  64. const hasDefaultExport = stmts.some(
  65. (node) => node.type === "ExportDefaultDeclaration"
  66. );
  67. if (hasDefaultExport)
  68. throw new SyntaxError(
  69. `${_common.DEFINE_OPTIONS} cannot be used with default export within <script>.`
  70. );
  71. };
  72. var getIdentifiers = (stmts) => {
  73. let ids = [];
  74. const program = {
  75. type: "Program",
  76. body: stmts,
  77. directives: [],
  78. sourceType: "module",
  79. sourceFile: ""
  80. };
  81. _astwalkerscope.walkAST.call(void 0, program, {
  82. enter(node) {
  83. if (node.type === "BlockStatement") {
  84. this.skip();
  85. }
  86. },
  87. leave(node) {
  88. if (node.type !== "Program")
  89. return;
  90. ids = Object.keys(this.scope);
  91. }
  92. });
  93. return ids;
  94. };
  95. exports.filterMacro = filterMacro; exports.hasPropsOrEmits = hasPropsOrEmits; exports.transformDefineOptions = transformDefineOptions; exports.checkDefaultExport = checkDefaultExport; exports.getIdentifiers = getIdentifiers;