svelte2tsx 0.7.52 → 0.7.54

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 (3) hide show
  1. package/index.js +21 -14
  2. package/index.mjs +21 -14
  3. package/package.json +1 -2
package/index.js CHANGED
@@ -2810,12 +2810,9 @@ function handleAttribute(str, attr, parent, preserveCase, svelte5Plus, element)
2810
2810
  if (!needsNumberConversion) {
2811
2811
  attributeValue.push(quote);
2812
2812
  }
2813
- if (includesTemplateLiteralQuote && attrVal.data.split('\n').length > 1) {
2814
- // Multiline attribute value text which can't be wrapped in a template literal
2815
- // -> ensure it's still a valid transformation by transforming the actual line break
2816
- str.overwrite(attrVal.start, attrVal.end, attrVal.data.split('\n').join('\\n'), {
2817
- contentOnly: true
2818
- });
2813
+ const escapedValue = tryEscapeAttributeValue(attrVal.data, !includesTemplateLiteralQuote);
2814
+ if (escapedValue !== null) {
2815
+ str.overwrite(attrVal.start, attrVal.end, escapedValue, { contentOnly: true });
2819
2816
  }
2820
2817
  attributeValue.push([attrVal.start, attrVal.end]);
2821
2818
  if (!needsNumberConversion) {
@@ -2849,6 +2846,15 @@ function handleAttribute(str, attr, parent, preserveCase, svelte5Plus, element)
2849
2846
  function attributeValueIsOfType(value, type) {
2850
2847
  return value !== true && value.length == 1 && value[0].type == type;
2851
2848
  }
2849
+ function tryEscapeAttributeValue(str, useTemplateLiteral) {
2850
+ // Multiline attribute value text which can't be wrapped in a template literal
2851
+ // -> ensure it's still a valid transformation by transforming the actual line break
2852
+ // \ is not a valid escape in HTML, but it could be part of the attribute value and would break the generated code
2853
+ if (!str.includes('\\') && (useTemplateLiteral || !str.includes('\n'))) {
2854
+ return null;
2855
+ }
2856
+ return JSON.stringify(str).slice(1, -1);
2857
+ }
2852
2858
 
2853
2859
  /**
2854
2860
  * This needs to be called on the way out, not on the way on, when walking,
@@ -6706,12 +6712,12 @@ class HoistableInterfaces {
6706
6712
  const generics = (_b = (_a = node.typeParameters) === null || _a === void 0 ? void 0 : _a.map((param) => param.name.text)) !== null && _b !== void 0 ? _b : [];
6707
6713
  node.members.forEach((member) => {
6708
6714
  if (ts.isPropertySignature(member) && member.type) {
6709
- this.collectTypeDependencies(member.type, type_dependencies, value_dependencies, generics);
6715
+ this.collectTypeDependencies(member.type, type_dependencies, value_dependencies, generics, interface_name);
6710
6716
  }
6711
6717
  else if (ts.isIndexSignatureDeclaration(member)) {
6712
- this.collectTypeDependencies(member.type, type_dependencies, value_dependencies, generics);
6718
+ this.collectTypeDependencies(member.type, type_dependencies, value_dependencies, generics, interface_name);
6713
6719
  member.parameters.forEach((param) => {
6714
- this.collectTypeDependencies(param.type, type_dependencies, value_dependencies, generics);
6720
+ this.collectTypeDependencies(param.type, type_dependencies, value_dependencies, generics, interface_name);
6715
6721
  });
6716
6722
  }
6717
6723
  });
@@ -6723,7 +6729,7 @@ class HoistableInterfaces {
6723
6729
  type_dependencies.add(type_name);
6724
6730
  }
6725
6731
  }
6726
- this.collectTypeDependencies(type, type_dependencies, value_dependencies, generics);
6732
+ this.collectTypeDependencies(type, type_dependencies, value_dependencies, generics, interface_name);
6727
6733
  });
6728
6734
  });
6729
6735
  if (this.module_types.has(interface_name)) {
@@ -6744,7 +6750,7 @@ class HoistableInterfaces {
6744
6750
  const type_dependencies = new Set();
6745
6751
  const value_dependencies = new Set();
6746
6752
  const generics = (_e = (_d = node.typeParameters) === null || _d === void 0 ? void 0 : _d.map((param) => param.name.text)) !== null && _e !== void 0 ? _e : [];
6747
- this.collectTypeDependencies(node.type, type_dependencies, value_dependencies, generics);
6753
+ this.collectTypeDependencies(node.type, type_dependencies, value_dependencies, generics, alias_name);
6748
6754
  if (this.module_types.has(alias_name)) {
6749
6755
  // shadowed; we can't hoist
6750
6756
  this.disallowed_types.add(alias_name);
@@ -6809,7 +6815,7 @@ class HoistableInterfaces {
6809
6815
  else {
6810
6816
  this.props_interface.name = '$$ComponentProps';
6811
6817
  this.props_interface.node = generic_arg;
6812
- this.collectTypeDependencies(generic_arg, this.props_interface.type_deps, this.props_interface.value_deps, []);
6818
+ this.collectTypeDependencies(generic_arg, this.props_interface.type_deps, this.props_interface.value_deps, [], undefined);
6813
6819
  }
6814
6820
  }
6815
6821
  }
@@ -6921,11 +6927,12 @@ class HoistableInterfaces {
6921
6927
  * @param type_dependencies The set to collect type dependencies into.
6922
6928
  * @param value_dependencies The set to collect value dependencies into.
6923
6929
  */
6924
- collectTypeDependencies(type_node, type_dependencies, value_dependencies, generics) {
6930
+ collectTypeDependencies(type_node, type_dependencies, value_dependencies, generics, root_type_name) {
6925
6931
  const walk = (node) => {
6926
6932
  if (ts.isTypeReferenceNode(node)) {
6927
6933
  const type_name = this.getEntityNameRoot(node.typeName);
6928
- if (!generics.includes(type_name)) {
6934
+ const self_reference = type_name === root_type_name;
6935
+ if (!self_reference && !generics.includes(type_name)) {
6929
6936
  type_dependencies.add(type_name);
6930
6937
  }
6931
6938
  }
package/index.mjs CHANGED
@@ -2790,12 +2790,9 @@ function handleAttribute(str, attr, parent, preserveCase, svelte5Plus, element)
2790
2790
  if (!needsNumberConversion) {
2791
2791
  attributeValue.push(quote);
2792
2792
  }
2793
- if (includesTemplateLiteralQuote && attrVal.data.split('\n').length > 1) {
2794
- // Multiline attribute value text which can't be wrapped in a template literal
2795
- // -> ensure it's still a valid transformation by transforming the actual line break
2796
- str.overwrite(attrVal.start, attrVal.end, attrVal.data.split('\n').join('\\n'), {
2797
- contentOnly: true
2798
- });
2793
+ const escapedValue = tryEscapeAttributeValue(attrVal.data, !includesTemplateLiteralQuote);
2794
+ if (escapedValue !== null) {
2795
+ str.overwrite(attrVal.start, attrVal.end, escapedValue, { contentOnly: true });
2799
2796
  }
2800
2797
  attributeValue.push([attrVal.start, attrVal.end]);
2801
2798
  if (!needsNumberConversion) {
@@ -2829,6 +2826,15 @@ function handleAttribute(str, attr, parent, preserveCase, svelte5Plus, element)
2829
2826
  function attributeValueIsOfType(value, type) {
2830
2827
  return value !== true && value.length == 1 && value[0].type == type;
2831
2828
  }
2829
+ function tryEscapeAttributeValue(str, useTemplateLiteral) {
2830
+ // Multiline attribute value text which can't be wrapped in a template literal
2831
+ // -> ensure it's still a valid transformation by transforming the actual line break
2832
+ // \ is not a valid escape in HTML, but it could be part of the attribute value and would break the generated code
2833
+ if (!str.includes('\\') && (useTemplateLiteral || !str.includes('\n'))) {
2834
+ return null;
2835
+ }
2836
+ return JSON.stringify(str).slice(1, -1);
2837
+ }
2832
2838
 
2833
2839
  /**
2834
2840
  * This needs to be called on the way out, not on the way on, when walking,
@@ -6686,12 +6692,12 @@ class HoistableInterfaces {
6686
6692
  const generics = (_b = (_a = node.typeParameters) === null || _a === void 0 ? void 0 : _a.map((param) => param.name.text)) !== null && _b !== void 0 ? _b : [];
6687
6693
  node.members.forEach((member) => {
6688
6694
  if (ts.isPropertySignature(member) && member.type) {
6689
- this.collectTypeDependencies(member.type, type_dependencies, value_dependencies, generics);
6695
+ this.collectTypeDependencies(member.type, type_dependencies, value_dependencies, generics, interface_name);
6690
6696
  }
6691
6697
  else if (ts.isIndexSignatureDeclaration(member)) {
6692
- this.collectTypeDependencies(member.type, type_dependencies, value_dependencies, generics);
6698
+ this.collectTypeDependencies(member.type, type_dependencies, value_dependencies, generics, interface_name);
6693
6699
  member.parameters.forEach((param) => {
6694
- this.collectTypeDependencies(param.type, type_dependencies, value_dependencies, generics);
6700
+ this.collectTypeDependencies(param.type, type_dependencies, value_dependencies, generics, interface_name);
6695
6701
  });
6696
6702
  }
6697
6703
  });
@@ -6703,7 +6709,7 @@ class HoistableInterfaces {
6703
6709
  type_dependencies.add(type_name);
6704
6710
  }
6705
6711
  }
6706
- this.collectTypeDependencies(type, type_dependencies, value_dependencies, generics);
6712
+ this.collectTypeDependencies(type, type_dependencies, value_dependencies, generics, interface_name);
6707
6713
  });
6708
6714
  });
6709
6715
  if (this.module_types.has(interface_name)) {
@@ -6724,7 +6730,7 @@ class HoistableInterfaces {
6724
6730
  const type_dependencies = new Set();
6725
6731
  const value_dependencies = new Set();
6726
6732
  const generics = (_e = (_d = node.typeParameters) === null || _d === void 0 ? void 0 : _d.map((param) => param.name.text)) !== null && _e !== void 0 ? _e : [];
6727
- this.collectTypeDependencies(node.type, type_dependencies, value_dependencies, generics);
6733
+ this.collectTypeDependencies(node.type, type_dependencies, value_dependencies, generics, alias_name);
6728
6734
  if (this.module_types.has(alias_name)) {
6729
6735
  // shadowed; we can't hoist
6730
6736
  this.disallowed_types.add(alias_name);
@@ -6789,7 +6795,7 @@ class HoistableInterfaces {
6789
6795
  else {
6790
6796
  this.props_interface.name = '$$ComponentProps';
6791
6797
  this.props_interface.node = generic_arg;
6792
- this.collectTypeDependencies(generic_arg, this.props_interface.type_deps, this.props_interface.value_deps, []);
6798
+ this.collectTypeDependencies(generic_arg, this.props_interface.type_deps, this.props_interface.value_deps, [], undefined);
6793
6799
  }
6794
6800
  }
6795
6801
  }
@@ -6901,11 +6907,12 @@ class HoistableInterfaces {
6901
6907
  * @param type_dependencies The set to collect type dependencies into.
6902
6908
  * @param value_dependencies The set to collect value dependencies into.
6903
6909
  */
6904
- collectTypeDependencies(type_node, type_dependencies, value_dependencies, generics) {
6910
+ collectTypeDependencies(type_node, type_dependencies, value_dependencies, generics, root_type_name) {
6905
6911
  const walk = (node) => {
6906
6912
  if (ts.isTypeReferenceNode(node)) {
6907
6913
  const type_name = this.getEntityNameRoot(node.typeName);
6908
- if (!generics.includes(type_name)) {
6914
+ const self_reference = type_name === root_type_name;
6915
+ if (!self_reference && !generics.includes(type_name)) {
6909
6916
  type_dependencies.add(type_name);
6910
6917
  }
6911
6918
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte2tsx",
3
- "version": "0.7.52",
3
+ "version": "0.7.54",
4
4
  "description": "Convert Svelte components to TSX for type checking",
5
5
  "author": "The Svelte Community",
6
6
  "license": "MIT",
@@ -29,7 +29,6 @@
29
29
  "@types/node": "^18.0.0",
30
30
  "@types/unist": "^2.0.3",
31
31
  "@types/vfile": "^3.0.2",
32
- "builtin-modules": "^3.3.0",
33
32
  "estree-walker": "^2.0.1",
34
33
  "magic-string": "^0.30.11",
35
34
  "mocha": "^9.2.0",