@tsrx/core 0.0.21 → 0.0.22

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.
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Core compiler infrastructure for TSRX syntax",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.0.21",
6
+ "version": "0.0.22",
7
7
  "type": "module",
8
8
  "repository": {
9
9
  "type": "git",
@@ -186,7 +186,31 @@ export function tsx_with_ts_locations() {
186
186
  context.visit(body);
187
187
  }
188
188
  },
189
+
190
+ // esrap's JSXOpeningElement printer doesn't emit `typeArguments`, so generic
191
+ // component tags like `<RenderProp<User>>` lose the `<User>` in the output.
192
+ JSXOpeningElement: (node, context) => {
193
+ context.write('<');
194
+ context.visit(node.name);
195
+ if (node.typeArguments) {
196
+ context.visit(node.typeArguments);
197
+ }
198
+ for (const attribute of node.attributes) {
199
+ context.write(' ');
200
+ context.visit(attribute);
201
+ }
202
+ if (node.selfClosing) {
203
+ context.write(' /');
204
+ }
205
+ context.write('>');
206
+ },
189
207
  };
208
+
209
+ // Be careful when duplicating visitors that are already defined
210
+ // above in the `wrappers`
211
+ // if there is already a visitor but you still need a mapping
212
+ // on the whole node, only then duplicate it here
213
+ // e.g. JSXOpeningElement is such a case
190
214
  for (const type of [
191
215
  // JS nodes whose esrap printer emits no location marker, causing
192
216
  // segments.js get_mapping_from_node() to throw when it asks for the
@@ -214,7 +238,9 @@ export function tsx_with_ts_locations() {
214
238
  'TSTypeParameterDeclaration',
215
239
  'TSTypeParameter',
216
240
  ]) {
217
- wrappers[type] = (node, context) => wrap_with_locations(node, context, base[type]);
241
+ const visitor = wrappers[type];
242
+
243
+ wrappers[type] = (node, context) => wrap_with_locations(node, context, visitor ?? base[type]);
218
244
  }
219
245
 
220
246
  return { ...base, ...wrappers };
@@ -2209,7 +2209,12 @@ function to_jsx_element(node, transform_context, raw_children = node.children ||
2209
2209
  (/** @type {any} */ attribute) => attribute?.metadata?.has_unmappable_value,
2210
2210
  );
2211
2211
 
2212
- const opening_element_node = b.jsx_opening_element(name, attributes, selfClosing);
2212
+ const opening_element_node = b.jsx_opening_element(
2213
+ name,
2214
+ attributes,
2215
+ selfClosing,
2216
+ node.openingElement?.typeArguments,
2217
+ );
2213
2218
  const openingElement = has_unmappable_attribute
2214
2219
  ? opening_element_node
2215
2220
  : set_loc(opening_element_node, node.openingElement || node);
@@ -653,8 +653,11 @@ export function convert_source_map_to_mappings(
653
653
  // Nothing to visit (just source string)
654
654
  return;
655
655
  } else if (node.type === 'JSXOpeningElement') {
656
- // Visit name and attributes in source order
656
+ // Visit name, type arguments, and attributes in source order
657
657
  visit(node.name);
658
+ if (node.typeArguments) {
659
+ visit(node.typeArguments);
660
+ }
658
661
  for (const attr of node.attributes) {
659
662
  visit(attr);
660
663
  }
@@ -1107,15 +1107,23 @@ export function jsx_attribute(name, value = null, shorthand = false, loc_info) {
1107
1107
  * @param {ESTreeJSX.JSXOpeningElement['name']} name
1108
1108
  * @param {ESTreeJSX.JSXOpeningElement['attributes']} [attributes]
1109
1109
  * @param {boolean} [self_closing]
1110
+ * @param {ESTreeJSX.JSXOpeningElement['typeArguments']} [type_arguments]
1110
1111
  * @param {AST.NodeWithLocation} [loc_info]
1111
1112
  * @returns {ESTreeJSX.JSXOpeningElement}
1112
1113
  */
1113
- export function jsx_opening_element(name, attributes = [], self_closing = false, loc_info) {
1114
+ export function jsx_opening_element(
1115
+ name,
1116
+ attributes = [],
1117
+ self_closing = false,
1118
+ type_arguments = undefined,
1119
+ loc_info,
1120
+ ) {
1114
1121
  const node = /** @type {ESTreeJSX.JSXOpeningElement} */ ({
1115
1122
  type: 'JSXOpeningElement',
1116
1123
  name,
1117
1124
  attributes,
1118
1125
  selfClosing: self_closing,
1126
+ typeArguments: type_arguments,
1119
1127
  metadata: { path: [] },
1120
1128
  });
1121
1129