@tsrx/vue 0.1.18 → 0.1.19

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 +2 -2
  2. package/src/transform.js +100 -136
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.1.18",
6
+ "version": "0.1.19",
7
7
  "type": "module",
8
8
  "publishConfig": {
9
9
  "access": "public"
@@ -35,7 +35,7 @@
35
35
  "esrap": "^2.2.8",
36
36
  "is-reference": "^3.0.3",
37
37
  "zimmerframe": "^1.1.2",
38
- "@tsrx/core": "0.1.18"
38
+ "@tsrx/core": "0.1.19"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "vue": ">=3.5",
package/src/transform.js CHANGED
@@ -3,7 +3,7 @@
3
3
  import { walk } from 'zimmerframe';
4
4
  import is_reference from 'is-reference';
5
5
  import {
6
- builders,
6
+ builders as b,
7
7
  addJsxSetupDeclaration,
8
8
  clone_expression_node,
9
9
  clone_identifier,
@@ -66,9 +66,6 @@ const vue_platform = {
66
66
  ctx.needs_define_vapor_component = true;
67
67
  return wrap_helper_component(helper_fn, helper_id, source_node);
68
68
  },
69
- wrapNativeFunctionComponent(fn, ctx, path) {
70
- return wrap_native_function_component(fn, ctx, path);
71
- },
72
69
  canHoistStaticNode(node) {
73
70
  return !contains_component_jsx(node);
74
71
  },
@@ -109,7 +106,7 @@ const vue_platform = {
109
106
  ? create_fallback_component_renderer(fallback_component, fallback_fn)
110
107
  : fallback_fn;
111
108
  const default_slot = ctx.typeOnly
112
- ? builders.arrow([], jsx_child_to_expression(raw_try_content))
109
+ ? b.arrow([], jsx_child_to_expression(raw_try_content))
113
110
  : create_sync_error_boundary_slot(
114
111
  raw_try_content,
115
112
  fallback_fn,
@@ -128,7 +125,7 @@ const vue_platform = {
128
125
  return boundary;
129
126
  },
130
127
  createErrorBoundaryContent(try_content) {
131
- return builders.arrow([], jsx_child_to_expression(try_content));
128
+ return b.arrow([], jsx_child_to_expression(try_content));
132
129
  },
133
130
  transformElementChildren(node, walked_children, raw_children, attributes, ctx) {
134
131
  return rewrite_host_text_children(node, walked_children, raw_children, attributes);
@@ -143,6 +140,7 @@ const vue_platform = {
143
140
  );
144
141
  },
145
142
  injectImports(program, ctx) {
143
+ wrap_native_function_components(program, ctx);
146
144
  inject_vue_imports(program, ctx);
147
145
  },
148
146
  },
@@ -157,7 +155,7 @@ export const transform = createJsxTransform(vue_platform);
157
155
  */
158
156
  function create_vapor_pending_boundary(try_content, fallback_content) {
159
157
  return create_vapor_pending_boundary_from_default_slot(
160
- builders.arrow([], jsx_child_to_expression(try_content)),
158
+ b.arrow([], jsx_child_to_expression(try_content)),
161
159
  fallback_content,
162
160
  );
163
161
  }
@@ -169,21 +167,18 @@ function create_vapor_pending_boundary(try_content, fallback_content) {
169
167
  */
170
168
  function create_vapor_pending_boundary_from_default_slot(default_slot, fallback_content) {
171
169
  const fallback_expression = jsx_child_to_expression(fallback_content);
172
- const slots_properties = [
173
- builders.init('_', builders.literal(1)),
174
- builders.init('default', default_slot),
175
- ];
170
+ const slots_properties = [b.init('_', b.literal(1)), b.init('default', default_slot)];
176
171
 
177
172
  if (fallback_expression.type !== 'Literal' || fallback_expression.value !== null) {
178
- slots_properties.push(builders.init('fallback', builders.arrow([], fallback_expression)));
173
+ slots_properties.push(b.init('fallback', b.arrow([], fallback_expression)));
179
174
  }
180
175
 
181
- const slots = builders.object(slots_properties);
176
+ const slots = b.object(slots_properties);
182
177
 
183
- const boundary = builders.jsx_element_fresh(
184
- builders.jsx_opening_element(
185
- builders.jsx_id('Suspense'),
186
- [builders.jsx_attribute(builders.jsx_id('v-slots'), to_jsx_expression_container(slots))],
178
+ const boundary = b.jsx_element_fresh(
179
+ b.jsx_opening_element(
180
+ b.jsx_id('Suspense'),
181
+ [b.jsx_attribute(b.jsx_id('v-slots'), to_jsx_expression_container(slots))],
187
182
  true,
188
183
  ),
189
184
  null,
@@ -236,34 +231,21 @@ function create_sync_error_boundary_slot(
236
231
  const fallback_expression = fallback_component
237
232
  ? create_fallback_component_element(fallback_component, fallback_fn, [
238
233
  error_id,
239
- builders.arrow([], builders.block([])),
234
+ b.arrow([], b.block([])),
240
235
  ])
241
- : builders.call(
242
- builders.parenthesized(fallback_fn),
243
- clone_identifier(error_id),
244
- builders.arrow([], builders.block([])),
245
- );
246
- const try_block = setLocation(
247
- builders.block([builders.return(content_expression)]),
248
- source_block,
249
- true,
250
- );
236
+ : b.call(b.parenthesized(fallback_fn), clone_identifier(error_id), b.arrow([], b.block([])));
237
+ const try_block = setLocation(b.block([b.return(content_expression)]), source_block, true);
251
238
  const try_statement = setLocation(
252
- builders.try(
239
+ b.try(
253
240
  try_block,
254
- {
255
- type: 'CatchClause',
256
- param: error_id,
257
- body: builders.block([builders.return(fallback_expression)]),
258
- metadata: { path: [] },
259
- },
241
+ b.catch_clause(error_id, null, b.block([b.return(fallback_expression)])),
260
242
  null,
261
243
  null,
262
244
  ),
263
245
  source_try,
264
246
  true,
265
247
  );
266
- return builders.arrow([], builders.block([try_statement]));
248
+ return b.arrow([], b.block([try_statement]));
267
249
  }
268
250
 
269
251
  /**
@@ -272,11 +254,9 @@ function create_sync_error_boundary_slot(
272
254
  * @returns {any}
273
255
  */
274
256
  function create_fallback_component_renderer(fallback_component, fallback_fn) {
275
- return builders.arrow(
257
+ return b.arrow(
276
258
  fallback_fn.params.map((/** @type {any} */ param) => clone_expression_node(param, false)),
277
- builders.block([
278
- builders.return(create_fallback_component_element(fallback_component, fallback_fn)),
279
- ]),
259
+ b.block([b.return(create_fallback_component_element(fallback_component, fallback_fn))]),
280
260
  );
281
261
  }
282
262
 
@@ -331,17 +311,14 @@ function jsx_child_to_expression(child) {
331
311
  * @returns {any}
332
312
  */
333
313
  function create_vapor_error_boundary(content, fallback_fn) {
334
- return builders.jsx_element_fresh(
335
- builders.jsx_opening_element(
336
- builders.jsx_id('TsrxErrorBoundary'),
314
+ return b.jsx_element_fresh(
315
+ b.jsx_opening_element(
316
+ b.jsx_id('TsrxErrorBoundary'),
337
317
  [
338
- builders.jsx_attribute(
339
- builders.jsx_id('fallback'),
340
- to_jsx_expression_container(fallback_fn),
341
- ),
342
- builders.jsx_attribute(
343
- builders.jsx_id('content'),
344
- to_jsx_expression_container(builders.arrow([], jsx_child_to_expression(content))),
318
+ b.jsx_attribute(b.jsx_id('fallback'), to_jsx_expression_container(fallback_fn)),
319
+ b.jsx_attribute(
320
+ b.jsx_id('content'),
321
+ to_jsx_expression_container(b.arrow([], jsx_child_to_expression(content))),
345
322
  ),
346
323
  ],
347
324
  true,
@@ -370,13 +347,9 @@ function mark_type_only_host_ref_attribute(attr) {
370
347
  return attr;
371
348
  }
372
349
 
373
- return {
374
- ...attr,
375
- name: {
376
- ...attr.name,
377
- metadata: { ...(attr.name.metadata || {}), disable_verification: true },
378
- },
379
- };
350
+ const name = b.jsx_id(attr.name.name, attr.name);
351
+ name.metadata = { ...(attr.name.metadata || {}), disable_verification: true };
352
+ return b.jsx_attribute(name, attr.value, attr.shorthand, attr);
380
353
  }
381
354
 
382
355
  /**
@@ -387,8 +360,8 @@ function mark_type_only_host_ref_attribute(attr) {
387
360
  */
388
361
  function wrap_helper_component(helper_fn, helper_id, source_node) {
389
362
  return setLocation(
390
- builders.declaration('const', [
391
- builders.declarator(
363
+ b.declaration('const', [
364
+ b.declarator(
392
365
  clone_identifier(helper_id),
393
366
  create_define_vapor_component_call(function_declaration_to_expression(helper_fn), [], []),
394
367
  ),
@@ -397,6 +370,29 @@ function wrap_helper_component(helper_fn, helper_id, source_node) {
397
370
  );
398
371
  }
399
372
 
373
+ /**
374
+ * @param {any} program
375
+ * @param {any} ctx
376
+ * @returns {void}
377
+ */
378
+ function wrap_native_function_components(program, ctx) {
379
+ const wrapped = walk(program, ctx, {
380
+ FunctionDeclaration(node, { next, path, state }) {
381
+ const inner = next() ?? node;
382
+ return wrap_native_function_component(inner, state, path) ?? inner;
383
+ },
384
+ FunctionExpression(node, { next, path, state }) {
385
+ const inner = next() ?? node;
386
+ return wrap_native_function_component(inner, state, path) ?? inner;
387
+ },
388
+ ArrowFunctionExpression(node, { next, path, state }) {
389
+ const inner = next() ?? node;
390
+ return wrap_native_function_component(inner, state, path) ?? inner;
391
+ },
392
+ });
393
+ program.body = wrapped.body;
394
+ }
395
+
400
396
  /**
401
397
  * @param {any} fn
402
398
  * @param {any} ctx
@@ -404,26 +400,34 @@ function wrap_helper_component(helper_fn, helper_id, source_node) {
404
400
  * @returns {any | null}
405
401
  */
406
402
  function wrap_native_function_component(fn, ctx, path) {
407
- const name = get_function_component_name(fn, path);
403
+ if (!fn.metadata?.native_tsrx_function) {
404
+ return null;
405
+ }
406
+
407
+ const parent = path.at(-1);
408
+ const name = get_function_component_name(fn, parent);
408
409
  if (!name || !/^[A-Z]/.test(name)) {
409
410
  return null;
410
411
  }
411
412
 
412
413
  ctx.needs_define_vapor_component = true;
413
414
 
414
- const call = create_define_vapor_component_call(
415
- function_declaration_to_expression(fn),
416
- fn.metadata?.generated_helpers || [],
417
- fn.metadata?.generated_statics || [],
415
+ const call = setLocation(
416
+ create_define_vapor_component_call(
417
+ function_declaration_to_expression(fn),
418
+ fn.metadata?.generated_helpers || [],
419
+ fn.metadata?.generated_statics || [],
420
+ ),
421
+ fn,
422
+ true,
418
423
  );
419
424
 
420
425
  if (fn.type !== 'FunctionDeclaration') {
421
426
  return call;
422
427
  }
423
428
 
424
- const parent = path.at(-1);
425
429
  if (parent?.type === 'ExportDefaultDeclaration' && parent.declaration === fn) {
426
- return setLocation(call, fn, true);
430
+ return call;
427
431
  }
428
432
 
429
433
  if (!fn.id) {
@@ -431,9 +435,7 @@ function wrap_native_function_component(fn, ctx, path) {
431
435
  }
432
436
 
433
437
  return setLocation(
434
- builders.declaration('const', [
435
- builders.declarator(create_generated_identifier(fn.id.name), setLocation(call, fn, true)),
436
- ]),
438
+ b.declaration('const', [b.declarator(create_generated_identifier(fn.id.name), call)]),
437
439
  fn,
438
440
  true,
439
441
  );
@@ -441,15 +443,14 @@ function wrap_native_function_component(fn, ctx, path) {
441
443
 
442
444
  /**
443
445
  * @param {any} fn
444
- * @param {any[]} path
446
+ * @param {any} parent
445
447
  * @returns {string | null}
446
448
  */
447
- function get_function_component_name(fn, path) {
449
+ function get_function_component_name(fn, parent) {
448
450
  if (fn.id?.type === 'Identifier') {
449
451
  return fn.id.name;
450
452
  }
451
453
 
452
- const parent = path.at(-1);
453
454
  if (parent?.type === 'VariableDeclarator' && parent.init === fn) {
454
455
  return get_static_name(parent.id);
455
456
  }
@@ -489,7 +490,7 @@ function get_static_name(node) {
489
490
  * @returns {any}
490
491
  */
491
492
  function create_define_vapor_component_call(fn_expression, generated_helpers, generated_statics) {
492
- const call = builders.call('defineVaporComponent', fn_expression);
493
+ const call = b.call('defineVaporComponent', fn_expression);
493
494
  Object.assign(/** @type {any} */ (call.metadata), {
494
495
  generated_helpers,
495
496
  generated_statics,
@@ -538,25 +539,22 @@ function render_for_of_as_vapor_for(node, loop_params, body_statements, transfor
538
539
  }
539
540
 
540
541
  const attributes = [
541
- builders.jsx_attribute(
542
- builders.jsx_id('in'),
543
- to_jsx_expression_container(clone_expression_node(node.right)),
544
- ),
542
+ b.jsx_attribute(b.jsx_id('in'), to_jsx_expression_container(clone_expression_node(node.right))),
545
543
  ];
546
544
 
547
545
  if (key_expression) {
548
546
  attributes.push(
549
- builders.jsx_attribute(
550
- builders.jsx_id('getKey'),
547
+ b.jsx_attribute(
548
+ b.jsx_id('getKey'),
551
549
  to_jsx_expression_container(create_loop_callback(loop_params, key_expression, true)),
552
550
  ),
553
551
  );
554
552
  }
555
553
 
556
554
  return to_jsx_expression_container(
557
- builders.jsx_element_fresh(
558
- builders.jsx_opening_element(builders.jsx_id('VaporFor'), attributes),
559
- builders.jsx_closing_element(builders.jsx_id('VaporFor')),
555
+ b.jsx_element_fresh(
556
+ b.jsx_opening_element(b.jsx_id('VaporFor'), attributes),
557
+ b.jsx_closing_element(b.jsx_id('VaporFor')),
560
558
  [to_jsx_expression_container(create_loop_callback(slot.params, slot.body, slot.expression))],
561
559
  ),
562
560
  );
@@ -570,12 +568,9 @@ function render_for_of_as_vapor_for(node, loop_params, body_statements, transfor
570
568
  */
571
569
  function render_for_of_as_flat_map(node, loop_params, rendered) {
572
570
  return to_jsx_expression_container(
573
- builders.call(
574
- builders.member(clone_expression_node(node.right), 'flatMap'),
575
- builders.arrow(
576
- loop_params,
577
- builders.block([builders.return(to_array_render_expression(rendered))]),
578
- ),
571
+ b.call(
572
+ b.member(clone_expression_node(node.right), 'flatMap'),
573
+ b.arrow(loop_params, b.block([b.return(to_array_render_expression(rendered))])),
579
574
  ),
580
575
  );
581
576
  }
@@ -616,11 +611,11 @@ function expression_can_skip_rendering(node) {
616
611
  */
617
612
  function to_array_render_expression(node) {
618
613
  if (node?.type === 'Literal' && node.value === null) {
619
- return builders.array([]);
614
+ return b.array([]);
620
615
  }
621
616
 
622
617
  if (node?.type === 'ConditionalExpression') {
623
- return builders.conditional(
618
+ return b.conditional(
624
619
  node.test,
625
620
  to_array_render_expression(node.consequent),
626
621
  to_array_render_expression(node.alternate),
@@ -628,14 +623,10 @@ function to_array_render_expression(node) {
628
623
  }
629
624
 
630
625
  if (node?.type === 'LogicalExpression' && node.operator === '&&') {
631
- return builders.conditional(
632
- node.left,
633
- to_array_render_expression(node.right),
634
- builders.array([]),
635
- );
626
+ return b.conditional(node.left, to_array_render_expression(node.right), b.array([]));
636
627
  }
637
628
 
638
- return builders.array([node]);
629
+ return b.array([node]);
639
630
  }
640
631
 
641
632
  /**
@@ -693,7 +684,7 @@ function strip_top_level_jsx_keys(node) {
693
684
  * @returns {any}
694
685
  */
695
686
  function create_loop_callback(loop_params, body, expression) {
696
- const callback = builders.arrow(
687
+ const callback = b.arrow(
697
688
  loop_params.map((param) => clone_expression_node(param)),
698
689
  body,
699
690
  );
@@ -1013,12 +1004,7 @@ function create_property_member_expression(object, key) {
1013
1004
  * @returns {any}
1014
1005
  */
1015
1006
  function create_index_member_expression(object, index) {
1016
- return create_member_expression(
1017
- clone_expression_node(object),
1018
- builders.literal(index),
1019
- true,
1020
- object,
1021
- );
1007
+ return create_member_expression(clone_expression_node(object), b.literal(index), true, object);
1022
1008
  }
1023
1009
 
1024
1010
  /**
@@ -1037,7 +1023,7 @@ function create_value_member_expression(identifier) {
1037
1023
  * @returns {any}
1038
1024
  */
1039
1025
  function create_member_expression(object, property, computed, source_node) {
1040
- return builders.member(object, property, computed, false, source_node);
1026
+ return b.member(object, property, computed, false, source_node);
1041
1027
  }
1042
1028
 
1043
1029
  /**
@@ -1046,23 +1032,13 @@ function create_member_expression(object, property, computed, source_node) {
1046
1032
  */
1047
1033
  function function_declaration_to_expression(fn) {
1048
1034
  if (fn.type === 'ArrowFunctionExpression') {
1049
- return {
1050
- ...fn,
1051
- metadata: {
1052
- ...(fn.metadata || {}),
1053
- path: fn.metadata?.path || [],
1054
- },
1055
- };
1035
+ return fn;
1056
1036
  }
1057
1037
 
1058
- return {
1059
- ...fn,
1060
- type: 'FunctionExpression',
1061
- metadata: {
1062
- ...(fn.metadata || {}),
1063
- path: fn.metadata?.path || [],
1064
- },
1065
- };
1038
+ const expression = b.function(fn.id, fn.params, fn.body, fn.async, fn.typeParameters, fn);
1039
+ expression.generator = fn.generator;
1040
+ expression.metadata = { ...(fn.metadata || {}), path: fn.metadata?.path || [] };
1041
+ return expression;
1066
1042
  }
1067
1043
 
1068
1044
  const VUE_SETUP_CALLS = new Set([
@@ -1155,14 +1131,8 @@ function create_vue_named_ref_spread(attr) {
1155
1131
  const attr_name = get_vue_attribute_name(attr);
1156
1132
  const value = get_vue_attribute_expression(attr);
1157
1133
  if (attr_name === null) return attr;
1158
- const prop = builders.prop(
1159
- 'init',
1160
- builders.key(attr_name),
1161
- value ?? builders.literal(true),
1162
- false,
1163
- false,
1164
- );
1165
- return builders.jsx_spread_attribute(builders.object([prop], attr), attr);
1134
+ const prop = b.prop('init', b.key(attr_name), value ?? b.literal(true), false, false);
1135
+ return b.jsx_spread_attribute(b.object([prop], attr), attr);
1166
1136
  }
1167
1137
 
1168
1138
  /**
@@ -1212,7 +1182,7 @@ function rewrite_host_text_children(node, walked_children, raw_children, attribu
1212
1182
  * @returns {any}
1213
1183
  */
1214
1184
  function to_jsx_expression_container(expression, source_node = expression) {
1215
- return builders.jsx_expression_container(expression, source_node);
1185
+ return b.jsx_expression_container(expression, source_node);
1216
1186
  }
1217
1187
 
1218
1188
  /**
@@ -1288,7 +1258,7 @@ function ensure_named_import(program, source, name, local = name) {
1288
1258
  return;
1289
1259
  }
1290
1260
 
1291
- program.body.unshift(builders.imports([[name, local, 'value']], source));
1261
+ program.body.unshift(b.imports([[name, local, 'value']], source));
1292
1262
  }
1293
1263
 
1294
1264
  /**
@@ -1297,11 +1267,5 @@ function ensure_named_import(program, source, name, local = name) {
1297
1267
  * @returns {any}
1298
1268
  */
1299
1269
  function create_import_specifier(name, local = name) {
1300
- return {
1301
- type: 'ImportSpecifier',
1302
- imported: builders.id(name),
1303
- local: builders.id(local),
1304
- importKind: 'value',
1305
- metadata: { path: [] },
1306
- };
1270
+ return b.import_specifier(name, local, 'value');
1307
1271
  }