svelte2tsx 0.6.16 → 0.6.18

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 (4) hide show
  1. package/index.d.ts +4 -0
  2. package/index.js +55 -21
  3. package/index.mjs +55 -21
  4. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -86,6 +86,10 @@ export interface EmitDtsConfig {
86
86
  /**
87
87
  * Path to `svelte-shims.d.ts` of `svelte2tsx`.
88
88
  * Example: `require.resolve('svelte2tsx/svelte-shims.d.ts')`
89
+ *
90
+ * If a path is given that points to `svelte-shims-v4.d.ts`,
91
+ * the `SvelteComponent` import is used instead of
92
+ * `SvelteComponentTyped` which is deprecated in Svelte v4.
89
93
  */
90
94
  svelteShimsPath: string;
91
95
  /**
package/index.js CHANGED
@@ -2342,11 +2342,13 @@ const oneWayBindingAttributes = new Set([
2342
2342
  'ended',
2343
2343
  'readyState',
2344
2344
  'naturalWidth',
2345
- 'naturalHeight',
2346
- 'contentRect',
2347
- 'contentBoxSize',
2348
- 'borderBoxSize',
2349
- 'devicePixelContentBoxSize'
2345
+ 'naturalHeight'
2346
+ ]);
2347
+ const oneWayBindingAttributesNotOnElement = new Map([
2348
+ ['contentRect', 'DOMRectReadOnly'],
2349
+ ['contentBoxSize', 'ResizeObserverSize[]'],
2350
+ ['borderBoxSize', 'ResizeObserverSize[]'],
2351
+ ['devicePixelContentBoxSize', 'ResizeObserverSize[]']
2350
2352
  ]);
2351
2353
  /**
2352
2354
  * List of all binding names that are transformed to sth like `binding = variable`.
@@ -2392,6 +2394,14 @@ function handleBinding(str, attr, parent, element, preserveBind) {
2392
2394
  ]);
2393
2395
  return;
2394
2396
  }
2397
+ // one way binding whose property is not on the element
2398
+ if (oneWayBindingAttributesNotOnElement.has(attr.name) && element instanceof Element) {
2399
+ element.appendToStartEnd([
2400
+ [attr.expression.start, attr.expression.end],
2401
+ `= ${surroundWithIgnoreComments(`null as ${oneWayBindingAttributesNotOnElement.get(attr.name)}`)};`
2402
+ ]);
2403
+ return;
2404
+ }
2395
2405
  // other bindings which are transformed to normal attributes/props
2396
2406
  const isShorthand = attr.expression.start === attr.start + 'bind:'.length;
2397
2407
  const name = preserveBind && element instanceof Element
@@ -3731,7 +3741,7 @@ function upserKitRouteFile(ts, basename, getSource, surround) {
3731
3741
  addTypeToVariable(exports, surround, insert, 'trailingSlash', `'never' | 'always' | 'ignore'`);
3732
3742
  addTypeToVariable(exports, surround, insert, 'ssr', `boolean`);
3733
3743
  addTypeToVariable(exports, surround, insert, 'csr', `boolean`);
3734
- // add types to GET/PUT/POST/PATCH/DELETE/OPTIONS if not explicitly typed
3744
+ // add types to GET/PUT/POST/PATCH/DELETE/OPTIONS/HEAD if not explicitly typed
3735
3745
  const insertApiMethod = (name) => {
3736
3746
  addTypeToFunction(ts, exports, surround, insert, name, `import('./$types.js').RequestEvent`, `Response | Promise<Response>`);
3737
3747
  };
@@ -3741,6 +3751,7 @@ function upserKitRouteFile(ts, basename, getSource, surround) {
3741
3751
  insertApiMethod('PATCH');
3742
3752
  insertApiMethod('DELETE');
3743
3753
  insertApiMethod('OPTIONS');
3754
+ insertApiMethod('HEAD');
3744
3755
  return { addedCode, originalText: source.getFullText() };
3745
3756
  }
3746
3757
  function upserKitParamsFile(ts, fileName, basename, paramsPath, getSource, surround) {
@@ -5711,7 +5722,7 @@ function addComponentExport(params) {
5711
5722
  addSimpleComponentExport(params);
5712
5723
  }
5713
5724
  }
5714
- function addGenericsComponentExport({ strictEvents, canHaveAnyProp, exportedNames, componentDocumentation, fileName, mode, usesAccessors, str, generics }) {
5725
+ function addGenericsComponentExport({ strictEvents, canHaveAnyProp, exportedNames, componentDocumentation, fileName, mode, usesAccessors, str, generics, noSvelteComponentTyped }) {
5715
5726
  const genericsDef = generics.toDefinitionString();
5716
5727
  const genericsRef = generics.toReferencesString();
5717
5728
  const doc = componentDocumentation.getFormatted();
@@ -5732,19 +5743,22 @@ class __sveltets_Render${genericsDef} {
5732
5743
  }
5733
5744
  }
5734
5745
  `;
5746
+ const svelteComponentClass = noSvelteComponentTyped
5747
+ ? 'SvelteComponent'
5748
+ : 'SvelteComponentTyped';
5735
5749
  if (mode === 'dts') {
5736
5750
  statement +=
5737
5751
  `export type ${className}Props${genericsDef} = ${returnType('props')};\n` +
5738
5752
  `export type ${className}Events${genericsDef} = ${returnType('events')};\n` +
5739
5753
  `export type ${className}Slots${genericsDef} = ${returnType('slots')};\n` +
5740
- `\n${doc}export default class${className ? ` ${className}` : ''}${genericsDef} extends SvelteComponentTyped<${className}Props${genericsRef}, ${className}Events${genericsRef}, ${className}Slots${genericsRef}> {` +
5754
+ `\n${doc}export default class${className ? ` ${className}` : ''}${genericsDef} extends ${svelteComponentClass}<${className}Props${genericsRef}, ${className}Events${genericsRef}, ${className}Slots${genericsRef}> {` +
5741
5755
  exportedNames.createClassGetters() +
5742
5756
  (usesAccessors ? exportedNames.createClassAccessors() : '') +
5743
5757
  '\n}';
5744
5758
  }
5745
5759
  else {
5746
5760
  statement +=
5747
- '\n\nimport { SvelteComponentTyped as __SvelteComponentTyped__ } from "svelte" \n' +
5761
+ `\n\nimport { ${svelteComponentClass} as __SvelteComponentTyped__ } from "svelte" \n` +
5748
5762
  `${doc}export default class${className ? ` ${className}` : ''}${genericsDef} extends __SvelteComponentTyped__<${returnType('props')}, ${returnType('events')}, ${returnType('slots')}> {` +
5749
5763
  exportedNames.createClassGetters() +
5750
5764
  (usesAccessors ? exportedNames.createClassAccessors() : '') +
@@ -5752,18 +5766,32 @@ class __sveltets_Render${genericsDef} {
5752
5766
  }
5753
5767
  str.append(statement);
5754
5768
  }
5755
- function addSimpleComponentExport({ strictEvents, isTsFile, canHaveAnyProp, exportedNames, componentDocumentation, fileName, mode, usesAccessors, str }) {
5769
+ function addSimpleComponentExport({ strictEvents, isTsFile, canHaveAnyProp, exportedNames, componentDocumentation, fileName, mode, usesAccessors, str, noSvelteComponentTyped }) {
5756
5770
  const propDef = props(isTsFile, canHaveAnyProp, exportedNames, events(strictEvents, 'render()'));
5757
5771
  const doc = componentDocumentation.getFormatted();
5758
5772
  const className = fileName && classNameFromFilename(fileName, mode !== 'dts');
5759
5773
  let statement;
5760
5774
  if (mode === 'dts' && isTsFile) {
5775
+ const addTypeExport = (type) => {
5776
+ const exportName = className + type;
5777
+ if (!str.original.includes(exportName)) {
5778
+ return `export type ${exportName} = typeof __propDef.${type.toLowerCase()};\n`;
5779
+ }
5780
+ let replacement = exportName + '_';
5781
+ while (str.original.includes(replacement)) {
5782
+ replacement += '_';
5783
+ }
5784
+ return `type ${replacement} = typeof __propDef.${type.toLowerCase()};\nexport { ${replacement} as ${exportName} };\n`;
5785
+ };
5786
+ const svelteComponentClass = noSvelteComponentTyped
5787
+ ? 'SvelteComponent'
5788
+ : 'SvelteComponentTyped';
5761
5789
  statement =
5762
5790
  `\nconst __propDef = ${propDef};\n` +
5763
- `export type ${className}Props = typeof __propDef.props;\n` +
5764
- `export type ${className}Events = typeof __propDef.events;\n` +
5765
- `export type ${className}Slots = typeof __propDef.slots;\n` +
5766
- `\n${doc}export default class${className ? ` ${className}` : ''} extends SvelteComponentTyped<${className}Props, ${className}Events, ${className}Slots> {` +
5791
+ addTypeExport('Props') +
5792
+ addTypeExport('Events') +
5793
+ addTypeExport('Slots') +
5794
+ `\n${doc}export default class${className ? ` ${className}` : ''} extends ${svelteComponentClass}<${className}Props, ${className}Events, ${className}Slots> {` +
5767
5795
  exportedNames.createClassGetters() +
5768
5796
  (usesAccessors ? exportedNames.createClassAccessors() : '') +
5769
5797
  '\n}';
@@ -6194,14 +6222,20 @@ function svelte2tsx(svelte, options = {}) {
6194
6222
  fileName: options === null || options === void 0 ? void 0 : options.filename,
6195
6223
  componentDocumentation,
6196
6224
  mode: options.mode,
6197
- generics
6225
+ generics,
6226
+ noSvelteComponentTyped: options.noSvelteComponentTyped
6198
6227
  });
6199
6228
  if (options.mode === 'dts') {
6200
6229
  // Prepend the import which is used for TS files
6201
6230
  // The other shims need to be provided by the user ambient-style,
6202
6231
  // for example through filenames.push(require.resolve('svelte2tsx/svelte-shims.d.ts'))
6203
6232
  // TODO replace with SvelteComponent for Svelte 5, keep old for backwards compatibility with Svelte 3
6204
- str.prepend('import { SvelteComponentTyped } from "svelte"\n' + '\n');
6233
+ if (options.noSvelteComponentTyped) {
6234
+ str.prepend('import { SvelteComponent } from "svelte"\n' + '\n');
6235
+ }
6236
+ else {
6237
+ str.prepend('import { SvelteComponentTyped } from "svelte"\n' + '\n');
6238
+ }
6205
6239
  let code = str.toString();
6206
6240
  // Remove all tsx occurences and the template part from the output
6207
6241
  code = code
@@ -6394,15 +6428,15 @@ async function createTsCompilerHost(options, svelteMap) {
6394
6428
  async function createSvelteMap(config) {
6395
6429
  const svelteFiles = new Map();
6396
6430
  function add(path) {
6397
- var _a, _b;
6398
6431
  const code = ts.sys.readFile(path, 'utf-8');
6399
- const isTsFile = // svelte-preprocess allows default languages
6400
- ['ts', 'typescript'].includes((_b = (_a = config.preprocess) === null || _a === void 0 ? void 0 : _a.defaultLanguages) === null || _b === void 0 ? void 0 : _b.script) ||
6401
- /<script\s+[^>]*?lang=('|")(ts|typescript)('|")/.test(code);
6432
+ const isTsFile = /<script\s+[^>]*?lang=('|")(ts|typescript)('|")/.test(code);
6402
6433
  const transformed = svelte2tsx(code, {
6403
6434
  filename: path,
6404
6435
  isTsFile,
6405
- mode: 'dts'
6436
+ mode: 'dts',
6437
+ noSvelteComponentTyped: config.svelteShimsPath
6438
+ .replace(/\\/g, '/')
6439
+ .endsWith('svelte2tsx/svelte-shims-v4.d.ts')
6406
6440
  }).code;
6407
6441
  svelteFiles.set(path, transformed);
6408
6442
  return isTsFile;
package/index.mjs CHANGED
@@ -2322,11 +2322,13 @@ const oneWayBindingAttributes = new Set([
2322
2322
  'ended',
2323
2323
  'readyState',
2324
2324
  'naturalWidth',
2325
- 'naturalHeight',
2326
- 'contentRect',
2327
- 'contentBoxSize',
2328
- 'borderBoxSize',
2329
- 'devicePixelContentBoxSize'
2325
+ 'naturalHeight'
2326
+ ]);
2327
+ const oneWayBindingAttributesNotOnElement = new Map([
2328
+ ['contentRect', 'DOMRectReadOnly'],
2329
+ ['contentBoxSize', 'ResizeObserverSize[]'],
2330
+ ['borderBoxSize', 'ResizeObserverSize[]'],
2331
+ ['devicePixelContentBoxSize', 'ResizeObserverSize[]']
2330
2332
  ]);
2331
2333
  /**
2332
2334
  * List of all binding names that are transformed to sth like `binding = variable`.
@@ -2372,6 +2374,14 @@ function handleBinding(str, attr, parent, element, preserveBind) {
2372
2374
  ]);
2373
2375
  return;
2374
2376
  }
2377
+ // one way binding whose property is not on the element
2378
+ if (oneWayBindingAttributesNotOnElement.has(attr.name) && element instanceof Element) {
2379
+ element.appendToStartEnd([
2380
+ [attr.expression.start, attr.expression.end],
2381
+ `= ${surroundWithIgnoreComments(`null as ${oneWayBindingAttributesNotOnElement.get(attr.name)}`)};`
2382
+ ]);
2383
+ return;
2384
+ }
2375
2385
  // other bindings which are transformed to normal attributes/props
2376
2386
  const isShorthand = attr.expression.start === attr.start + 'bind:'.length;
2377
2387
  const name = preserveBind && element instanceof Element
@@ -3711,7 +3721,7 @@ function upserKitRouteFile(ts, basename, getSource, surround) {
3711
3721
  addTypeToVariable(exports, surround, insert, 'trailingSlash', `'never' | 'always' | 'ignore'`);
3712
3722
  addTypeToVariable(exports, surround, insert, 'ssr', `boolean`);
3713
3723
  addTypeToVariable(exports, surround, insert, 'csr', `boolean`);
3714
- // add types to GET/PUT/POST/PATCH/DELETE/OPTIONS if not explicitly typed
3724
+ // add types to GET/PUT/POST/PATCH/DELETE/OPTIONS/HEAD if not explicitly typed
3715
3725
  const insertApiMethod = (name) => {
3716
3726
  addTypeToFunction(ts, exports, surround, insert, name, `import('./$types.js').RequestEvent`, `Response | Promise<Response>`);
3717
3727
  };
@@ -3721,6 +3731,7 @@ function upserKitRouteFile(ts, basename, getSource, surround) {
3721
3731
  insertApiMethod('PATCH');
3722
3732
  insertApiMethod('DELETE');
3723
3733
  insertApiMethod('OPTIONS');
3734
+ insertApiMethod('HEAD');
3724
3735
  return { addedCode, originalText: source.getFullText() };
3725
3736
  }
3726
3737
  function upserKitParamsFile(ts, fileName, basename, paramsPath, getSource, surround) {
@@ -5691,7 +5702,7 @@ function addComponentExport(params) {
5691
5702
  addSimpleComponentExport(params);
5692
5703
  }
5693
5704
  }
5694
- function addGenericsComponentExport({ strictEvents, canHaveAnyProp, exportedNames, componentDocumentation, fileName, mode, usesAccessors, str, generics }) {
5705
+ function addGenericsComponentExport({ strictEvents, canHaveAnyProp, exportedNames, componentDocumentation, fileName, mode, usesAccessors, str, generics, noSvelteComponentTyped }) {
5695
5706
  const genericsDef = generics.toDefinitionString();
5696
5707
  const genericsRef = generics.toReferencesString();
5697
5708
  const doc = componentDocumentation.getFormatted();
@@ -5712,19 +5723,22 @@ class __sveltets_Render${genericsDef} {
5712
5723
  }
5713
5724
  }
5714
5725
  `;
5726
+ const svelteComponentClass = noSvelteComponentTyped
5727
+ ? 'SvelteComponent'
5728
+ : 'SvelteComponentTyped';
5715
5729
  if (mode === 'dts') {
5716
5730
  statement +=
5717
5731
  `export type ${className}Props${genericsDef} = ${returnType('props')};\n` +
5718
5732
  `export type ${className}Events${genericsDef} = ${returnType('events')};\n` +
5719
5733
  `export type ${className}Slots${genericsDef} = ${returnType('slots')};\n` +
5720
- `\n${doc}export default class${className ? ` ${className}` : ''}${genericsDef} extends SvelteComponentTyped<${className}Props${genericsRef}, ${className}Events${genericsRef}, ${className}Slots${genericsRef}> {` +
5734
+ `\n${doc}export default class${className ? ` ${className}` : ''}${genericsDef} extends ${svelteComponentClass}<${className}Props${genericsRef}, ${className}Events${genericsRef}, ${className}Slots${genericsRef}> {` +
5721
5735
  exportedNames.createClassGetters() +
5722
5736
  (usesAccessors ? exportedNames.createClassAccessors() : '') +
5723
5737
  '\n}';
5724
5738
  }
5725
5739
  else {
5726
5740
  statement +=
5727
- '\n\nimport { SvelteComponentTyped as __SvelteComponentTyped__ } from "svelte" \n' +
5741
+ `\n\nimport { ${svelteComponentClass} as __SvelteComponentTyped__ } from "svelte" \n` +
5728
5742
  `${doc}export default class${className ? ` ${className}` : ''}${genericsDef} extends __SvelteComponentTyped__<${returnType('props')}, ${returnType('events')}, ${returnType('slots')}> {` +
5729
5743
  exportedNames.createClassGetters() +
5730
5744
  (usesAccessors ? exportedNames.createClassAccessors() : '') +
@@ -5732,18 +5746,32 @@ class __sveltets_Render${genericsDef} {
5732
5746
  }
5733
5747
  str.append(statement);
5734
5748
  }
5735
- function addSimpleComponentExport({ strictEvents, isTsFile, canHaveAnyProp, exportedNames, componentDocumentation, fileName, mode, usesAccessors, str }) {
5749
+ function addSimpleComponentExport({ strictEvents, isTsFile, canHaveAnyProp, exportedNames, componentDocumentation, fileName, mode, usesAccessors, str, noSvelteComponentTyped }) {
5736
5750
  const propDef = props(isTsFile, canHaveAnyProp, exportedNames, events(strictEvents, 'render()'));
5737
5751
  const doc = componentDocumentation.getFormatted();
5738
5752
  const className = fileName && classNameFromFilename(fileName, mode !== 'dts');
5739
5753
  let statement;
5740
5754
  if (mode === 'dts' && isTsFile) {
5755
+ const addTypeExport = (type) => {
5756
+ const exportName = className + type;
5757
+ if (!str.original.includes(exportName)) {
5758
+ return `export type ${exportName} = typeof __propDef.${type.toLowerCase()};\n`;
5759
+ }
5760
+ let replacement = exportName + '_';
5761
+ while (str.original.includes(replacement)) {
5762
+ replacement += '_';
5763
+ }
5764
+ return `type ${replacement} = typeof __propDef.${type.toLowerCase()};\nexport { ${replacement} as ${exportName} };\n`;
5765
+ };
5766
+ const svelteComponentClass = noSvelteComponentTyped
5767
+ ? 'SvelteComponent'
5768
+ : 'SvelteComponentTyped';
5741
5769
  statement =
5742
5770
  `\nconst __propDef = ${propDef};\n` +
5743
- `export type ${className}Props = typeof __propDef.props;\n` +
5744
- `export type ${className}Events = typeof __propDef.events;\n` +
5745
- `export type ${className}Slots = typeof __propDef.slots;\n` +
5746
- `\n${doc}export default class${className ? ` ${className}` : ''} extends SvelteComponentTyped<${className}Props, ${className}Events, ${className}Slots> {` +
5771
+ addTypeExport('Props') +
5772
+ addTypeExport('Events') +
5773
+ addTypeExport('Slots') +
5774
+ `\n${doc}export default class${className ? ` ${className}` : ''} extends ${svelteComponentClass}<${className}Props, ${className}Events, ${className}Slots> {` +
5747
5775
  exportedNames.createClassGetters() +
5748
5776
  (usesAccessors ? exportedNames.createClassAccessors() : '') +
5749
5777
  '\n}';
@@ -6174,14 +6202,20 @@ function svelte2tsx(svelte, options = {}) {
6174
6202
  fileName: options === null || options === void 0 ? void 0 : options.filename,
6175
6203
  componentDocumentation,
6176
6204
  mode: options.mode,
6177
- generics
6205
+ generics,
6206
+ noSvelteComponentTyped: options.noSvelteComponentTyped
6178
6207
  });
6179
6208
  if (options.mode === 'dts') {
6180
6209
  // Prepend the import which is used for TS files
6181
6210
  // The other shims need to be provided by the user ambient-style,
6182
6211
  // for example through filenames.push(require.resolve('svelte2tsx/svelte-shims.d.ts'))
6183
6212
  // TODO replace with SvelteComponent for Svelte 5, keep old for backwards compatibility with Svelte 3
6184
- str.prepend('import { SvelteComponentTyped } from "svelte"\n' + '\n');
6213
+ if (options.noSvelteComponentTyped) {
6214
+ str.prepend('import { SvelteComponent } from "svelte"\n' + '\n');
6215
+ }
6216
+ else {
6217
+ str.prepend('import { SvelteComponentTyped } from "svelte"\n' + '\n');
6218
+ }
6185
6219
  let code = str.toString();
6186
6220
  // Remove all tsx occurences and the template part from the output
6187
6221
  code = code
@@ -6374,15 +6408,15 @@ async function createTsCompilerHost(options, svelteMap) {
6374
6408
  async function createSvelteMap(config) {
6375
6409
  const svelteFiles = new Map();
6376
6410
  function add(path) {
6377
- var _a, _b;
6378
6411
  const code = ts.sys.readFile(path, 'utf-8');
6379
- const isTsFile = // svelte-preprocess allows default languages
6380
- ['ts', 'typescript'].includes((_b = (_a = config.preprocess) === null || _a === void 0 ? void 0 : _a.defaultLanguages) === null || _b === void 0 ? void 0 : _b.script) ||
6381
- /<script\s+[^>]*?lang=('|")(ts|typescript)('|")/.test(code);
6412
+ const isTsFile = /<script\s+[^>]*?lang=('|")(ts|typescript)('|")/.test(code);
6382
6413
  const transformed = svelte2tsx(code, {
6383
6414
  filename: path,
6384
6415
  isTsFile,
6385
- mode: 'dts'
6416
+ mode: 'dts',
6417
+ noSvelteComponentTyped: config.svelteShimsPath
6418
+ .replace(/\\/g, '/')
6419
+ .endsWith('svelte2tsx/svelte-shims-v4.d.ts')
6386
6420
  }).code;
6387
6421
  svelteFiles.set(path, transformed);
6388
6422
  return isTsFile;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte2tsx",
3
- "version": "0.6.16",
3
+ "version": "0.6.18",
4
4
  "description": "Convert Svelte components to TSX for type checking",
5
5
  "author": "David Pershouse",
6
6
  "license": "MIT",