@wevu/compiler 6.6.6 → 6.6.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.mjs +78 -2
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -325,8 +325,57 @@ function templateLiteralToConcat(node) {
325
325
  if (segments.length === 1) return segments[0];
326
326
  return segments.reduce((acc, cur) => t.binaryExpression("+", acc, cur));
327
327
  }
328
+ function isOptionalChainNode(node) {
329
+ return Boolean(node && (t.isOptionalMemberExpression(node) || t.isOptionalCallExpression(node)));
330
+ }
331
+ function collectOptionalChain(node) {
332
+ const operations = [];
333
+ let current = node;
334
+ while (isOptionalChainNode(current)) {
335
+ if (t.isOptionalMemberExpression(current)) {
336
+ operations.push({
337
+ type: "member",
338
+ optional: current.optional === true,
339
+ computed: current.computed,
340
+ property: t.cloneNode(current.property)
341
+ });
342
+ current = current.object;
343
+ continue;
344
+ }
345
+ operations.push({
346
+ type: "call",
347
+ optional: current.optional === true,
348
+ args: current.arguments.map((arg) => t.cloneNode(arg))
349
+ });
350
+ current = current.callee;
351
+ }
352
+ if (!t.isExpression(current)) return null;
353
+ return {
354
+ base: t.cloneNode(current),
355
+ operations: operations.reverse()
356
+ };
357
+ }
358
+ function applyOptionalChainOperation(base, operation) {
359
+ if (operation.type === "member") return t.memberExpression(base, t.cloneNode(operation.property), operation.computed);
360
+ return t.callExpression(base, operation.args.map((arg) => t.cloneNode(arg)));
361
+ }
362
+ function lowerOptionalChain(node) {
363
+ const chain = collectOptionalChain(node);
364
+ if (!chain) return t.cloneNode(node);
365
+ const segments = [chain.base];
366
+ for (const operation of chain.operations) {
367
+ const currentBase = t.cloneNode(segments[segments.length - 1]);
368
+ segments.push(applyOptionalChainOperation(currentBase, operation));
369
+ }
370
+ let lowered = t.cloneNode(segments[segments.length - 1]);
371
+ for (let index = chain.operations.length - 1; index >= 0; index--) {
372
+ if (!chain.operations[index].optional) continue;
373
+ lowered = t.conditionalExpression(t.binaryExpression("==", t.cloneNode(segments[index]), t.nullLiteral()), t.identifier("undefined"), lowered);
374
+ }
375
+ return lowered;
376
+ }
328
377
  function normalizeWxmlExpression(exp) {
329
- if (!exp.includes("`") && !exp.includes("??")) return exp;
378
+ if (!exp.includes("`") && !exp.includes("??") && !exp.includes("?.")) return exp;
330
379
  try {
331
380
  const ast = parse$2(`(${exp})`, {
332
381
  sourceType: "module",
@@ -335,6 +384,16 @@ function normalizeWxmlExpression(exp) {
335
384
  const stmt = ast.program.body[0];
336
385
  if (!stmt || !("expression" in stmt)) return exp;
337
386
  traverse(ast, {
387
+ OptionalMemberExpression: { exit(path) {
388
+ if (isOptionalChainNode(path.parentPath.node)) return;
389
+ path.replaceWith(lowerOptionalChain(path.node));
390
+ path.skip();
391
+ } },
392
+ OptionalCallExpression: { exit(path) {
393
+ if (isOptionalChainNode(path.parentPath.node)) return;
394
+ path.replaceWith(lowerOptionalChain(path.node));
395
+ path.skip();
396
+ } },
338
397
  LogicalExpression(path) {
339
398
  if (path.node.operator !== "??") return;
340
399
  const left = path.node.left;
@@ -4606,6 +4665,23 @@ function createThisMemberAccess(prop) {
4606
4665
  function createUnrefCall(exp) {
4607
4666
  return t.callExpression(t.identifier("__wevuUnref"), [exp]);
4608
4667
  }
4668
+ function createHasOwnPropertyCall(target, key) {
4669
+ return t.callExpression(t.memberExpression(t.memberExpression(t.memberExpression(t.identifier("Object"), t.identifier("prototype")), t.identifier("hasOwnProperty")), t.identifier("call")), [target, t.stringLiteral(key)]);
4670
+ }
4671
+ function createIdentifierAccessWithPropsFallback(name) {
4672
+ if (name === "props") {
4673
+ const propsObject = createThisMemberAccess("__wevuProps");
4674
+ return t.conditionalExpression(t.binaryExpression("!=", propsObject, t.nullLiteral()), propsObject, createThisMemberAccess("props"));
4675
+ }
4676
+ const thisAccess = createThisMemberAccess(name);
4677
+ const propsAccess = createMemberAccess(createThisMemberAccess("__wevuProps"), name);
4678
+ const propsObject = createThisMemberAccess("__wevuProps");
4679
+ const hasPropsObject = t.binaryExpression("!=", propsObject, t.nullLiteral());
4680
+ const hasDefinedPropsValue = t.binaryExpression("!==", propsAccess, t.identifier("undefined"));
4681
+ const hasPropsKey = createHasOwnPropertyCall(propsObject, name);
4682
+ const hasUsablePropsValue = t.logicalExpression("&&", hasPropsObject, t.logicalExpression("||", hasDefinedPropsValue, hasPropsKey));
4683
+ return t.conditionalExpression(hasUsablePropsValue, propsAccess, thisAccess);
4684
+ }
4609
4685
  function normalizeJsExpressionWithContext(exp, context, options) {
4610
4686
  const trimmed = exp.trim();
4611
4687
  if (!trimmed) return null;
@@ -4631,7 +4707,7 @@ function normalizeJsExpressionWithContext(exp, context, options) {
4631
4707
  replacement = createUnrefCall(prop ? createMemberAccess(base, prop) : base);
4632
4708
  } else if (name === "__wvOwner" || name === "__wvSlotPropsData" || name === "__wvSlotProps" || name === "__wvSlotScope") replacement = createUnrefCall(createThisMemberAccess(name));
4633
4709
  else replacement = createUnrefCall(createMemberAccess(createThisMemberAccess("__wvOwner"), name));
4634
- else replacement = createUnrefCall(createThisMemberAccess(name));
4710
+ else replacement = createUnrefCall(createIdentifierAccessWithPropsFallback(name));
4635
4711
  const parent = path.parentPath;
4636
4712
  if (parent.isObjectProperty() && parent.node.shorthand && parent.node.key === path.node) {
4637
4713
  parent.node.shorthand = false;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wevu/compiler",
3
3
  "type": "module",
4
- "version": "6.6.6",
4
+ "version": "6.6.8",
5
5
  "description": "wevu 编译器基础包,面向小程序模板的编译与转换",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",