@tsrx/vue 0.4.11 → 0.4.13

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/package.json +3 -3
  2. package/src/transform.js +58 -1
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Vue compiler built on @tsrx/core",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.4.11",
6
+ "version": "0.4.13",
7
7
  "type": "module",
8
8
  "publishConfig": {
9
9
  "access": "public"
@@ -42,8 +42,8 @@
42
42
  "esrap": "^2.4.2",
43
43
  "is-reference": "^3.0.3",
44
44
  "zimmerframe": "^1.1.2",
45
- "@tsrx/core": "0.7.1",
46
- "@tsrx/vue-runtime": "0.2.5"
45
+ "@tsrx/core": "0.8.1",
46
+ "@tsrx/vue-runtime": "0.2.6"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "vue": ">=3.6.0-0",
package/src/transform.js CHANGED
@@ -351,7 +351,7 @@ function create_pending_body(fallback_expression, ctx) {
351
351
  [
352
352
  b.jsx_attribute(
353
353
  b.jsx_id('content'),
354
- to_jsx_expression_container(b.arrow([], fallback_expression)),
354
+ to_jsx_expression_container(b.arrow([], with_reactive_output(fallback_expression))),
355
355
  ),
356
356
  ],
357
357
  true,
@@ -361,6 +361,63 @@ function create_pending_body(fallback_expression, ctx) {
361
361
  );
362
362
  }
363
363
 
364
+ /**
365
+ * A `@pending` body whose output follows what it reads. `TsrxPendingBody`
366
+ * calls its content once, as a component's setup, so output that isn't an
367
+ * element or a fragment, such as the choice of a lone `@if`, a `@switch` or a
368
+ * loop through `map_iterable`, would keep what it rendered first. In a
369
+ * fragment, vue-jsx-vapor renders it reactively: a choice becomes a
370
+ * `createIf`, which switches when its condition does, and anything else is
371
+ * evaluated in an effect. The body's statements still run once.
372
+ *
373
+ * @param {AST.Expression} expression
374
+ * @returns {AST.Expression}
375
+ */
376
+ function with_reactive_output(expression) {
377
+ // The shared lowering's `(() => { …statements; return output; })()`.
378
+ if (
379
+ expression.type === 'CallExpression' &&
380
+ expression.arguments.length === 0 &&
381
+ expression.callee.type === 'ArrowFunctionExpression' &&
382
+ expression.callee.body.type === 'BlockStatement'
383
+ ) {
384
+ const callee = expression.callee;
385
+ const body = /** @type {AST.BlockStatement} */ (callee.body);
386
+ return {
387
+ ...expression,
388
+ callee: {
389
+ ...callee,
390
+ body: {
391
+ ...body,
392
+ body: body.body.map((statement) =>
393
+ statement.type === 'ReturnStatement' && statement.argument
394
+ ? { ...statement, argument: in_fragment_unless_node(statement.argument) }
395
+ : statement,
396
+ ),
397
+ },
398
+ },
399
+ };
400
+ }
401
+ return in_fragment_unless_node(expression);
402
+ }
403
+
404
+ /**
405
+ * @param {AST.Expression} expression
406
+ * @returns {AST.Expression}
407
+ */
408
+ function in_fragment_unless_node(expression) {
409
+ if (
410
+ expression.type === 'JSXElement' ||
411
+ expression.type === 'JSXFragment' ||
412
+ (expression.type === 'Literal' && expression.value === null)
413
+ ) {
414
+ return expression;
415
+ }
416
+ return /** @type {AST.Expression} */ (
417
+ /** @type {unknown} */ (b.jsx_fragment([to_jsx_expression_container(expression)]))
418
+ );
419
+ }
420
+
364
421
  /**
365
422
  * @param {AST.Pattern[]} patterns
366
423
  * @returns {Set<string>}