版博士V2.0程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

810 satır
26 KiB

  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// src/core/impl.ts
  2. var _magicstring = require('magic-string'); var _magicstring2 = _interopRequireDefault(_magicstring);
  3. // ../../node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/walker.js
  4. var WalkerBase = class {
  5. constructor() {
  6. this.should_skip = false;
  7. this.should_remove = false;
  8. this.replacement = null;
  9. this.context = {
  10. skip: () => this.should_skip = true,
  11. remove: () => this.should_remove = true,
  12. replace: (node) => this.replacement = node
  13. };
  14. }
  15. /**
  16. * @template {Node} Parent
  17. * @param {Parent | null | undefined} parent
  18. * @param {keyof Parent | null | undefined} prop
  19. * @param {number | null | undefined} index
  20. * @param {Node} node
  21. */
  22. replace(parent, prop, index, node) {
  23. if (parent && prop) {
  24. if (index != null) {
  25. parent[prop][index] = node;
  26. } else {
  27. parent[prop] = node;
  28. }
  29. }
  30. }
  31. /**
  32. * @template {Node} Parent
  33. * @param {Parent | null | undefined} parent
  34. * @param {keyof Parent | null | undefined} prop
  35. * @param {number | null | undefined} index
  36. */
  37. remove(parent, prop, index) {
  38. if (parent && prop) {
  39. if (index !== null && index !== void 0) {
  40. parent[prop].splice(index, 1);
  41. } else {
  42. delete parent[prop];
  43. }
  44. }
  45. }
  46. };
  47. // ../../node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/sync.js
  48. var SyncWalker = class extends WalkerBase {
  49. /**
  50. *
  51. * @param {SyncHandler} [enter]
  52. * @param {SyncHandler} [leave]
  53. */
  54. constructor(enter, leave) {
  55. super();
  56. this.should_skip = false;
  57. this.should_remove = false;
  58. this.replacement = null;
  59. this.context = {
  60. skip: () => this.should_skip = true,
  61. remove: () => this.should_remove = true,
  62. replace: (node) => this.replacement = node
  63. };
  64. this.enter = enter;
  65. this.leave = leave;
  66. }
  67. /**
  68. * @template {Node} Parent
  69. * @param {Node} node
  70. * @param {Parent | null} parent
  71. * @param {keyof Parent} [prop]
  72. * @param {number | null} [index]
  73. * @returns {Node | null}
  74. */
  75. visit(node, parent, prop, index) {
  76. if (node) {
  77. if (this.enter) {
  78. const _should_skip = this.should_skip;
  79. const _should_remove = this.should_remove;
  80. const _replacement = this.replacement;
  81. this.should_skip = false;
  82. this.should_remove = false;
  83. this.replacement = null;
  84. this.enter.call(this.context, node, parent, prop, index);
  85. if (this.replacement) {
  86. node = this.replacement;
  87. this.replace(parent, prop, index, node);
  88. }
  89. if (this.should_remove) {
  90. this.remove(parent, prop, index);
  91. }
  92. const skipped = this.should_skip;
  93. const removed = this.should_remove;
  94. this.should_skip = _should_skip;
  95. this.should_remove = _should_remove;
  96. this.replacement = _replacement;
  97. if (skipped)
  98. return node;
  99. if (removed)
  100. return null;
  101. }
  102. let key;
  103. for (key in node) {
  104. const value = node[key];
  105. if (value && typeof value === "object") {
  106. if (Array.isArray(value)) {
  107. const nodes = (
  108. /** @type {Array<unknown>} */
  109. value
  110. );
  111. for (let i = 0; i < nodes.length; i += 1) {
  112. const item = nodes[i];
  113. if (isNode(item)) {
  114. if (!this.visit(item, node, key, i)) {
  115. i--;
  116. }
  117. }
  118. }
  119. } else if (isNode(value)) {
  120. this.visit(value, node, key, null);
  121. }
  122. }
  123. }
  124. if (this.leave) {
  125. const _replacement = this.replacement;
  126. const _should_remove = this.should_remove;
  127. this.replacement = null;
  128. this.should_remove = false;
  129. this.leave.call(this.context, node, parent, prop, index);
  130. if (this.replacement) {
  131. node = this.replacement;
  132. this.replace(parent, prop, index, node);
  133. }
  134. if (this.should_remove) {
  135. this.remove(parent, prop, index);
  136. }
  137. const removed = this.should_remove;
  138. this.replacement = _replacement;
  139. this.should_remove = _should_remove;
  140. if (removed)
  141. return null;
  142. }
  143. }
  144. return node;
  145. }
  146. };
  147. function isNode(value) {
  148. return value !== null && typeof value === "object" && "type" in value && typeof value.type === "string";
  149. }
  150. // ../../node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/index.js
  151. function walk(ast, { enter, leave }) {
  152. const instance = new SyncWalker(enter, leave);
  153. return instance.visit(ast, null);
  154. }
  155. // src/core/impl.ts
  156. var _compilercore = require('@vue/compiler-core');
  157. var _parser = require('@babel/parser');
  158. var _shared = require('@vue/shared');
  159. var _common = require('@vue-macros/common');
  160. var CONVERT_SYMBOL = "$";
  161. var ESCAPE_SYMBOL = "$$";
  162. var IMPORT_SOURCE = "vue/macros";
  163. var shorthands = ["ref", "computed", "shallowRef", "toRef", "customRef"];
  164. var transformCheckRE = /\W\$(?:\$|ref|computed|shallowRef|toRef|customRef)?\s*([(<])/;
  165. function shouldTransform(src) {
  166. return transformCheckRE.test(src);
  167. }
  168. function transform(src, {
  169. filename,
  170. sourceMap,
  171. parserPlugins,
  172. importHelpersFrom = "vue"
  173. } = {}) {
  174. const plugins = parserPlugins || [];
  175. if (filename) {
  176. if (/\.tsx?$/.test(filename)) {
  177. plugins.push("typescript");
  178. }
  179. if (filename.endsWith("x")) {
  180. plugins.push("jsx");
  181. }
  182. }
  183. const ast = _parser.parse.call(void 0, src, {
  184. sourceType: "module",
  185. plugins
  186. });
  187. const s = new (0, _magicstring2.default)(src);
  188. const res = transformAST(ast.program, s, 0);
  189. if (res.importedHelpers.length > 0) {
  190. s.prepend(
  191. `import { ${res.importedHelpers.map((h) => `${h} as _${h}`).join(", ")} } from '${importHelpersFrom}'
  192. `
  193. );
  194. }
  195. return {
  196. ...res,
  197. code: s.toString(),
  198. map: sourceMap ? s.generateMap({
  199. source: filename,
  200. hires: true,
  201. includeContent: true
  202. }) : null
  203. };
  204. }
  205. function transformAST(ast, s, offset = 0, knownRefs, knownProps) {
  206. const userImports = /* @__PURE__ */ Object.create(null);
  207. for (const node of ast.body) {
  208. if (node.type !== "ImportDeclaration")
  209. continue;
  210. walkImportDeclaration(node);
  211. }
  212. let convertSymbol;
  213. let escapeSymbol;
  214. for (const { local, imported, source, specifier } of Object.values(
  215. userImports
  216. )) {
  217. if (source === IMPORT_SOURCE) {
  218. if (imported === ESCAPE_SYMBOL) {
  219. escapeSymbol = local;
  220. } else if (imported === CONVERT_SYMBOL) {
  221. convertSymbol = local;
  222. } else if (imported !== local) {
  223. error(
  224. `macro imports for ref-creating methods do not support aliasing.`,
  225. specifier
  226. );
  227. }
  228. }
  229. }
  230. if (!convertSymbol && !userImports[CONVERT_SYMBOL]) {
  231. convertSymbol = CONVERT_SYMBOL;
  232. }
  233. if (!escapeSymbol && !userImports[ESCAPE_SYMBOL]) {
  234. escapeSymbol = ESCAPE_SYMBOL;
  235. }
  236. const importedHelpers = /* @__PURE__ */ new Set();
  237. const rootScope = {};
  238. const scopeStack = [rootScope];
  239. let currentScope = rootScope;
  240. let escapeScope;
  241. const excludedIds = /* @__PURE__ */ new WeakSet();
  242. const parentStack = [];
  243. const propsLocalToPublicMap = /* @__PURE__ */ Object.create(null);
  244. if (knownRefs) {
  245. for (const key of knownRefs) {
  246. rootScope[key] = {};
  247. }
  248. }
  249. if (knownProps) {
  250. for (const key in knownProps) {
  251. const { local, isConst } = knownProps[key];
  252. rootScope[local] = {
  253. isProp: true,
  254. isConst: !!isConst
  255. };
  256. propsLocalToPublicMap[local] = key;
  257. }
  258. }
  259. function walkImportDeclaration(node) {
  260. const source = node.source.value;
  261. if (source === IMPORT_SOURCE) {
  262. s.remove(node.start + offset, node.end + offset);
  263. }
  264. for (const specifier of node.specifiers) {
  265. const local = specifier.local.name;
  266. const imported = specifier.type === "ImportSpecifier" && specifier.imported.type === "Identifier" && specifier.imported.name || "default";
  267. userImports[local] = {
  268. source,
  269. local,
  270. imported,
  271. specifier
  272. };
  273. }
  274. }
  275. function isRefCreationCall(callee) {
  276. if (!convertSymbol || getCurrentScope()[convertSymbol] !== void 0) {
  277. return false;
  278. }
  279. if (callee === convertSymbol) {
  280. return convertSymbol;
  281. }
  282. if (callee[0] === "$" && shorthands.includes(callee.slice(1))) {
  283. return callee;
  284. }
  285. return false;
  286. }
  287. function error(msg, node) {
  288. const e = new Error(msg);
  289. e.node = node;
  290. throw e;
  291. }
  292. function helper(msg) {
  293. importedHelpers.add(msg);
  294. return `_${msg}`;
  295. }
  296. function getCurrentScope() {
  297. return scopeStack.reduce((prev, curr) => ({ ...prev, ...curr }), {});
  298. }
  299. function registerBinding(id, binding) {
  300. excludedIds.add(id);
  301. if (currentScope) {
  302. currentScope[id.name] = binding ? binding : false;
  303. } else {
  304. error(
  305. "registerBinding called without active scope, something is wrong.",
  306. id
  307. );
  308. }
  309. }
  310. const registerRefBinding = (id, isConst = false) => registerBinding(id, { isConst });
  311. let tempVarCount = 0;
  312. function genTempVar() {
  313. return `__$temp_${++tempVarCount}`;
  314. }
  315. function snip(node) {
  316. return s.original.slice(node.start + offset, node.end + offset);
  317. }
  318. function findUpParent() {
  319. return parentStack.slice().reverse().find(({ type }) => !_common.TS_NODE_TYPES.includes(type));
  320. }
  321. function walkScope(node, isRoot = false) {
  322. for (const stmt of node.body) {
  323. if (stmt.type === "VariableDeclaration") {
  324. walkVariableDeclaration(stmt, isRoot);
  325. } else if (stmt.type === "FunctionDeclaration" || stmt.type === "ClassDeclaration") {
  326. if (stmt.declare || !stmt.id)
  327. continue;
  328. registerBinding(stmt.id);
  329. } else if ((stmt.type === "ForOfStatement" || stmt.type === "ForInStatement") && stmt.left.type === "VariableDeclaration") {
  330. walkVariableDeclaration(stmt.left);
  331. } else if (stmt.type === "ExportNamedDeclaration" && stmt.declaration && stmt.declaration.type === "VariableDeclaration") {
  332. walkVariableDeclaration(stmt.declaration, isRoot);
  333. } else if (stmt.type === "LabeledStatement" && stmt.body.type === "VariableDeclaration") {
  334. walkVariableDeclaration(stmt.body, isRoot);
  335. }
  336. }
  337. }
  338. function walkVariableDeclaration(stmt, isRoot = false) {
  339. if (stmt.declare) {
  340. return;
  341. }
  342. for (const decl of stmt.declarations) {
  343. let refCall;
  344. const init = decl.init ? _common.unwrapTSNode.call(void 0, decl.init) : null;
  345. const isCall = init && init.type === "CallExpression" && init.callee.type === "Identifier";
  346. if (isCall && (refCall = isRefCreationCall(init.callee.name))) {
  347. processRefDeclaration(
  348. refCall,
  349. decl.id,
  350. decl.init,
  351. init,
  352. stmt.kind === "const"
  353. );
  354. } else {
  355. const isProps = isRoot && isCall && init.callee.name === "defineProps";
  356. for (const id of _compilercore.extractIdentifiers.call(void 0, decl.id)) {
  357. if (isProps) {
  358. excludedIds.add(id);
  359. } else {
  360. registerBinding(id);
  361. }
  362. }
  363. }
  364. }
  365. }
  366. function processRefDeclaration(method, id, init, call, isConst) {
  367. excludedIds.add(call.callee);
  368. if (method === convertSymbol) {
  369. s.remove(call.callee.start + offset, call.callee.end + offset);
  370. if (id.type === "Identifier") {
  371. registerRefBinding(id, isConst);
  372. } else if (id.type === "ObjectPattern") {
  373. processRefObjectPattern(id, init, isConst);
  374. } else if (id.type === "ArrayPattern") {
  375. processRefArrayPattern(id, init, isConst);
  376. }
  377. } else if (id.type === "Identifier") {
  378. registerRefBinding(id, isConst);
  379. s.overwrite(
  380. call.start + offset,
  381. call.start + method.length + offset,
  382. helper(method.slice(1))
  383. );
  384. } else {
  385. error(`${method}() cannot be used with destructure patterns.`, call);
  386. }
  387. }
  388. function processRefObjectPattern(pattern, value, isConst, tempVar, path = []) {
  389. if (!tempVar) {
  390. tempVar = genTempVar();
  391. s.overwrite(pattern.start + offset, pattern.end + offset, tempVar);
  392. }
  393. let nameId;
  394. for (const p of pattern.properties) {
  395. let key;
  396. let defaultValue;
  397. if (p.type === "ObjectProperty") {
  398. if (p.key.start === p.value.start) {
  399. nameId = p.key;
  400. if (p.value.type === "Identifier") {
  401. excludedIds.add(p.value);
  402. } else if (p.value.type === "AssignmentPattern" && p.value.left.type === "Identifier") {
  403. excludedIds.add(p.value.left);
  404. defaultValue = p.value.right;
  405. }
  406. } else {
  407. key = p.computed ? p.key : p.key.name;
  408. if (p.value.type === "Identifier") {
  409. nameId = p.value;
  410. } else if (p.value.type === "ObjectPattern") {
  411. processRefObjectPattern(p.value, value, isConst, tempVar, [
  412. ...path,
  413. key
  414. ]);
  415. } else if (p.value.type === "ArrayPattern") {
  416. processRefArrayPattern(p.value, value, isConst, tempVar, [
  417. ...path,
  418. key
  419. ]);
  420. } else if (p.value.type === "AssignmentPattern") {
  421. if (p.value.left.type === "Identifier") {
  422. nameId = p.value.left;
  423. defaultValue = p.value.right;
  424. } else if (p.value.left.type === "ObjectPattern") {
  425. processRefObjectPattern(p.value.left, value, isConst, tempVar, [
  426. ...path,
  427. [key, p.value.right]
  428. ]);
  429. } else if (p.value.left.type === "ArrayPattern") {
  430. processRefArrayPattern(p.value.left, value, isConst, tempVar, [
  431. ...path,
  432. [key, p.value.right]
  433. ]);
  434. } else {
  435. }
  436. }
  437. }
  438. } else {
  439. error(`reactivity destructure does not support rest elements.`, p);
  440. }
  441. if (nameId) {
  442. registerRefBinding(nameId, isConst);
  443. const source = pathToString(tempVar, path);
  444. const keyStr = _shared.isString.call(void 0, key) ? `'${key}'` : key ? snip(key) : `'${nameId.name}'`;
  445. const defaultStr = defaultValue ? `, ${snip(defaultValue)}` : ``;
  446. s.appendLeft(
  447. value.end + offset,
  448. `,
  449. ${nameId.name} = ${helper(
  450. "toRef"
  451. )}(${source}, ${keyStr}${defaultStr})`
  452. );
  453. }
  454. }
  455. if (nameId) {
  456. s.appendLeft(value.end + offset, ";");
  457. }
  458. }
  459. function processRefArrayPattern(pattern, value, isConst, tempVar, path = []) {
  460. if (!tempVar) {
  461. tempVar = genTempVar();
  462. s.overwrite(pattern.start + offset, pattern.end + offset, tempVar);
  463. }
  464. let nameId;
  465. for (let i = 0; i < pattern.elements.length; i++) {
  466. const e = pattern.elements[i];
  467. if (!e)
  468. continue;
  469. let defaultValue;
  470. if (e.type === "Identifier") {
  471. nameId = e;
  472. } else if (e.type === "AssignmentPattern") {
  473. nameId = e.left;
  474. defaultValue = e.right;
  475. } else if (e.type === "RestElement") {
  476. error(`reactivity destructure does not support rest elements.`, e);
  477. } else if (e.type === "ObjectPattern") {
  478. processRefObjectPattern(e, value, isConst, tempVar, [...path, i]);
  479. } else if (e.type === "ArrayPattern") {
  480. processRefArrayPattern(e, value, isConst, tempVar, [...path, i]);
  481. }
  482. if (nameId) {
  483. registerRefBinding(nameId, isConst);
  484. const source = pathToString(tempVar, path);
  485. const defaultStr = defaultValue ? `, ${snip(defaultValue)}` : ``;
  486. s.appendLeft(
  487. value.end + offset,
  488. `,
  489. ${nameId.name} = ${helper(
  490. "toRef"
  491. )}(${source}, ${i}${defaultStr})`
  492. );
  493. }
  494. }
  495. if (nameId) {
  496. s.appendLeft(value.end + offset, ";");
  497. }
  498. }
  499. function pathToString(source, path) {
  500. if (path.length > 0) {
  501. for (const seg of path) {
  502. if (_shared.isArray.call(void 0, seg)) {
  503. source = `(${source}${segToString(seg[0])} || ${snip(seg[1])})`;
  504. } else {
  505. source += segToString(seg);
  506. }
  507. }
  508. }
  509. return source;
  510. }
  511. function segToString(seg) {
  512. if (typeof seg === "number") {
  513. return `[${seg}]`;
  514. } else if (typeof seg === "string") {
  515. return `.${seg}`;
  516. } else {
  517. return snip(seg);
  518. }
  519. }
  520. function rewriteId(scope, id, parent, parentStack2) {
  521. if (_shared.hasOwn.call(void 0, scope, id.name)) {
  522. const binding = scope[id.name];
  523. if (binding) {
  524. if (binding.isConst && (parent.type === "AssignmentExpression" && id === parent.left || parent.type === "UpdateExpression")) {
  525. error(`Assignment to constant variable.`, id);
  526. }
  527. const { isProp } = binding;
  528. if (_compilercore.isStaticProperty.call(void 0, parent) && parent.shorthand) {
  529. if (!parent.inPattern || _compilercore.isInDestructureAssignment.call(void 0, parent, parentStack2)) {
  530. if (isProp) {
  531. if (escapeScope) {
  532. registerEscapedPropBinding(id);
  533. s.appendLeft(
  534. id.end + offset,
  535. `: __props_${propsLocalToPublicMap[id.name]}`
  536. );
  537. } else {
  538. s.appendLeft(
  539. id.end + offset,
  540. `: ${_shared.genPropsAccessExp.call(void 0, propsLocalToPublicMap[id.name])}`
  541. );
  542. }
  543. } else {
  544. s.appendLeft(id.end + offset, `: ${id.name}.value`);
  545. }
  546. }
  547. } else if (isProp) {
  548. if (escapeScope) {
  549. registerEscapedPropBinding(id);
  550. s.overwrite(
  551. id.start + offset,
  552. id.end + offset,
  553. `__props_${propsLocalToPublicMap[id.name]}`
  554. );
  555. } else {
  556. s.overwrite(
  557. id.start + offset,
  558. id.end + offset,
  559. _shared.genPropsAccessExp.call(void 0, propsLocalToPublicMap[id.name])
  560. );
  561. }
  562. } else {
  563. s.appendLeft(id.end + offset, ".value");
  564. }
  565. }
  566. return true;
  567. }
  568. return false;
  569. }
  570. const propBindingRefs = {};
  571. function registerEscapedPropBinding(id) {
  572. if (!propBindingRefs.hasOwnProperty(id.name)) {
  573. propBindingRefs[id.name] = true;
  574. const publicKey = propsLocalToPublicMap[id.name];
  575. s.prependRight(
  576. offset,
  577. `const __props_${publicKey} = ${helper(
  578. `toRef`
  579. )}(__props, '${publicKey}');
  580. `
  581. );
  582. }
  583. }
  584. walkScope(ast, true);
  585. walk(ast, {
  586. enter(node, parent) {
  587. parent && parentStack.push(parent);
  588. if (_compilercore.isFunctionType.call(void 0, node)) {
  589. scopeStack.push(currentScope = {});
  590. _compilercore.walkFunctionParams.call(void 0, node, registerBinding);
  591. if (node.body.type === "BlockStatement") {
  592. walkScope(node.body);
  593. }
  594. return;
  595. }
  596. if (node.type === "CatchClause") {
  597. scopeStack.push(currentScope = {});
  598. if (node.param && node.param.type === "Identifier") {
  599. registerBinding(node.param);
  600. }
  601. walkScope(node.body);
  602. return;
  603. }
  604. if (node.type === "BlockStatement" && !_compilercore.isFunctionType.call(void 0, parent)) {
  605. scopeStack.push(currentScope = {});
  606. walkScope(node);
  607. return;
  608. }
  609. if (parent && parent.type.startsWith("TS") && !_common.TS_NODE_TYPES.includes(parent.type)) {
  610. return this.skip();
  611. }
  612. if (node.type === "Identifier") {
  613. const binding = rootScope[node.name];
  614. if (
  615. // if inside $$(), skip unless this is a destructured prop binding
  616. !(escapeScope && (!binding || !binding.isProp)) && _compilercore.isReferencedIdentifier.call(void 0, node, parent, parentStack) && !excludedIds.has(node)
  617. ) {
  618. let i = scopeStack.length;
  619. while (i--) {
  620. if (rewriteId(scopeStack[i], node, parent, parentStack)) {
  621. return;
  622. }
  623. }
  624. }
  625. }
  626. if (node.type === "CallExpression" && node.callee.type === "Identifier") {
  627. const callee = node.callee.name;
  628. const refCall = isRefCreationCall(callee);
  629. const parent2 = findUpParent();
  630. if (refCall && (!parent2 || parent2.type !== "VariableDeclarator")) {
  631. return error(
  632. `${refCall} can only be used as the initializer of a variable declaration.`,
  633. node
  634. );
  635. }
  636. if (escapeSymbol && getCurrentScope()[escapeSymbol] === void 0 && callee === escapeSymbol) {
  637. escapeScope = node;
  638. s.remove(node.callee.start + offset, node.callee.end + offset);
  639. if ((parent2 == null ? void 0 : parent2.type) === "ExpressionStatement") {
  640. let i = (node.leadingComments ? node.leadingComments[0].start : node.start) + offset;
  641. while (i--) {
  642. const char = s.original.charAt(i);
  643. if (char === "\n") {
  644. s.prependRight(node.start + offset, ";");
  645. break;
  646. } else if (!/\s/.test(char)) {
  647. break;
  648. }
  649. }
  650. }
  651. }
  652. }
  653. },
  654. leave(node, parent) {
  655. parent && parentStack.pop();
  656. if (node.type === "BlockStatement" && !_compilercore.isFunctionType.call(void 0, parent) || _compilercore.isFunctionType.call(void 0, node)) {
  657. scopeStack.pop();
  658. currentScope = scopeStack[scopeStack.length - 1] || null;
  659. }
  660. if (node === escapeScope) {
  661. escapeScope = void 0;
  662. }
  663. }
  664. });
  665. return {
  666. rootRefs: Object.keys(rootScope).filter((key) => {
  667. const binding = rootScope[key];
  668. return binding && !binding.isProp;
  669. }),
  670. importedHelpers: [...importedHelpers]
  671. };
  672. }
  673. // src/core/helper/code.ts?raw
  674. var code_default = "export function createPropsRestProxy(props,excludedKeys){const ret={};for(const key in props){if(!excludedKeys.includes(key)){Object.defineProperty(ret,key,{enumerable:true,get:()=>props[key]})}}return ret}\n";
  675. // src/core/helper/index.ts
  676. var helperId = "/vue-macros/reactivity-transform/helper";
  677. // src/core/index.ts
  678. function transformVueSFC(code, id) {
  679. const s = new (0, _common.MagicString)(code);
  680. const { script, scriptSetup, getScriptAst, getSetupAst } = _common.parseSFC.call(void 0, code, id);
  681. let refBindings;
  682. let propsDestructuredBindings;
  683. if (script && shouldTransform(script.content)) {
  684. const offset = script.loc.start.offset;
  685. const { importedHelpers, rootRefs } = transformAST(
  686. getScriptAst(),
  687. s,
  688. offset
  689. );
  690. refBindings = rootRefs;
  691. importHelpers(s, script.loc.start.offset, importedHelpers);
  692. }
  693. if (scriptSetup) {
  694. const ast = getSetupAst();
  695. for (const node of ast.body) {
  696. processDefineProps(node);
  697. }
  698. if (propsDestructuredBindings || refBindings || shouldTransform(scriptSetup.content)) {
  699. const { importedHelpers } = transformAST(
  700. ast,
  701. s,
  702. scriptSetup.loc.start.offset,
  703. refBindings,
  704. propsDestructuredBindings
  705. );
  706. importHelpers(s, scriptSetup.loc.start.offset, importedHelpers);
  707. }
  708. }
  709. return _common.getTransformResult.call(void 0, s, id);
  710. function processDefineProps(node) {
  711. if (node.type !== "VariableDeclaration")
  712. return;
  713. const decl = node.declarations.find(
  714. (decl2) => _common.isCallOf.call(void 0, decl2.init, _common.DEFINE_PROPS)
  715. );
  716. if (!decl || decl.id.type !== "ObjectPattern")
  717. return;
  718. if (node.declarations.length > 1)
  719. throw new SyntaxError(
  720. `${_common.DEFINE_PROPS}() don't support multiple declarations.`
  721. );
  722. const offset = scriptSetup.loc.start.offset;
  723. let defaultStr = "";
  724. propsDestructuredBindings = {};
  725. for (const prop of decl.id.properties) {
  726. if (prop.type === "ObjectProperty") {
  727. const propKey = _common.resolveObjectKey.call(void 0, prop.key, prop.computed, false);
  728. if (!propKey) {
  729. throw new SyntaxError(
  730. `${_common.DEFINE_PROPS}() destructure cannot use computed key.`
  731. );
  732. }
  733. if (prop.value.type === "AssignmentPattern") {
  734. const { left, right } = prop.value;
  735. if (left.type !== "Identifier") {
  736. throw new SyntaxError(
  737. `${_common.DEFINE_PROPS}() destructure does not support nested patterns.`
  738. );
  739. }
  740. propsDestructuredBindings[propKey] = {
  741. local: left.name
  742. };
  743. defaultStr += `${propKey}: ${s.sliceNode(right, { offset })},`;
  744. } else if (prop.value.type === "Identifier") {
  745. propsDestructuredBindings[propKey] = {
  746. local: prop.value.name
  747. };
  748. } else {
  749. throw new SyntaxError(
  750. `${_common.DEFINE_PROPS}() destructure does not support nested patterns.`
  751. );
  752. }
  753. } else {
  754. s.prependLeft(
  755. offset,
  756. `import { createPropsRestProxy } from '${helperId}';
  757. const ${prop.argument.name} = createPropsRestProxy(__props, ${JSON.stringify(
  758. Object.keys(propsDestructuredBindings)
  759. )});
  760. `
  761. );
  762. }
  763. }
  764. const declStr = `withDefaults(${s.sliceNode(decl.init, {
  765. offset
  766. })}, { ${defaultStr} })`;
  767. s.overwriteNode(node, declStr, { offset });
  768. }
  769. }
  770. function importHelpers(s, offset, helpers) {
  771. if (helpers.length === 0)
  772. return;
  773. s.prependLeft(
  774. offset,
  775. `import { ${helpers.map((h) => `${h} as _${h}`).join(", ")} } from 'vue';
  776. `
  777. );
  778. }
  779. exports.shouldTransform = shouldTransform; exports.transform = transform; exports.transformAST = transformAST; exports.code_default = code_default; exports.helperId = helperId; exports.transformVueSFC = transformVueSFC;