svelte2tsx 0.6.2 → 0.6.3

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/index.js CHANGED
@@ -2961,7 +2961,9 @@ function isInterfaceOrTypeDeclaration(node) {
2961
2961
  }
2962
2962
  function findExportKeyword(node) {
2963
2963
  var _a;
2964
- return (_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.find((x) => x.kind == ts.SyntaxKind.ExportKeyword);
2964
+ return ts.canHaveModifiers(node)
2965
+ ? (_a = ts.getModifiers(node)) === null || _a === void 0 ? void 0 : _a.find((x) => x.kind == ts.SyntaxKind.ExportKeyword)
2966
+ : undefined;
2965
2967
  }
2966
2968
  /**
2967
2969
  * Node is like `bla = ...` or `{bla} = ...` or `[bla] = ...`
@@ -3570,10 +3572,12 @@ function updatePrepends(str, index, toAppend, removeExisting) {
3570
3572
  function is$$PropsDeclaration(node) {
3571
3573
  return isInterfaceOrTypeDeclaration(node) && node.name.text === '$$Props';
3572
3574
  }
3575
+ const kitPageFiles = new Set(['+page.svelte', '+layout.svelte']);
3573
3576
  class ExportedNames {
3574
- constructor(str, astOffset) {
3577
+ constructor(str, astOffset, basename) {
3575
3578
  this.str = str;
3576
3579
  this.astOffset = astOffset;
3580
+ this.basename = basename;
3577
3581
  /**
3578
3582
  * Uses the $$Props type
3579
3583
  */
@@ -3651,6 +3655,20 @@ class ExportedNames {
3651
3655
  const tsType = declaration.type;
3652
3656
  const jsDocType = ts.getJSDocType(declaration);
3653
3657
  const type = tsType || jsDocType;
3658
+ const name = identifier.getText();
3659
+ const isKitExport = kitPageFiles.has(this.basename) &&
3660
+ (name === 'data' || name === 'form' || name === 'snapshot');
3661
+ // TS types are not allowed in JS files, but TS will still pick it up and the ignore comment will filter out the error
3662
+ const kitType = isKitExport && !type
3663
+ ? `: import('./$types').${name === 'data'
3664
+ ? this.basename.includes('layout')
3665
+ ? 'LayoutData'
3666
+ : 'PageData'
3667
+ : name === 'form'
3668
+ ? 'ActionData'
3669
+ : 'Snapshot'}`
3670
+ : '';
3671
+ const end = declaration.end + this.astOffset;
3654
3672
  if (ts.isIdentifier(identifier) &&
3655
3673
  // Ensure initialization for proper control flow and to avoid "possibly undefined" type errors.
3656
3674
  // Also ensure prop is typed as any with a type annotation in TS strict mode
@@ -3662,8 +3680,10 @@ class ExportedNames {
3662
3680
  (!type &&
3663
3681
  [ts.SyntaxKind.FalseKeyword, ts.SyntaxKind.TrueKeyword].includes(declaration.initializer.kind)))) {
3664
3682
  const name = identifier.getText();
3665
- const end = declaration.end + this.astOffset;
3666
- preprendStr(this.str, end, surroundWithIgnoreComments(`;${name} = __sveltets_2_any(${name});`));
3683
+ preprendStr(this.str, end, surroundWithIgnoreComments(`${kitType};${name} = __sveltets_2_any(${name});`));
3684
+ }
3685
+ else if (kitType) {
3686
+ preprendStr(this.str, end, surroundWithIgnoreComments(`${kitType}`));
3667
3687
  }
3668
3688
  };
3669
3689
  const findComma = (target) => target.getChildren().filter((child) => child.kind === ts.SyntaxKind.CommaToken);
@@ -4986,12 +5006,12 @@ class InterfacesAndTypes {
4986
5006
  }
4987
5007
  }
4988
5008
 
