svelte2tsx 0.7.31 → 0.7.33

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 +71 -86
  2. package/index.mjs +71 -86
  3. package/package.json +2 -2
package/index.js CHANGED
@@ -5212,7 +5212,7 @@ function addComponentExport(params) {
5212
5212
  addSimpleComponentExport(params);
5213
5213
  }
5214
5214
  }
5215
- function addGenericsComponentExport({ events, canHaveAnyProp, exportedNames, componentDocumentation, fileName, mode, usesAccessors, str, generics, usesSlots, isSvelte5, noSvelteComponentTyped }) {
5215
+ function addGenericsComponentExport({ events, canHaveAnyProp, exportedNames, componentDocumentation, fileName, mode, usesAccessors, isTsFile, str, generics, usesSlots, isSvelte5, noSvelteComponentTyped }) {
5216
5216
  const genericsDef = generics.toDefinitionString();
5217
5217
  const genericsRef = generics.toReferencesString();
5218
5218
  const doc = componentDocumentation.getFormatted();
@@ -5220,6 +5220,8 @@ function addGenericsComponentExport({ events, canHaveAnyProp, exportedNames, com
5220
5220
  function returnType(forPart) {
5221
5221
  return `ReturnType<__sveltets_Render${genericsRef}['${forPart}']>`;
5222
5222
  }
5223
+ // TODO once Svelte 4 compatibility is dropped, we can simplify this, because since TS 4.7 it is possible to use generics
5224
+ // like this: `typeof render<T>` - which wasn't possibly before, hence the class + methods workaround.
5223
5225
  let statement = `
5224
5226
  class __sveltets_Render${genericsDef} {
5225
5227
  props() {
@@ -5231,12 +5233,21 @@ class __sveltets_Render${genericsDef} {
5231
5233
  slots() {
5232
5234
  return render${genericsRef}().slots;
5233
5235
  }
5234
- ${isSvelte5
5236
+ `;
5237
+ // For Svelte 5+ we assume TS > 4.7
5238
+ if (isSvelte5 && !isTsFile && exportedNames.usesRunes()) {
5239
+ statement = `
5240
+ class __sveltets_Render${genericsDef} {
5241
+ props(): ReturnType<typeof render${genericsRef}>['props'] { return null as any; }
5242
+ events(): ReturnType<typeof render${genericsRef}>['events'] { return null as any; }
5243
+ slots(): ReturnType<typeof render${genericsRef}>['slots'] { return null as any; }
5244
+ `;
5245
+ }
5246
+ statement += isSvelte5
5235
5247
  ? ` bindings() { return ${exportedNames.createBindingsStr()}; }
5236
5248
  exports() { return ${exportedNames.hasExports() ? `render${genericsRef}().exports` : '{}'}; }
5237
- }`
5238
- : '}'}
5239
- `;
5249
+ }\n`
5250
+ : '}\n';
5240
5251
  const svelteComponentClass = noSvelteComponentTyped
5241
5252
  ? 'SvelteComponent'
5242
5253
  : 'SvelteComponentTyped';
@@ -5462,7 +5473,7 @@ function classNameFromFilename(filename, appendSuffix) {
5462
5473
  }
5463
5474
  }
5464
5475
 
5465
- function createRenderFunction({ str, scriptTag, scriptDestination, slots, events, exportedNames, uses$$props, uses$$restProps, uses$$slots, uses$$SlotsInterface, generics, mode }) {
5476
+ function createRenderFunction({ str, scriptTag, scriptDestination, slots, events, exportedNames, uses$$props, uses$$restProps, uses$$slots, uses$$SlotsInterface, generics, isTsFile, mode }) {
5466
5477
  const htmlx = str.original;
5467
5478
  let propsDecl = '';
5468
5479
  if (uses$$props) {
@@ -5497,8 +5508,8 @@ function createRenderFunction({ str, scriptTag, scriptDestination, slots, events
5497
5508
  end--;
5498
5509
  }
5499
5510
  str.overwrite(scriptTag.start + 1, start - 1, `function render`);
5500
- str.overwrite(start - 1, start, `<`); // if the generics are unused, only this char is colored opaque
5501
- str.overwrite(end, scriptTagEnd, `>() {${propsDecl}\n`);
5511
+ str.overwrite(start - 1, start, isTsFile ? '<' : `<${IGNORE_START_COMMENT}`); // if the generics are unused, only this char is colored opaque
5512
+ str.overwrite(end, scriptTagEnd, `>${isTsFile ? '' : IGNORE_END_COMMENT}() {${propsDecl}\n`);
5502
5513
  }
5503
5514
  else {
5504
5515
  str.overwrite(scriptTag.start + 1, scriptTagEnd, `function render${generics.toDefinitionString(true)}() {${propsDecl}\n`);
@@ -5998,8 +6009,9 @@ function getCurrentPrepends(str, index) {
5998
6009
  */
5999
6010
  class HoistableInterfaces {
6000
6011
  constructor() {
6001
- this.import_value_set = new Set();
6002
- this.import_type_set = new Set();
6012
+ this.module_types = new Set();
6013
+ this.disallowed_types = new Set();
6014
+ this.disallowed_values = new Set();
6003
6015
  this.interface_map = new Map();
6004
6016
  this.props_interface = {
6005
6017
  name: '',
@@ -6008,6 +6020,7 @@ class HoistableInterfaces {
6008
6020
  value_deps: new Set()
6009
6021
  };
6010
6022
  }
6023
+ /** should be called before analyzeInstanceScriptNode */
6011
6024
  analyzeModuleScriptNode(node) {
6012
6025
  // Handle Import Declarations
6013
6026
  if (ts.isImportDeclaration(node) && node.importClause) {
@@ -6017,10 +6030,7 @@ class HoistableInterfaces {
6017
6030
  node.importClause.namedBindings.elements.forEach((element) => {
6018
6031
  const import_name = element.name.text;
6019
6032
  if (is_type_only || element.isTypeOnly) {
6020
- this.import_type_set.add(import_name);
6021
- }
6022
- else {
6023
- this.import_value_set.add(import_name);
6033
+ this.module_types.add(import_name);
6024
6034
  }
6025
6035
  });
6026
6036
  }
@@ -6028,10 +6038,7 @@ class HoistableInterfaces {
6028
6038
  if (node.importClause.name) {
6029
6039
  const default_import = node.importClause.name.text;
6030
6040
  if (is_type_only) {
6031
- this.import_type_set.add(default_import);
6032
- }
6033
- else {
6034
- this.import_value_set.add(default_import);
6041
+ this.module_types.add(default_import);
6035
6042
  }
6036
6043
  }
6037
6044
  // Handle namespace imports
@@ -6039,35 +6046,15 @@ class HoistableInterfaces {
6039
6046
  ts.isNamespaceImport(node.importClause.namedBindings)) {
6040
6047
  const namespace_import = node.importClause.namedBindings.name.text;
6041
6048
  if (is_type_only) {
6042
- this.import_type_set.add(namespace_import);
6043
- }
6044
- else {
6045
- this.import_value_set.add(namespace_import);
6049
+ this.module_types.add(namespace_import);
6046
6050
  }
6047
6051
  }
6048
6052
  }
6049
- // Handle top-level declarations
6050
- if (ts.isVariableStatement(node)) {
6051
- node.declarationList.declarations.forEach((declaration) => {
6052
- if (ts.isIdentifier(declaration.name)) {
6053
- this.import_value_set.add(declaration.name.text);
6054
- }
6055
- });
6056
- }
6057
- if (ts.isFunctionDeclaration(node) && node.name) {
6058
- this.import_value_set.add(node.name.text);
6059
- }
6060
- if (ts.isClassDeclaration(node) && node.name) {
6061
- this.import_value_set.add(node.name.text);
6062
- }
6063
- if (ts.isEnumDeclaration(node)) {
6064
- this.import_value_set.add(node.name.text);
6065
- }
6066
6053
  if (ts.isTypeAliasDeclaration(node)) {
6067
- this.import_type_set.add(node.name.text);
6054
+ this.module_types.add(node.name.text);
6068
6055
  }
6069
6056
  if (ts.isInterfaceDeclaration(node)) {
6070
- this.import_type_set.add(node.name.text);
6057
+ this.module_types.add(node.name.text);
6071
6058
  }
6072
6059
  }
6073
6060
  analyzeInstanceScriptNode(node) {
@@ -6080,10 +6067,7 @@ class HoistableInterfaces {
6080
6067
  node.importClause.namedBindings.elements.forEach((element) => {
6081
6068
  const import_name = element.name.text;
6082
6069
  if (is_type_only) {
6083
- this.import_type_set.add(import_name);
6084
- }
6085
- else {
6086
- this.import_value_set.add(import_name);
6070
+ this.module_types.add(import_name);
6087
6071
  }
6088
6072
  });
6089
6073
  }
@@ -6091,10 +6075,7 @@ class HoistableInterfaces {
6091
6075
  if (node.importClause.name) {
6092
6076
  const default_import = node.importClause.name.text;
6093
6077
  if (is_type_only) {
6094
- this.import_type_set.add(default_import);
6095
- }
6096
- else {
6097
- this.import_value_set.add(default_import);
6078
+ this.module_types.add(default_import);
6098
6079
  }
6099
6080
  }
6100
6081
  // Handle namespace imports
@@ -6102,10 +6083,7 @@ class HoistableInterfaces {
6102
6083
  ts.isNamespaceImport(node.importClause.namedBindings)) {
6103
6084
  const namespace_import = node.importClause.namedBindings.name.text;
6104
6085
  if (is_type_only) {
6105
- this.import_type_set.add(namespace_import);
6106
- }
6107
- else {
6108
- this.import_value_set.add(namespace_import);
6086
+ this.module_types.add(namespace_import);
6109
6087
  }
6110
6088
  }
6111
6089
  }
@@ -6126,9 +6104,9 @@ class HoistableInterfaces {
6126
6104
  });
6127
6105
  }
6128
6106
  });
6129
- if (this.import_type_set.has(interface_name)) {
6130
- // shadowed; delete because we can't hoist
6131
- this.import_type_set.delete(interface_name);
6107
+ if (this.module_types.has(interface_name)) {
6108
+ // shadowed; we can't hoist
6109
+ this.disallowed_types.add(interface_name);
6132
6110
  }
6133
6111
  else {
6134
6112
  this.interface_map.set(interface_name, {
@@ -6145,9 +6123,9 @@ class HoistableInterfaces {
6145
6123
  const value_dependencies = new Set();
6146
6124
  const generics = (_d = (_c = node.typeParameters) === null || _c === void 0 ? void 0 : _c.map((param) => param.name.text)) !== null && _d !== void 0 ? _d : [];
6147
6125
  this.collectTypeDependencies(node.type, type_dependencies, value_dependencies, generics);
6148
- if (this.import_type_set.has(alias_name)) {
6149
- // shadowed; delete because we can't hoist
6150
- this.import_type_set.delete(alias_name);
6126
+ if (this.module_types.has(alias_name)) {
6127
+ // shadowed; we can't hoist
6128
+ this.disallowed_types.add(alias_name);
6151
6129
  }
6152
6130
  else {
6153
6131
  this.interface_map.set(alias_name, {
@@ -6161,24 +6139,18 @@ class HoistableInterfaces {
6161
6139
  if (ts.isVariableStatement(node)) {
6162
6140
  node.declarationList.declarations.forEach((declaration) => {
6163
6141
  if (ts.isIdentifier(declaration.name)) {
6164
- this.import_value_set.delete(declaration.name.text);
6142
+ this.disallowed_values.add(declaration.name.text);
6165
6143
  }
6166
6144
  });
6167
6145
  }
6168
6146
  if (ts.isFunctionDeclaration(node) && node.name) {
6169
- this.import_value_set.delete(node.name.text);
6147
+ this.disallowed_values.add(node.name.text);
6170
6148
  }
6171
6149
  if (ts.isClassDeclaration(node) && node.name) {
6172
- this.import_value_set.delete(node.name.text);
6150
+ this.disallowed_values.add(node.name.text);
6173
6151
  }
6174
6152
  if (ts.isEnumDeclaration(node)) {
6175
- this.import_value_set.delete(node.name.text);
6176
- }
6177
- if (ts.isTypeAliasDeclaration(node)) {
6178
- this.import_type_set.delete(node.name.text);
6179
- }
6180
- if (ts.isInterfaceDeclaration(node)) {
6181
- this.import_type_set.delete(node.name.text);
6153
+ this.disallowed_values.add(node.name.text);
6182
6154
  }
6183
6155
  }
6184
6156
  analyze$propsRune(node) {
@@ -6216,11 +6188,24 @@ class HoistableInterfaces {
6216
6188
  if (hoistable_interfaces.has(interface_name)) {
6217
6189
  continue;
6218
6190
  }
6219
- const can_hoist = [...deps.type_deps, ...deps.value_deps].every((dep) => {
6220
- return (this.import_type_set.has(dep) ||
6221
- this.import_value_set.has(dep) ||
6222
- hoistable_interfaces.has(dep));
6223
- });
6191
+ let can_hoist = true;
6192
+ for (const dep of deps.type_deps) {
6193
+ if (this.disallowed_types.has(dep)) {
6194
+ this.disallowed_types.add(interface_name);
6195
+ can_hoist = false;
6196
+ break;
6197
+ }
6198
+ if (this.interface_map.has(dep) && !hoistable_interfaces.has(dep)) {
6199
+ can_hoist = false;
6200
+ }
6201
+ }
6202
+ for (const dep of deps.value_deps) {
6203
+ if (this.disallowed_values.has(dep)) {
6204
+ this.disallowed_types.add(interface_name);
6205
+ can_hoist = false;
6206
+ break;
6207
+ }
6208
+ }
6224
6209
  if (can_hoist) {
6225
6210
  hoistable_interfaces.set(interface_name, deps.node);
6226
6211
  progress = true;
@@ -6232,9 +6217,7 @@ class HoistableInterfaces {
6232
6217
  ...this.props_interface.type_deps,
6233
6218
  ...this.props_interface.value_deps
6234
6219
  ].every((dep) => {
6235
- return (this.import_type_set.has(dep) ||
6236
- this.import_value_set.has(dep) ||
6237
- hoistable_interfaces.has(dep));
6220
+ return !this.disallowed_types.has(dep) && !this.disallowed_values.has(dep);
6238
6221
  });
6239
6222
  if (can_hoist) {
6240
6223
  hoistable_interfaces.set(this.props_interface.name, this.props_interface.node);
@@ -6249,7 +6232,7 @@ class HoistableInterfaces {
6249
6232
  if (!this.props_interface.name)
6250
6233
  return;
6251
6234
  for (const generic of generics) {
6252
- this.import_type_set.delete(generic);
6235
+ this.disallowed_types.add(generic);
6253
6236
  }
6254
6237
  const hoistable = this.determineHoistableInterfaces();
6255
6238
  if (hoistable.has(this.props_interface.name)) {
@@ -6277,8 +6260,8 @@ class HoistableInterfaces {
6277
6260
  return hoistable;
6278
6261
  }
6279
6262
  }
6280
- getAllowedValues() {
6281
- return this.import_value_set;
6263
+ isAllowedReference(reference) {
6264
+ return !this.disallowed_values.has(reference);
6282
6265
  }
6283
6266
  /**
6284
6267
  * Collects type and value dependencies from a given TypeNode.
@@ -7747,6 +7730,7 @@ function svelte2tsx(svelte, options = { parse: compiler.parse }) {
7747
7730
  const str = new MagicString(svelte);
7748
7731
  const basename = path.basename(options.filename || '');
7749
7732
  const svelte5Plus = Number(options.version[0]) > 4;
7733
+ const isTsFile = options === null || options === void 0 ? void 0 : options.isTsFile;
7750
7734
  // process the htmlx as a svelte template
7751
7735
  let { htmlAst, moduleScriptTag, scriptTag, rootSnippets, slots, uses$$props, uses$$slots, uses$$restProps, events, componentDocumentation, resolvedStores, usesAccessors, isRunes } = processSvelteTemplate(str, options.parse || compiler.parse, {
7752
7736
  ...options,
@@ -7775,7 +7759,7 @@ function svelte2tsx(svelte, options = { parse: compiler.parse }) {
7775
7759
  : instanceScriptTarget;
7776
7760
  const implicitStoreValues = new ImplicitStoreValues(resolvedStores, renderFunctionStart);
7777
7761
  //move the instance script and process the content
7778
- let exportedNames = new ExportedNames(str, 0, basename, options === null || options === void 0 ? void 0 : options.isTsFile, svelte5Plus, isRunes);
7762
+ let exportedNames = new ExportedNames(str, 0, basename, isTsFile, svelte5Plus, isRunes);
7779
7763
  let generics = new Generics(str, 0, { attributes: [] });
7780
7764
  let uses$$SlotsInterface = false;
7781
7765
  if (scriptTag) {
@@ -7783,7 +7767,7 @@ function svelte2tsx(svelte, options = { parse: compiler.parse }) {
7783
7767
  if (scriptTag.start != instanceScriptTarget) {
7784
7768
  str.move(scriptTag.start, scriptTag.end, instanceScriptTarget);
7785
7769
  }
7786
- const res = processInstanceScriptContent(str, scriptTag, events, implicitStoreValues, options.mode, moduleAst, options === null || options === void 0 ? void 0 : options.isTsFile, basename, svelte5Plus, isRunes);
7770
+ const res = processInstanceScriptContent(str, scriptTag, events, implicitStoreValues, options.mode, moduleAst, isTsFile, basename, svelte5Plus, isRunes);
7787
7771
  uses$$props = uses$$props || res.uses$$props;
7788
7772
  uses$$restProps = uses$$restProps || res.uses$$restProps;
7789
7773
  uses$$slots = uses$$slots || res.uses$$slots;
@@ -7807,6 +7791,7 @@ function svelte2tsx(svelte, options = { parse: compiler.parse }) {
7807
7791
  uses$$SlotsInterface,
7808
7792
  generics,
7809
7793
  svelte5Plus,
7794
+ isTsFile,
7810
7795
  mode: options.mode
7811
7796
  });
7812
7797
  // we need to process the module script after the instance script has moved otherwise we get warnings about moving edited items
@@ -7817,14 +7802,14 @@ function svelte2tsx(svelte, options = { parse: compiler.parse }) {
7817
7802
  }
7818
7803
  }
7819
7804
  if (moduleScriptTag || scriptTag) {
7820
- const allowed = exportedNames.hoistableInterfaces.getAllowedValues();
7821
7805
  for (const [start, end, globals] of rootSnippets) {
7822
7806
  const hoist_to_module = moduleScriptTag &&
7823
- (globals.size === 0 || [...globals.keys()].every((id) => allowed.has(id)));
7807
+ (globals.size === 0 ||
7808
+ [...globals.keys()].every((id) => exportedNames.hoistableInterfaces.isAllowedReference(id)));
7824
7809
  if (hoist_to_module) {
7825
7810
  str.move(start, end, scriptTag
7826
7811
  ? scriptTag.start + 1 // +1 because imports are also moved at that position, and we want to move interfaces after imports
7827
- : moduleScriptTag.end);
7812
+ : instanceScriptTarget);
7828
7813
  }
7829
7814
  else if (scriptTag) {
7830
7815
  str.move(start, end, renderFunctionStart);
@@ -7835,7 +7820,7 @@ function svelte2tsx(svelte, options = { parse: compiler.parse }) {
7835
7820
  str,
7836
7821
  canHaveAnyProp: !exportedNames.uses$$Props && (uses$$props || uses$$restProps),
7837
7822
  events,
7838
- isTsFile: options === null || options === void 0 ? void 0 : options.isTsFile,
7823
+ isTsFile,
7839
7824
  exportedNames,
7840
7825
  usesAccessors,
7841
7826
  usesSlots: slots.size > 0,
package/index.mjs CHANGED
@@ -5192,7 +5192,7 @@ function addComponentExport(params) {
5192
5192
  addSimpleComponentExport(params);
5193
5193
  }
5194
5194
  }
5195
- function addGenericsComponentExport({ events, canHaveAnyProp, exportedNames, componentDocumentation, fileName, mode, usesAccessors, str, generics, usesSlots, isSvelte5, noSvelteComponentTyped }) {
5195
+ function addGenericsComponentExport({ events, canHaveAnyProp, exportedNames, componentDocumentation, fileName, mode, usesAccessors, isTsFile, str, generics, usesSlots, isSvelte5, noSvelteComponentTyped }) {
5196
5196
  const genericsDef = generics.toDefinitionString();
5197
5197
  const genericsRef = generics.toReferencesString();
5198
5198
  const doc = componentDocumentation.getFormatted();
@@ -5200,6 +5200,8 @@ function addGenericsComponentExport({ events, canHaveAnyProp, exportedNames, com
5200
5200
  function returnType(forPart) {
5201
5201
  return `ReturnType<__sveltets_Render${genericsRef}['${forPart}']>`;
5202
5202
  }
5203
+ // TODO once Svelte 4 compatibility is dropped, we can simplify this, because since TS 4.7 it is possible to use generics
5204
+ // like this: `typeof render<T>` - which wasn't possibly before, hence the class + methods workaround.
5203
5205
  let statement = `
5204
5206
  class __sveltets_Render${genericsDef} {
5205
5207
  props() {
@@ -5211,12 +5213,21 @@ class __sveltets_Render${genericsDef} {
5211
5213
  slots() {
5212
5214
  return render${genericsRef}().slots;
5213
5215
  }
5214
- ${isSvelte5
5216
+ `;
5217
+ // For Svelte 5+ we assume TS > 4.7
5218
+ if (isSvelte5 && !isTsFile && exportedNames.usesRunes()) {
5219
+ statement = `
5220
+ class __sveltets_Render${genericsDef} {
5221
+ props(): ReturnType<typeof render${genericsRef}>['props'] { return null as any; }
5222
+ events(): ReturnType<typeof render${genericsRef}>['events'] { return null as any; }
5223
+ slots(): ReturnType<typeof render${genericsRef}>['slots'] { return null as any; }
5224
+ `;
5225
+ }
5226
+ statement += isSvelte5
5215
5227
  ? ` bindings() { return ${exportedNames.createBindingsStr()}; }
5216
5228
  exports() { return ${exportedNames.hasExports() ? `render${genericsRef}().exports` : '{}'}; }
5217
- }`
5218
- : '}'}
5219
- `;
5229
+ }\n`
5230
+ : '}\n';
5220
5231
  const svelteComponentClass = noSvelteComponentTyped
5221
5232
  ? 'SvelteComponent'
5222
5233
  : 'SvelteComponentTyped';
@@ -5442,7 +5453,7 @@ function classNameFromFilename(filename, appendSuffix) {
5442
5453
  }
5443
5454
  }
5444
5455
 
5445
- function createRenderFunction({ str, scriptTag, scriptDestination, slots, events, exportedNames, uses$$props, uses$$restProps, uses$$slots, uses$$SlotsInterface, generics, mode }) {
5456
+ function createRenderFunction({ str, scriptTag, scriptDestination, slots, events, exportedNames, uses$$props, uses$$restProps, uses$$slots, uses$$SlotsInterface, generics, isTsFile, mode }) {
5446
5457
  const htmlx = str.original;
5447
5458
  let propsDecl = '';
5448
5459
  if (uses$$props) {
@@ -5477,8 +5488,8 @@ function createRenderFunction({ str, scriptTag, scriptDestination, slots, events
5477
5488
  end--;
5478
5489
  }
5479
5490
  str.overwrite(scriptTag.start + 1, start - 1, `function render`);
5480
- str.overwrite(start - 1, start, `<`); // if the generics are unused, only this char is colored opaque
5481
- str.overwrite(end, scriptTagEnd, `>() {${propsDecl}\n`);
5491
+ str.overwrite(start - 1, start, isTsFile ? '<' : `<${IGNORE_START_COMMENT}`); // if the generics are unused, only this char is colored opaque
5492
+ str.overwrite(end, scriptTagEnd, `>${isTsFile ? '' : IGNORE_END_COMMENT}() {${propsDecl}\n`);
5482
5493
  }
5483
5494
  else {
5484
5495
  str.overwrite(scriptTag.start + 1, scriptTagEnd, `function render${generics.toDefinitionString(true)}() {${propsDecl}\n`);
@@ -5978,8 +5989,9 @@ function getCurrentPrepends(str, index) {
5978
5989
  */
5979
5990
  class HoistableInterfaces {
5980
5991
  constructor() {
5981
- this.import_value_set = new Set();
5982
- this.import_type_set = new Set();
5992
+ this.module_types = new Set();
5993
+ this.disallowed_types = new Set();
5994
+ this.disallowed_values = new Set();
5983
5995
  this.interface_map = new Map();
5984
5996
  this.props_interface = {
5985
5997
  name: '',
@@ -5988,6 +6000,7 @@ class HoistableInterfaces {
5988
6000
  value_deps: new Set()
5989
6001
  };
5990
6002
  }
6003
+ /** should be called before analyzeInstanceScriptNode */
5991
6004
  analyzeModuleScriptNode(node) {
5992
6005
  // Handle Import Declarations
5993
6006
  if (ts.isImportDeclaration(node) && node.importClause) {
@@ -5997,10 +6010,7 @@ class HoistableInterfaces {
5997
6010
  node.importClause.namedBindings.elements.forEach((element) => {
5998
6011
  const import_name = element.name.text;
5999
6012
  if (is_type_only || element.isTypeOnly) {
6000
- this.import_type_set.add(import_name);
6001
- }
6002
- else {
6003
- this.import_value_set.add(import_name);
6013
+ this.module_types.add(import_name);
6004
6014
  }
6005
6015
  });
6006
6016
  }
@@ -6008,10 +6018,7 @@ class HoistableInterfaces {
6008
6018
  if (node.importClause.name) {
6009
6019
  const default_import = node.importClause.name.text;
6010
6020
  if (is_type_only) {
6011
- this.import_type_set.add(default_import);
6012
- }
6013
- else {
6014
- this.import_value_set.add(default_import);
6021
+ this.module_types.add(default_import);
6015
6022
  }
6016
6023
  }
6017
6024
  // Handle namespace imports
@@ -6019,35 +6026,15 @@ class HoistableInterfaces {
6019
6026
  ts.isNamespaceImport(node.importClause.namedBindings)) {
6020
6027
  const namespace_import = node.importClause.namedBindings.name.text;
6021
6028
  if (is_type_only) {
6022
- this.import_type_set.add(namespace_import);
6023
- }
6024
- else {
6025
- this.import_value_set.add(namespace_import);
6029
+ this.module_types.add(namespace_import);
6026
6030
  }
6027
6031
  }
6028
6032
  }
6029
- // Handle top-level declarations
6030
- if (ts.isVariableStatement(node)) {
6031
- node.declarationList.declarations.forEach((declaration) => {
6032
- if (ts.isIdentifier(declaration.name)) {
6033
- this.import_value_set.add(declaration.name.text);
6034
- }
6035
- });
6036
- }
6037
- if (ts.isFunctionDeclaration(node) && node.name) {
6038
- this.import_value_set.add(node.name.text);
6039
- }
6040
- if (ts.isClassDeclaration(node) && node.name) {
6041
- this.import_value_set.add(node.name.text);
6042
- }
6043
- if (ts.isEnumDeclaration(node)) {
6044
- this.import_value_set.add(node.name.text);
6045
- }
6046
6033
  if (ts.isTypeAliasDeclaration(node)) {
6047
- this.import_type_set.add(node.name.text);
6034
+ this.module_types.add(node.name.text);
6048
6035
  }
6049
6036
  if (ts.isInterfaceDeclaration(node)) {
6050
- this.import_type_set.add(node.name.text);
6037
+ this.module_types.add(node.name.text);
6051
6038
  }
6052
6039
  }
6053
6040
  analyzeInstanceScriptNode(node) {
@@ -6060,10 +6047,7 @@ class HoistableInterfaces {
6060
6047
  node.importClause.namedBindings.elements.forEach((element) => {
6061
6048
  const import_name = element.name.text;
6062
6049
  if (is_type_only) {
6063
- this.import_type_set.add(import_name);
6064
- }
6065
- else {
6066
- this.import_value_set.add(import_name);
6050
+ this.module_types.add(import_name);
6067
6051
  }
6068
6052
  });
6069
6053
  }
@@ -6071,10 +6055,7 @@ class HoistableInterfaces {
6071
6055
  if (node.importClause.name) {
6072
6056
  const default_import = node.importClause.name.text;
6073
6057
  if (is_type_only) {
6074
- this.import_type_set.add(default_import);
6075
- }
6076
- else {
6077
- this.import_value_set.add(default_import);
6058
+ this.module_types.add(default_import);
6078
6059
  }
6079
6060
  }
6080
6061
  // Handle namespace imports
@@ -6082,10 +6063,7 @@ class HoistableInterfaces {
6082
6063
  ts.isNamespaceImport(node.importClause.namedBindings)) {
6083
6064
  const namespace_import = node.importClause.namedBindings.name.text;
6084
6065
  if (is_type_only) {
6085
- this.import_type_set.add(namespace_import);
6086
- }
6087
- else {
6088
- this.import_value_set.add(namespace_import);
6066
+ this.module_types.add(namespace_import);
6089
6067
  }
6090
6068
  }
6091
6069
  }
@@ -6106,9 +6084,9 @@ class HoistableInterfaces {
6106
6084
  });
6107
6085
  }
6108
6086
  });
6109
- if (this.import_type_set.has(interface_name)) {
6110
- // shadowed; delete because we can't hoist
6111
- this.import_type_set.delete(interface_name);
6087
+ if (this.module_types.has(interface_name)) {
6088
+ // shadowed; we can't hoist
6089
+ this.disallowed_types.add(interface_name);
6112
6090
  }
6113
6091
  else {
6114
6092
  this.interface_map.set(interface_name, {
@@ -6125,9 +6103,9 @@ class HoistableInterfaces {
6125
6103
  const value_dependencies = new Set();
6126
6104
  const generics = (_d = (_c = node.typeParameters) === null || _c === void 0 ? void 0 : _c.map((param) => param.name.text)) !== null && _d !== void 0 ? _d : [];
6127
6105
  this.collectTypeDependencies(node.type, type_dependencies, value_dependencies, generics);
6128
- if (this.import_type_set.has(alias_name)) {
6129
- // shadowed; delete because we can't hoist
6130
- this.import_type_set.delete(alias_name);
6106
+ if (this.module_types.has(alias_name)) {
6107
+ // shadowed; we can't hoist
6108
+ this.disallowed_types.add(alias_name);
6131
6109
  }
6132
6110
  else {
6133
6111
  this.interface_map.set(alias_name, {
@@ -6141,24 +6119,18 @@ class HoistableInterfaces {
6141
6119
  if (ts.isVariableStatement(node)) {
6142
6120
  node.declarationList.declarations.forEach((declaration) => {
6143
6121
  if (ts.isIdentifier(declaration.name)) {
6144
- this.import_value_set.delete(declaration.name.text);
6122
+ this.disallowed_values.add(declaration.name.text);
6145
6123
  }
6146
6124
  });
6147
6125
  }
6148
6126
  if (ts.isFunctionDeclaration(node) && node.name) {
6149
- this.import_value_set.delete(node.name.text);
6127
+ this.disallowed_values.add(node.name.text);
6150
6128
  }
6151
6129
  if (ts.isClassDeclaration(node) && node.name) {
6152
- this.import_value_set.delete(node.name.text);
6130
+ this.disallowed_values.add(node.name.text);
6153
6131
  }
6154
6132
  if (ts.isEnumDeclaration(node)) {
6155
- this.import_value_set.delete(node.name.text);
6156
- }
6157
- if (ts.isTypeAliasDeclaration(node)) {
6158
- this.import_type_set.delete(node.name.text);
6159
- }
6160
- if (ts.isInterfaceDeclaration(node)) {
6161
- this.import_type_set.delete(node.name.text);
6133
+ this.disallowed_values.add(node.name.text);
6162
6134
  }
6163
6135
  }
6164
6136
  analyze$propsRune(node) {
@@ -6196,11 +6168,24 @@ class HoistableInterfaces {
6196
6168
  if (hoistable_interfaces.has(interface_name)) {
6197
6169
  continue;
6198
6170
  }
6199
- const can_hoist = [...deps.type_deps, ...deps.value_deps].every((dep) => {
6200
- return (this.import_type_set.has(dep) ||
6201
- this.import_value_set.has(dep) ||
6202
- hoistable_interfaces.has(dep));
6203
- });
6171
+ let can_hoist = true;
6172
+ for (const dep of deps.type_deps) {
6173
+ if (this.disallowed_types.has(dep)) {
6174
+ this.disallowed_types.add(interface_name);
6175
+ can_hoist = false;
6176
+ break;
6177
+ }
6178
+ if (this.interface_map.has(dep) && !hoistable_interfaces.has(dep)) {
6179
+ can_hoist = false;
6180
+ }
6181
+ }
6182
+ for (const dep of deps.value_deps) {
6183
+ if (this.disallowed_values.has(dep)) {
6184
+ this.disallowed_types.add(interface_name);
6185
+ can_hoist = false;
6186
+ break;
6187
+ }
6188
+ }
6204
6189
  if (can_hoist) {
6205
6190
  hoistable_interfaces.set(interface_name, deps.node);
6206
6191
  progress = true;
@@ -6212,9 +6197,7 @@ class HoistableInterfaces {
6212
6197
  ...this.props_interface.type_deps,
6213
6198
  ...this.props_interface.value_deps
6214
6199
  ].every((dep) => {
6215
- return (this.import_type_set.has(dep) ||
6216
- this.import_value_set.has(dep) ||
6217
- hoistable_interfaces.has(dep));
6200
+ return !this.disallowed_types.has(dep) && !this.disallowed_values.has(dep);
6218
6201
  });
6219
6202
  if (can_hoist) {
6220
6203
  hoistable_interfaces.set(this.props_interface.name, this.props_interface.node);
@@ -6229,7 +6212,7 @@ class HoistableInterfaces {
6229
6212
  if (!this.props_interface.name)
6230
6213
  return;
6231
6214
  for (const generic of generics) {
6232
- this.import_type_set.delete(generic);
6215
+ this.disallowed_types.add(generic);
6233
6216
  }
6234
6217
  const hoistable = this.determineHoistableInterfaces();
6235
6218
  if (hoistable.has(this.props_interface.name)) {
@@ -6257,8 +6240,8 @@ class HoistableInterfaces {
6257
6240
  return hoistable;
6258
6241
  }
6259
6242
  }
6260
- getAllowedValues() {
6261
- return this.import_value_set;
6243
+ isAllowedReference(reference) {
6244
+ return !this.disallowed_values.has(reference);
6262
6245
  }
6263
6246
  /**
6264
6247
  * Collects type and value dependencies from a given TypeNode.
@@ -7727,6 +7710,7 @@ function svelte2tsx(svelte, options = { parse }) {
7727
7710
  const str = new MagicString(svelte);
7728
7711
  const basename = path__default.basename(options.filename || '');
7729
7712
  const svelte5Plus = Number(options.version[0]) > 4;
7713
+ const isTsFile = options === null || options === void 0 ? void 0 : options.isTsFile;
7730
7714
  // process the htmlx as a svelte template
7731
7715
  let { htmlAst, moduleScriptTag, scriptTag, rootSnippets, slots, uses$$props, uses$$slots, uses$$restProps, events, componentDocumentation, resolvedStores, usesAccessors, isRunes } = processSvelteTemplate(str, options.parse || parse, {
7732
7716
  ...options,
@@ -7755,7 +7739,7 @@ function svelte2tsx(svelte, options = { parse }) {
7755
7739
  : instanceScriptTarget;
7756
7740
  const implicitStoreValues = new ImplicitStoreValues(resolvedStores, renderFunctionStart);
7757
7741
  //move the instance script and process the content
7758
- let exportedNames = new ExportedNames(str, 0, basename, options === null || options === void 0 ? void 0 : options.isTsFile, svelte5Plus, isRunes);
7742
+ let exportedNames = new ExportedNames(str, 0, basename, isTsFile, svelte5Plus, isRunes);
7759
7743
  let generics = new Generics(str, 0, { attributes: [] });
7760
7744
  let uses$$SlotsInterface = false;
7761
7745
  if (scriptTag) {
@@ -7763,7 +7747,7 @@ function svelte2tsx(svelte, options = { parse }) {
7763
7747
  if (scriptTag.start != instanceScriptTarget) {
7764
7748
  str.move(scriptTag.start, scriptTag.end, instanceScriptTarget);
7765
7749
  }
7766
- const res = processInstanceScriptContent(str, scriptTag, events, implicitStoreValues, options.mode, moduleAst, options === null || options === void 0 ? void 0 : options.isTsFile, basename, svelte5Plus, isRunes);
7750
+ const res = processInstanceScriptContent(str, scriptTag, events, implicitStoreValues, options.mode, moduleAst, isTsFile, basename, svelte5Plus, isRunes);
7767
7751
  uses$$props = uses$$props || res.uses$$props;
7768
7752
  uses$$restProps = uses$$restProps || res.uses$$restProps;
7769
7753
  uses$$slots = uses$$slots || res.uses$$slots;
@@ -7787,6 +7771,7 @@ function svelte2tsx(svelte, options = { parse }) {
7787
7771
  uses$$SlotsInterface,
7788
7772
  generics,
7789
7773
  svelte5Plus,
7774
+ isTsFile,
7790
7775
  mode: options.mode
7791
7776
  });
7792
7777
  // we need to process the module script after the instance script has moved otherwise we get warnings about moving edited items
@@ -7797,14 +7782,14 @@ function svelte2tsx(svelte, options = { parse }) {
7797
7782
  }
7798
7783
  }
7799
7784
  if (moduleScriptTag || scriptTag) {
7800
- const allowed = exportedNames.hoistableInterfaces.getAllowedValues();
7801
7785
  for (const [start, end, globals] of rootSnippets) {
7802
7786
  const hoist_to_module = moduleScriptTag &&
7803
- (globals.size === 0 || [...globals.keys()].every((id) => allowed.has(id)));
7787
+ (globals.size === 0 ||
7788
+ [...globals.keys()].every((id) => exportedNames.hoistableInterfaces.isAllowedReference(id)));
7804
7789
  if (hoist_to_module) {
7805
7790
  str.move(start, end, scriptTag
7806
7791
  ? scriptTag.start + 1 // +1 because imports are also moved at that position, and we want to move interfaces after imports
7807
- : moduleScriptTag.end);
7792
+ : instanceScriptTarget);
7808
7793
  }
7809
7794
  else if (scriptTag) {
7810
7795
  str.move(start, end, renderFunctionStart);
@@ -7815,7 +7800,7 @@ function svelte2tsx(svelte, options = { parse }) {
7815
7800
  str,
7816
7801
  canHaveAnyProp: !exportedNames.uses$$Props && (uses$$props || uses$$restProps),
7817
7802
  events,
7818
- isTsFile: options === null || options === void 0 ? void 0 : options.isTsFile,
7803
+ isTsFile,
7819
7804
  exportedNames,
7820
7805
  usesAccessors,
7821
7806
  usesSlots: slots.size > 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte2tsx",
3
- "version": "0.7.31",
3
+ "version": "0.7.33",
4
4
  "description": "Convert Svelte components to TSX for type checking",
5
5
  "author": "David Pershouse",
6
6
  "license": "MIT",
@@ -40,7 +40,7 @@
40
40
  "svelte": "~4.2.19",
41
41
  "tiny-glob": "^0.2.6",
42
42
  "tslib": "^2.4.0",
43
- "typescript": "~5.6.3"
43
+ "typescript": "^5.7.2"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "svelte": "^3.55 || ^4.0.0-next.0 || ^4.0 || ^5.0.0-next.0",