transformRef.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.rewriteRef = void 0;
  4. const types_1 = require("@babel/types");
  5. const compiler_core_1 = require("@vue/compiler-core");
  6. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  7. const utils_1 = require("./utils");
  8. const runtimeHelpers_1 = require("../runtimeHelpers");
  9. const codegen_1 = require("../codegen");
  10. const ast_1 = require("../ast");
  11. function rewriteRef(node, context) {
  12. const vueIdProp = (0, compiler_core_1.findProp)(node, utils_1.ATTR_VUE_ID);
  13. if (!vueIdProp) {
  14. return;
  15. }
  16. const refProp = (0, compiler_core_1.findProp)(node, 'u-' + uni_cli_shared_1.VUE_REF) || (0, compiler_core_1.findProp)(node, 'u-' + uni_cli_shared_1.VUE_REF_IN_FOR);
  17. if (!refProp) {
  18. return;
  19. }
  20. if ((0, compiler_core_1.findProp)(node, 'ref')) {
  21. // 支付宝小程序
  22. const code = parseAlipayRefCode(refProp, context);
  23. if (code && context.inline && !(0, uni_cli_shared_1.isDirectiveNode)(refProp)) {
  24. refProp.value.content = code;
  25. const refPropIndex = node.props.findIndex((prop) => prop === refProp);
  26. node.props.splice(refPropIndex, 1, (0, uni_cli_shared_1.createBindDirectiveNode)(refProp.name, code));
  27. }
  28. }
  29. else {
  30. rewriteRefProp(refProp, vueIdProp, context);
  31. }
  32. }
  33. exports.rewriteRef = rewriteRef;
  34. function parseRef(prop, context) {
  35. let expr;
  36. let refKey = '';
  37. const isDir = (0, uni_cli_shared_1.isDirectiveNode)(prop);
  38. if (isDir) {
  39. if (prop.exp) {
  40. expr = (0, ast_1.parseExpr)(prop.exp, context, prop.exp);
  41. }
  42. }
  43. else {
  44. const { value } = prop;
  45. if (value && value.content) {
  46. if (context.inline && context.bindingMetadata[value.content]) {
  47. expr = (0, types_1.identifier)(value.content);
  48. refKey = value.content;
  49. }
  50. else {
  51. expr = (0, types_1.stringLiteral)(value.content);
  52. }
  53. }
  54. }
  55. return { expr, refKey };
  56. }
  57. function parseRefCode(prop, context) {
  58. const { expr, refKey } = parseRef(prop, context);
  59. if (!expr) {
  60. return { code: '', refKey };
  61. }
  62. return { code: (0, codegen_1.genBabelExpr)(expr), refKey };
  63. }
  64. function rewriteRefProp(prop, vueIdProp, context) {
  65. let id = '';
  66. if ((0, uni_cli_shared_1.isDirectiveNode)(vueIdProp)) {
  67. const vueIdExpr = (0, ast_1.parseExpr)(vueIdProp.exp, context, vueIdProp.exp);
  68. if (vueIdExpr) {
  69. id = (0, codegen_1.genBabelExpr)(vueIdExpr);
  70. }
  71. }
  72. else {
  73. id = `'${vueIdProp.value.content}'`;
  74. }
  75. if (!id) {
  76. return;
  77. }
  78. const { code, refKey } = parseRefCode(prop, context);
  79. const opts = Object.create(null);
  80. if (refKey) {
  81. opts.k = refKey;
  82. }
  83. if (context.inVFor) {
  84. opts.f = 1;
  85. }
  86. (0, utils_1.parseExprWithRewrite)(context.helperString(runtimeHelpers_1.SET_REF) +
  87. '(' +
  88. code +
  89. ', ' +
  90. id +
  91. (Object.keys(opts).length ? ', ' + JSON.stringify(opts) : '') +
  92. ')', prop.loc, context);
  93. }
  94. function parseAlipayRefCode(prop, context) {
  95. let expr;
  96. const isDir = (0, uni_cli_shared_1.isDirectiveNode)(prop);
  97. if (isDir) {
  98. if (prop.exp) {
  99. expr = (0, ast_1.parseExpr)(prop.exp, context, prop.exp);
  100. }
  101. }
  102. else {
  103. if (prop.value?.content) {
  104. expr = context.inline
  105. ? processInlineRef(prop, context)
  106. : (0, types_1.stringLiteral)(prop.value.content);
  107. }
  108. }
  109. if (!expr) {
  110. return;
  111. }
  112. return (0, codegen_1.genBabelExpr)(expr);
  113. }
  114. function processInlineRef(prop, context) {
  115. const properties = [];
  116. const { refKey } = parseRef(prop, context);
  117. properties.push((0, types_1.objectProperty)((0, types_1.identifier)('r'), (0, types_1.identifier)(prop.value.content)));
  118. if (refKey) {
  119. properties.push((0, types_1.objectProperty)((0, types_1.identifier)('k'), (0, types_1.stringLiteral)(refKey)));
  120. }
  121. if (context.inVFor) {
  122. properties.push((0, types_1.objectProperty)((0, types_1.identifier)('f'), (0, types_1.numericLiteral)(1)));
  123. }
  124. return (0, types_1.arrowFunctionExpression)([], (0, types_1.objectExpression)(properties));
  125. }