4989
- function processInstanceScriptContent(str, script, events, implicitStoreValues, mode, hasModuleScript) {
5009
+ function processInstanceScriptContent(str, script, events, implicitStoreValues, mode, hasModuleScript, basename) {
4990
5010
  const htmlx = str.original;
4991
5011
  const scriptContent = htmlx.substring(script.content.start, script.content.end);
4992
5012
  const tsAst = ts__namespace.createSourceFile('component.ts.svelte', scriptContent, ts__namespace.ScriptTarget.Latest, true, ts__namespace.ScriptKind.TS);
4993
5013
  const astOffset = script.content.start;
4994
- const exportedNames = new ExportedNames(str, astOffset);
5014
+ const exportedNames = new ExportedNames(str, astOffset, basename);
4995
5015
  const generics = new Generics(str, astOffset);
4996
5016
  const interfacesAndTypes = new InterfacesAndTypes();
4997
5017
  const implicitTopLevelNames = new ImplicitTopLevelNames(str, astOffset);
@@ -5686,6 +5706,7 @@ function processSvelteTemplate(str, options) {
5686
5706
  function svelte2tsx(svelte, options = {}) {
5687
5707
  options.mode = options.mode || 'ts';
5688
5708
  const str = new MagicString(svelte);
5709
+ const basename = path.basename(options.filename || '');
5689
5710
  // process the htmlx as a svelte template
5690
5711
  let { htmlAst, moduleScriptTag, scriptTag, slots, uses$$props, uses$$slots, uses$$restProps, events, componentDocumentation, resolvedStores, usesAccessors } = processSvelteTemplate(str, options);
5691
5712
  /* Rearrange the script tags so that module is first, and instance second followed finally by the template
@@ -5709,7 +5730,7 @@ function svelte2tsx(svelte, options = {}) {
5709
5730
  : instanceScriptTarget;
5710
5731
  const implicitStoreValues = new ImplicitStoreValues(resolvedStores, renderFunctionStart);
5711
5732
  //move the instance script and process the content
5712
- let exportedNames = new ExportedNames(str, 0);
5733
+ let exportedNames = new ExportedNames(str, 0, basename);
5713
5734
  let generics = new Generics(str, 0);
5714
5735
  let uses$$SlotsInterface = false;
5715
5736
  if (scriptTag) {
@@ -5718,7 +5739,7 @@ function svelte2tsx(svelte, options = {}) {
5718
5739
  str.move(scriptTag.start, scriptTag.end, instanceScriptTarget);
5719
5740
  }
5720
5741
  const res = processInstanceScriptContent(str, scriptTag, events, implicitStoreValues, options.mode,
5721
- /**hasModuleScripts */ !!moduleScriptTag);
5742
+ /**hasModuleScripts */ !!moduleScriptTag, basename);
5722
5743
  uses$$props = uses$$props || res.uses$$props;
5723
5744
  uses$$restProps = uses$$restProps || res.uses$$restProps;
5724
5745
  uses$$slots = uses$$slots || res.uses$$slots;
package/index.mjs CHANGED
@@ -2941,7 +2941,9 @@ function isInterfaceOrTypeDeclaration(node) {
2941
2941
  }
2942
2942
  function findExportKeyword(node) {
2943
2943
  var _a;
2944
- return (_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.find((x) => x.kind == ts__default.SyntaxKind.ExportKeyword);
2944
+ return ts__default.canHaveModifiers(node)
2945
+ ? (_a = ts__default.getModifiers(node)) === null || _a === void 0 ? void 0 : _a.find((x) => x.kind == ts__default.SyntaxKind.ExportKeyword)
2946
+ : undefined;
2945
2947
  }
2946
2948
  /**
2947
2949
  * Node is like `bla = ...` or `{bla} = ...` or `[bla] = ...`
@@ -3550,10 +3552,12 @@ function updatePrepends(str, index, toAppend, removeExisting) {
3550
3552
  function is$$PropsDeclaration(node) {
3551
3553
  return isInterfaceOrTypeDeclaration(node) && node.name.text === '$$Props';
3552
3554
  }
3555
+ const kitPageFiles = new Set(['+page.svelte', '+layout.svelte']);
3553
3556
  class ExportedNames {
3554
- constructor(str, astOffset) {
3557
+ constructor(str, astOffset, basename) {
3555
3558
  this.str = str;
3556
3559
  this.astOffset = astOffset;
3560
+ this.basename = basename;
3557
3561
  /**
3558
3562
  * Uses the $$Props type
3559
3563
  */
@@ -3631,6 +3635,20 @@ class ExportedNames {
3631
3635
  const tsType = declaration.type;
3632
3636
  const jsDocType = ts__default.getJSDocType(declaration);
3633
3637
  const type = tsType || jsDocType;
3638
+ const name = identifier.getText();
3639
+ const isKitExport = kitPageFiles.has(this.basename) &&
3640
+ (name === 'data' || name === 'form' || name === 'snapshot');
3641
+ // TS types are not allowed in JS files, but TS will still pick it up and the ignore comment will filter out the error
3642
+ const kitType = isKitExport && !type
3643
+ ? `: import('./$types').${name === 'data'
3644
+ ? this.basename.includes('layout')
3645
+ ? 'LayoutData'
3646
+ : 'PageData'
3647
+ : name === 'form'
3648
+ ? 'ActionData'
3649
+ : 'Snapshot'}`
3650
+ : '';
3651
+ const end = declaration.end + this.astOffset;
3634
3652
  if (ts__default.isIdentifier(identifier) &&
3635
3653
  // Ensure initialization for proper control flow and to avoid "possibly undefined" type errors.
3636
3654
  // Also ensure prop is typed as any with a type annotation in TS strict mode
@@ -3642,8 +3660,10 @@ class ExportedNames {
3642
3660
  (!type &&
3643
3661
  [ts__default.SyntaxKind.FalseKeyword, ts__default.SyntaxKind.TrueKeyword].includes(declaration.initializer.kind)))) {
3644
3662
  const name = identifier.getText();
3645
- const end = declaration.end + this.astOffset;
3646
- preprendStr(this.str, end, surroundWithIgnoreComments(`;${name} = __sveltets_2_any(${name});`));
3663
+ preprendStr(this.str, end, surroundWithIgnoreComments(`${kitType};${name} = __sveltets_2_any(${name});`));
3664
+ }
3665
+ else if (kitType) {
3666
+ preprendStr(this.str, end, surroundWithIgnoreComments(`${kitType}`));
3647
3667
  }
3648
3668
  };
3649
3669
  const findComma = (target) => target.getChildren().filter((child) => child.kind === ts__default.SyntaxKind.CommaToken);
@@ -4966,12 +4986,12 @@ class InterfacesAndTypes {
4966
4986
  }
4967
4987
  }
4968
4988
 
4969
- function processInstanceScriptContent(str, script, events, implicitStoreValues, mode, hasModuleScript) {
4989
+ function processInstanceScriptContent(str, script, events, implicitStoreValues, mode, hasModuleScript, basename) {
4970
4990
  const htmlx = str.original;
4971
4991
  const scriptContent = htmlx.substring(script.content.start, script.content.end);
4972
4992
  const tsAst = ts.createSourceFile('component.ts.svelte', scriptContent, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
4973
4993
  const astOffset = script.content.start;
4974
- const exportedNames = new ExportedNames(str, astOffset);
4994
+ const exportedNames = new ExportedNames(str, astOffset, basename);
4975
4995
  const generics = new Generics(str, astOffset);
4976
4996
  const interfacesAndTypes = new InterfacesAndTypes();
4977
4997
  const implicitTopLevelNames = new ImplicitTopLevelNames(str, astOffset);
@@ -5666,6 +5686,7 @@ function processSvelteTemplate(str, options) {
5666
5686
  function svelte2tsx(svelte, options = {}) {
5667
5687
  options.mode = options.mode || 'ts';
5668
5688
  const str = new MagicString(svelte);
5689
+ const basename = path__default.basename(options.filename || '');
5669
5690
  // process the htmlx as a svelte template
5670
5691
  let { htmlAst, moduleScriptTag, scriptTag, slots, uses$$props, uses$$slots, uses$$restProps, events, componentDocumentation, resolvedStores, usesAccessors } = processSvelteTemplate(str, options);
5671
5692
  /* Rearrange the script tags so that module is first, and instance second followed finally by the template
@@ -5689,7 +5710,7 @@ function svelte2tsx(svelte, options = {}) {
5689
5710
  : instanceScriptTarget;
5690
5711
  const implicitStoreValues = new ImplicitStoreValues(resolvedStores, renderFunctionStart);
5691
5712
  //move the instance script and process the content
5692
- let exportedNames = new ExportedNames(str, 0);
5713
+ let exportedNames = new ExportedNames(str, 0, basename);
5693
5714
  let generics = new Generics(str, 0);
5694
5715
  let uses$$SlotsInterface = false;
5695
5716
  if (scriptTag) {
@@ -5698,7 +5719,7 @@ function svelte2tsx(svelte, options = {}) {
5698
5719
  str.move(scriptTag.start, scriptTag.end, instanceScriptTarget);
5699
5720
  }
5700
5721
  const res = processInstanceScriptContent(str, scriptTag, events, implicitStoreValues, options.mode,
5701
- /**hasModuleScripts */ !!moduleScriptTag);
5722
+ /**hasModuleScripts */ !!moduleScriptTag, basename);
5702
5723
  uses$$props = uses$$props || res.uses$$props;
5703
5724
  uses$$restProps = uses$$restProps || res.uses$$restProps;
5704
5725
  uses$$slots = uses$$slots || res.uses$$slots;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte2tsx",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
4
4
  "description": "Convert Svelte components to TSX for type checking",
5
5
  "author": "David Pershouse",
6
6
  "license": "MIT",
package/svelte-jsx.d.ts CHANGED
@@ -562,7 +562,7 @@ declare namespace svelte.JSX {
562
562
  |'unselectable';
563
563
 
564
564
  /**
565
- * @deprecated use the types from `svelte/elements` instead, or the .
565
+ * @deprecated use the types from `svelte/elements` instead
566
566
  * For more info see https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-getting-deprecation-warnings-for-sveltejsx--i-want-to-migrate-to-the-new-typings
567
567
  */
568
568
  interface DOMAttributes<T extends EventTarget> {
@@ -670,7 +670,7 @@ declare namespace svelte.JSX {
670
670
  }
671
671
 
672
672
  /**
673
- * @deprecated use the types from `svelte/elements` instead, or the .
673
+ * @deprecated use the types from `svelte/elements` instead
674
674
  * For more info see https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-getting-deprecation-warnings-for-sveltejsx--i-want-to-migrate-to-the-new-typings
675
675
  */
676
676
  interface AriaAttributes {
@@ -725,7 +725,7 @@ declare namespace svelte.JSX {
725
725
  }
726
726
 
727
727
  /**
728
- * @deprecated use the types from `svelte/elements` instead, or the .
728
+ * @deprecated use the types from `svelte/elements` instead
729
729
  * For more info see https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-getting-deprecation-warnings-for-sveltejsx--i-want-to-migrate-to-the-new-typings
730
730
  */
731
731
  interface HTMLAttributes<T extends EventTarget> extends AriaAttributes, DOMAttributes<T> {
@@ -904,7 +904,7 @@ declare namespace svelte.JSX {
904
904
  }
905
905
 
906
906
  /**
907
- * @deprecated use the types from `svelte/elements` instead, or the .
907
+ * @deprecated use the types from `svelte/elements` instead
908
908
  * For more info see https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-getting-deprecation-warnings-for-sveltejsx--i-want-to-migrate-to-the-new-typings
909
909
  */
910
910
  interface SVGAttributes<T extends EventTarget> extends AriaAttributes, DOMAttributes<T> {
@@ -1172,18 +1172,18 @@ declare namespace svelte.JSX {
1172
1172
  }
1173
1173
 
1174
1174
  /**
1175
- * @deprecated use the types from `svelte/elements` instead, or the .
1175
+ * @deprecated use the types from `svelte/elements` instead
1176
1176
  * For more info see https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-getting-deprecation-warnings-for-sveltejsx--i-want-to-migrate-to-the-new-typings
1177
1177
  */
1178
1178
  interface HTMLProps<T extends EventTarget> extends HTMLAttributes<T> {}
1179
1179
  /**
1180
- * @deprecated use the types from `svelte/elements` instead, or the .
1180
+ * @deprecated use the types from `svelte/elements` instead
1181
1181
  * For more info see https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-getting-deprecation-warnings-for-sveltejsx--i-want-to-migrate-to-the-new-typings
1182
1182
  */
1183
1183
  interface SVGProps<T extends EventTarget> extends SVGAttributes<T> {}
1184
1184
 
1185
1185
  /**
1186
- * @deprecated use the types from `svelte/elements` instead, or the .
1186
+ * @deprecated use the types from `svelte/elements` instead
1187
1187
  * For more info see https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-getting-deprecation-warnings-for-sveltejsx--i-want-to-migrate-to-the-new-typings
1188
1188
  */
1189
1189
  interface SvelteInputProps extends HTMLProps<HTMLInputElement> {
@@ -1193,7 +1193,7 @@ declare namespace svelte.JSX {
1193
1193
  }
1194
1194
 
1195
1195
  /**
1196
- * @deprecated use the types from `svelte/elements` instead, or the .
1196
+ * @deprecated use the types from `svelte/elements` instead
1197
1197
  * For more info see https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-getting-deprecation-warnings-for-sveltejsx--i-want-to-migrate-to-the-new-typings
1198
1198
  */
1199
1199
  interface SvelteWindowProps {
@@ -1238,7 +1238,7 @@ declare namespace svelte.JSX {
1238
1238
  }
1239
1239
 
1240
1240
  /**
1241
- * @deprecated use the types from `svelte/elements` instead, or the .
1241
+ * @deprecated use the types from `svelte/elements` instead
1242
1242
  * For more info see https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-getting-deprecation-warnings-for-sveltejsx--i-want-to-migrate-to-the-new-typings
1243
1243
  */
1244
1244
  interface SapperAnchorProps {
@@ -1248,7 +1248,7 @@ declare namespace svelte.JSX {
1248
1248
  }
1249
1249
 
1250
1250
  /**
1251
- * @deprecated use the types from `svelte/elements` instead, or the .
1251
+ * @deprecated use the types from `svelte/elements` instead
1252
1252
  * For more info see https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-getting-deprecation-warnings-for-sveltejsx--i-want-to-migrate-to-the-new-typings
1253
1253
  */
1254
1254
  interface SvelteMediaTimeRange {
@@ -1257,7 +1257,7 @@ declare namespace svelte.JSX {
1257
1257
  }
1258
1258
 
1259
1259
  /**
1260
- * @deprecated use the types from `svelte/elements` instead, or the .
1260
+ * @deprecated use the types from `svelte/elements` instead
1261
1261
  * For more info see https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-getting-deprecation-warnings-for-sveltejsx--i-want-to-migrate-to-the-new-typings
1262
1262
  */
1263
1263
  interface SvelteMediaProps {
@@ -1286,7 +1286,7 @@ declare namespace svelte.JSX {
1286
1286
  }
1287
1287
 
1288
1288
  /**
1289
- * @deprecated use the types from `svelte/elements` instead, or the .
1289
+ * @deprecated use the types from `svelte/elements` instead
1290
1290
  * For more info see https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-getting-deprecation-warnings-for-sveltejsx--i-want-to-migrate-to-the-new-typings
1291
1291
  */
1292
1292
  interface SvelteVideoProps extends SvelteMediaProps {
@@ -1296,7 +1296,7 @@ declare namespace svelte.JSX {
1296
1296
  }
1297
1297
 
1298
1298
  /**
1299
- * @deprecated use the types from `svelte/elements` instead, or the .
1299
+ * @deprecated use the types from `svelte/elements` instead
1300
1300
  * For more info see https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-getting-deprecation-warnings-for-sveltejsx--i-want-to-migrate-to-the-new-typings
1301
1301
  */
1302
1302
  interface IntrinsicElements {