svelte2tsx 0.6.19 → 0.6.20

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
@@ -5279,7 +5279,7 @@ function getLineOffsets(text) {
5279
5279
 
5280
5280
  class Generics {
5281
5281
  constructor(str, astOffset, script) {
5282
- var _a, _b;
5282
+ var _a, _b, _c, _d;
5283
5283
  this.str = str;
5284
5284
  this.astOffset = astOffset;
5285
5285
  this.definitions = [];
@@ -5288,13 +5288,26 @@ class Generics {
5288
5288
  this.genericsAttr = script.attributes.find((attr) => attr.name === 'generics');
5289
5289
  const generics = (_b = (_a = this.genericsAttr) === null || _a === void 0 ? void 0 : _a.value[0]) === null || _b === void 0 ? void 0 : _b.raw;
5290
5290
  if (generics) {
5291
- this.definitions = generics.split(',').map((g) => g.trim());
5292
- this.references = this.definitions.map((def) => def.split(/\s/)[0]);
5291
+ const typeParameters = this.getGenericTypeParameters(generics);
5292
+ this.definitions = (_c = typeParameters === null || typeParameters === void 0 ? void 0 : typeParameters.map((param) => param.getText())) !== null && _c !== void 0 ? _c : [];
5293
+ this.references = (_d = typeParameters === null || typeParameters === void 0 ? void 0 : typeParameters.map((param) => param.name.getText())) !== null && _d !== void 0 ? _d : [];
5293
5294
  }
5294
5295
  else {
5295
5296
  this.genericsAttr = undefined;
5296
5297
  }
5297
5298
  }
5299
+ getGenericTypeParameters(rawGenericsAttr) {
5300
+ const sourceFile = ts.createSourceFile('index.ts', `<${rawGenericsAttr}>() => {}`, ts.ScriptTarget.Latest, true);
5301
+ const firstStatement = sourceFile.statements[0];
5302
+ if (!firstStatement || !ts.isExpressionStatement(firstStatement)) {
5303
+ return;
5304
+ }
5305
+ const arrowFunction = firstStatement.expression;
5306
+ if (!ts.isArrowFunction(arrowFunction)) {
5307
+ return;
5308
+ }
5309
+ return arrowFunction.typeParameters;
5310
+ }
5298
5311
  addIfIsGeneric(node) {
5299
5312
  var _a, _b;
5300
5313
  if (ts.isTypeAliasDeclaration(node) && this.is$$GenericType(node.type)) {
package/index.mjs CHANGED
@@ -5259,7 +5259,7 @@ function getLineOffsets(text) {
5259
5259
 
5260
5260
  class Generics {
5261
5261
  constructor(str, astOffset, script) {
5262
- var _a, _b;
5262
+ var _a, _b, _c, _d;
5263
5263
  this.str = str;
5264
5264
  this.astOffset = astOffset;
5265
5265
  this.definitions = [];
@@ -5268,13 +5268,26 @@ class Generics {
5268
5268
  this.genericsAttr = script.attributes.find((attr) => attr.name === 'generics');
5269
5269
  const generics = (_b = (_a = this.genericsAttr) === null || _a === void 0 ? void 0 : _a.value[0]) === null || _b === void 0 ? void 0 : _b.raw;
5270
5270
  if (generics) {
5271
- this.definitions = generics.split(',').map((g) => g.trim());
5272
- this.references = this.definitions.map((def) => def.split(/\s/)[0]);
5271
+ const typeParameters = this.getGenericTypeParameters(generics);
5272
+ this.definitions = (_c = typeParameters === null || typeParameters === void 0 ? void 0 : typeParameters.map((param) => param.getText())) !== null && _c !== void 0 ? _c : [];
5273
+ this.references = (_d = typeParameters === null || typeParameters === void 0 ? void 0 : typeParameters.map((param) => param.name.getText())) !== null && _d !== void 0 ? _d : [];
5273
5274
  }
5274
5275
  else {
5275
5276
  this.genericsAttr = undefined;
5276
5277
  }
5277
5278
  }
5279
+ getGenericTypeParameters(rawGenericsAttr) {
5280
+ const sourceFile = ts.createSourceFile('index.ts', `<${rawGenericsAttr}>() => {}`, ts.ScriptTarget.Latest, true);
5281
+ const firstStatement = sourceFile.statements[0];
5282
+ if (!firstStatement || !ts.isExpressionStatement(firstStatement)) {
5283
+ return;
5284
+ }
5285
+ const arrowFunction = firstStatement.expression;
5286
+ if (!ts.isArrowFunction(arrowFunction)) {
5287
+ return;
5288
+ }
5289
+ return arrowFunction.typeParameters;
5290
+ }
5278
5291
  addIfIsGeneric(node) {
5279
5292
  var _a, _b;
5280
5293
  if (ts.isTypeAliasDeclaration(node) && this.is$$GenericType(node.type)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte2tsx",
3
- "version": "0.6.19",
3
+ "version": "0.6.20",
4
4
  "description": "Convert Svelte components to TSX for type checking",
5
5
  "author": "David Pershouse",
6
6
  "license": "MIT",
@@ -219,6 +219,7 @@ declare namespace svelteHTML {
219
219
  // Svelte specific
220
220
  'svelte:window': HTMLProps<'svelte:window', HTMLAttributes>;
221
221
  'svelte:body': HTMLProps<'svelte:body', HTMLAttributes>;
222
+ 'svelte:document': HTMLProps<'svelte:document', HTMLAttributes>;
222
223
  'svelte:fragment': { slot?: string };
223
224
  'svelte:options': { [name: string]: any };
224
225
  'svelte:head': { [name: string]: any };
package/svelte-jsx.d.ts CHANGED
@@ -238,6 +238,7 @@ declare namespace svelteHTML {
238
238
  // Svelte specific
239
239
  'svelte:window': HTMLProps<'svelte:window', HTMLAttributes>;
240
240
  'svelte:body': HTMLProps<'svelte:body', HTMLAttributes>;
241
+ 'svelte:document': HTMLProps<'svelte:document', HTMLAttributes>;
241
242
  'svelte:fragment': { slot?: string };
242
243
  'svelte:options': { [name: string]: any };
243
244
  'svelte:head': { [name: string]: any };