@tsrx/vue 0.4.0 → 0.4.2

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 +119 -6
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.0",
6
+ "version": "0.4.2",
7
7
  "type": "module",
8
8
  "publishConfig": {
9
9
  "access": "public"
@@ -38,8 +38,8 @@
38
38
  "esrap": "^2.4.0",
39
39
  "is-reference": "^3.0.3",
40
40
  "zimmerframe": "^1.1.2",
41
- "@tsrx/core": "0.6.0",
42
- "@tsrx/vue-runtime": "0.2.0"
41
+ "@tsrx/core": "0.6.2",
42
+ "@tsrx/vue-runtime": "0.2.1"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "vue": ">=3.6.0-0",
package/src/transform.js CHANGED
@@ -13,8 +13,11 @@ import {
13
13
  createHookSafeHelper,
14
14
  create_generated_identifier,
15
15
  createJsxTransform,
16
+ destructure_for_of_item,
16
17
  ERROR_BOUNDARY_INTERNAL_NAME,
17
18
  error,
19
+ FRAGMENT_INTERNAL_NAME,
20
+ forOfPatternReadsIndex as for_of_pattern_reads_index,
18
21
  has_location,
19
22
  IDENTIFIER_OBFUSCATION_PREFIX,
20
23
  insertAfterDirectives,
@@ -1791,6 +1794,14 @@ function render_for_of_as_vapor_for(node, loop_params, body_statements, transfor
1791
1794
  return null;
1792
1795
  }
1793
1796
 
1797
+ // A default in the head's pattern that reads the index can't be read in
1798
+ // the parameters, where the index comes second. The shared lowering
1799
+ // destructures the item in the callback for a body that can skip a row.
1800
+ const pattern_reads_index = for_of_pattern_reads_index(loop_params[0], node.index);
1801
+ if (pattern_reads_index && expression_can_skip_rendering(statement.argument)) {
1802
+ return null;
1803
+ }
1804
+
1794
1805
  const rendered = statement.argument;
1795
1806
  if (expression_can_skip_rendering(rendered)) {
1796
1807
  return render_for_of_as_flat_map(node, loop_params, rendered);
@@ -1818,12 +1829,19 @@ function render_for_of_as_vapor_for(node, loop_params, body_statements, transfor
1818
1829
  ];
1819
1830
 
1820
1831
  if (key_expression) {
1821
- attributes.push(
1822
- b.jsx_attribute(
1823
- b.jsx_id('getKey'),
1824
- to_jsx_expression_container(create_loop_callback(loop_params, key_expression, true)),
1825
- ),
1826
- );
1832
+ let get_key;
1833
+ if (pattern_reads_index) {
1834
+ const item = destructure_for_of_item(
1835
+ loop_params.map((param) => clone_ast_node(param)),
1836
+ /** @type {'const' | 'let' | 'var'} */ (
1837
+ /** @type {AST.VariableDeclaration} */ (node.left).kind
1838
+ ),
1839
+ );
1840
+ get_key = b.arrow(item.params, b.block([item.declaration, b.return(key_expression)]));
1841
+ } else {
1842
+ get_key = create_loop_callback(loop_params, key_expression, true);
1843
+ }
1844
+ attributes.push(b.jsx_attribute(b.jsx_id('getKey'), to_jsx_expression_container(get_key)));
1827
1845
  }
1828
1846
 
1829
1847
  return to_jsx_expression_container(
@@ -2629,6 +2647,89 @@ function imported_name(specifier) {
2629
2647
  return undefined;
2630
2648
  }
2631
2649
 
2650
+ /**
2651
+ * Whether the module may bind `Fragment` to something other than Vue's: any
2652
+ * identifier `Fragment` in a value position, at any depth, other than in a
2653
+ * value import of `Fragment` from `vue`. Declarations of every form count, and
2654
+ * so do references, which costs only the row's key; a property name or a type
2655
+ * doesn't.
2656
+ *
2657
+ * @param {AST.Program} program
2658
+ * @returns {boolean}
2659
+ */
2660
+ function declares_other_fragment(program) {
2661
+ let found = false;
2662
+ walk(/** @type {AST.Node} */ (program), null, {
2663
+ _(node, { next }) {
2664
+ // A type names no runtime value.
2665
+ if (!found && !is_type_only_node(node)) next();
2666
+ },
2667
+ ImportDeclaration(node) {
2668
+ for (const specifier of node.specifiers) {
2669
+ if (specifier.local.name !== 'Fragment') continue;
2670
+ const vue_value =
2671
+ /** @type {AST.Literal} */ (node.source).value === 'vue' &&
2672
+ node.importKind !== 'type' &&
2673
+ specifier.type === 'ImportSpecifier' &&
2674
+ specifier.importKind !== 'type' &&
2675
+ imported_name(specifier) === 'Fragment';
2676
+ if (!vue_value) found = true;
2677
+ }
2678
+ },
2679
+ Identifier(node, { path }) {
2680
+ if (node.name !== 'Fragment') return;
2681
+ const parent = /** @type {AST.Node & { computed?: boolean, shorthand?: boolean }} */ (
2682
+ path.at(-1)
2683
+ );
2684
+ const name_only =
2685
+ !parent.computed &&
2686
+ ((parent.type === 'MemberExpression' && parent.property === node) ||
2687
+ ((parent.type === 'Property' ||
2688
+ parent.type === 'MethodDefinition' ||
2689
+ parent.type === 'PropertyDefinition') &&
2690
+ parent.key === node &&
2691
+ !parent.shorthand));
2692
+ if (!name_only) found = true;
2693
+ },
2694
+ });
2695
+ return found;
2696
+ }
2697
+
2698
+ /**
2699
+ * Rewrites the `<__Fragment key>` rows the shared lowering generated (their
2700
+ * names have no source location). vue-jsx-vapor's server build passes a
2701
+ * fragment's children as an array only when its tag is spelled `Fragment`;
2702
+ * under any other name it passes them as slots, which Vue's `Fragment`
2703
+ * doesn't render. So a row becomes `<Fragment key>`, with `Fragment` imported
2704
+ * from `vue`, unless the module declares something else by that name, and then
2705
+ * an unkeyed `<>` fragment: a loop that doesn't render through `VaporFor`
2706
+ * recreates its rows when they move (#178), so the key keeps no row state.
2707
+ *
2708
+ * @param {AST.Program} program
2709
+ * @param {boolean} keyed
2710
+ * @returns {void}
2711
+ */
2712
+ function rewrite_generated_fragments(program, keyed) {
2713
+ /** @param {ESTreeJSX.JSXElement['openingElement']['name'] | undefined} name */
2714
+ const is_generated = (name) =>
2715
+ name?.type === 'JSXIdentifier' && name.name === FRAGMENT_INTERNAL_NAME && !has_location(name);
2716
+ const rewritten = walk(/** @type {AST.Node} */ (program), null, {
2717
+ JSXElement(node, { next }) {
2718
+ const element = /** @type {ESTreeJSX.JSXElement} */ (/** @type {unknown} */ (node));
2719
+ if (!is_generated(element.openingElement.name)) return next();
2720
+ const walked = /** @type {ESTreeJSX.JSXElement} */ (next() ?? element);
2721
+ if (!keyed) {
2722
+ return /** @type {AST.Node} */ (/** @type {unknown} */ (b.jsx_fragment(walked.children)));
2723
+ }
2724
+ for (const name of [walked.openingElement.name, walked.closingElement?.name]) {
2725
+ if (is_generated(name)) /** @type {ESTreeJSX.JSXIdentifier} */ (name).name = 'Fragment';
2726
+ }
2727
+ return walked;
2728
+ },
2729
+ });
2730
+ program.body = /** @type {AST.Program} */ (rewritten).body;
2731
+ }
2732
+
2632
2733
  /**
2633
2734
  * Turn an authored type-only import of `defineVaporComponent` from
2634
2735
  * `vue-jsx-vapor` into the value import the generated components need. The
@@ -2851,6 +2952,18 @@ function inject_vue_imports(program, transform_context) {
2851
2952
  ensure_named_import(program, 'vue', 'Suspense', SUSPENSE_INTERNAL_NAME);
2852
2953
  }
2853
2954
 
2955
+ // A keyed row whose body is a fragment, in a loop that doesn't render
2956
+ // through `VaporFor`, is a `<Fragment key>`, which vue-jsx-vapor lowers to a
2957
+ // keyed fragment.
2958
+ if (transform_context.needs_fragment) {
2959
+ if (!declares_other_fragment(program)) {
2960
+ rewrite_generated_fragments(program, true);
2961
+ ensure_named_import(program, 'vue', 'Fragment');
2962
+ } else {
2963
+ rewrite_generated_fragments(program, false);
2964
+ }
2965
+ }
2966
+
2854
2967
  if (transform_context.needs_error_boundary) {
2855
2968
  ensure_named_import(
2856
2969
  program,