svelte2tsx 0.7.51 → 0.7.53

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 +18 -9
  2. package/index.mjs +18 -9
  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,
@@ -5906,7 +5912,7 @@ function upsertKitRouteFile(ts, basename, getSource, surround) {
5906
5912
  addTypeToVariable(exports, surround, insert, isTsFile, 'csr', `boolean`);
5907
5913
  // add types to GET/PUT/POST/PATCH/DELETE/OPTIONS/HEAD if not explicitly typed
5908
5914
  const insertApiMethod = (name) => {
5909
- addTypeToFunction(ts, exports, surround, insert, isTsFile, name, `import('./$types.js').RequestEvent`, `Response | Promise<Response>`);
5915
+ addTypeToFunction(ts, exports, surround, insert, isTsFile, name, `import('./$types.js').RequestEvent`, `Response | Promise<Response>`, `Promise<Response>`);
5910
5916
  };
5911
5917
  insertApiMethod('GET');
5912
5918
  insertApiMethod('PUT');
@@ -6000,7 +6006,8 @@ function addTypeToVariable(exports, surround, insert, isTsFile, name, type) {
6000
6006
  }
6001
6007
  }
6002
6008
  }
6003
- function addTypeToFunction(ts, exports, surround, insert, isTsFile, name, type, returnType) {
6009
+ function addTypeToFunction(ts, exports, surround, insert, isTsFile, name, type, returnType, asyncReturnType) {
6010
+ var _a, _b;
6004
6011
  const fn = exports.get(name);
6005
6012
  if ((fn === null || fn === void 0 ? void 0 : fn.type) === 'function' && fn.node.parameters.length === 1 && !fn.hasTypeDefinition) {
6006
6013
  if (isTsFile) {
@@ -6008,10 +6015,12 @@ function addTypeToFunction(ts, exports, surround, insert, isTsFile, name, type,
6008
6015
  const paramInsertion = surround(!returnType ? `: Parameters<${type}>[0]` : `: ${type}`);
6009
6016
  insert(paramPos, paramInsertion);
6010
6017
  if (!fn.node.type && fn.node.body) {
6018
+ const isAsync = (_b = (_a = fn.node.modifiers) === null || _a === void 0 ? void 0 : _a.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword)) !== null && _b !== void 0 ? _b : false;
6019
+ const effectiveReturnType = isAsync && asyncReturnType ? asyncReturnType : returnType;
6011
6020
  const returnPos = ts.isArrowFunction(fn.node)
6012
6021
  ? fn.node.equalsGreaterThanToken.getStart()
6013
6022
  : fn.node.body.getStart();
6014
- const returnInsertion = surround(!returnType ? `: ReturnType<${type}> ` : `: ${returnType} `);
6023
+ const returnInsertion = surround(!effectiveReturnType ? `: ReturnType<${type}> ` : `: ${effectiveReturnType} `);
6015
6024
  insert(returnPos, returnInsertion);
6016
6025
  }
6017
6026
  }
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,
@@ -5886,7 +5892,7 @@ function upsertKitRouteFile(ts, basename, getSource, surround) {
5886
5892
  addTypeToVariable(exports, surround, insert, isTsFile, 'csr', `boolean`);
5887
5893
  // add types to GET/PUT/POST/PATCH/DELETE/OPTIONS/HEAD if not explicitly typed
5888
5894
  const insertApiMethod = (name) => {
5889
- addTypeToFunction(ts, exports, surround, insert, isTsFile, name, `import('./$types.js').RequestEvent`, `Response | Promise<Response>`);
5895
+ addTypeToFunction(ts, exports, surround, insert, isTsFile, name, `import('./$types.js').RequestEvent`, `Response | Promise<Response>`, `Promise<Response>`);
5890
5896
  };
5891
5897
  insertApiMethod('GET');
5892
5898
  insertApiMethod('PUT');
@@ -5980,7 +5986,8 @@ function addTypeToVariable(exports, surround, insert, isTsFile, name, type) {
5980
5986
  }
5981
5987
  }
5982
5988
  }
5983
- function addTypeToFunction(ts, exports, surround, insert, isTsFile, name, type, returnType) {
5989
+ function addTypeToFunction(ts, exports, surround, insert, isTsFile, name, type, returnType, asyncReturnType) {
5990
+ var _a, _b;
5984
5991
  const fn = exports.get(name);
5985
5992
  if ((fn === null || fn === void 0 ? void 0 : fn.type) === 'function' && fn.node.parameters.length === 1 && !fn.hasTypeDefinition) {
5986
5993
  if (isTsFile) {
@@ -5988,10 +5995,12 @@ function addTypeToFunction(ts, exports, surround, insert, isTsFile, name, type,
5988
5995
  const paramInsertion = surround(!returnType ? `: Parameters<${type}>[0]` : `: ${type}`);
5989
5996
  insert(paramPos, paramInsertion);
5990
5997
  if (!fn.node.type && fn.node.body) {
5998
+ const isAsync = (_b = (_a = fn.node.modifiers) === null || _a === void 0 ? void 0 : _a.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword)) !== null && _b !== void 0 ? _b : false;
5999
+ const effectiveReturnType = isAsync && asyncReturnType ? asyncReturnType : returnType;
5991
6000
  const returnPos = ts.isArrowFunction(fn.node)
5992
6001
  ? fn.node.equalsGreaterThanToken.getStart()
5993
6002
  : fn.node.body.getStart();
5994
- const returnInsertion = surround(!returnType ? `: ReturnType<${type}> ` : `: ${returnType} `);
6003
+ const returnInsertion = surround(!effectiveReturnType ? `: ReturnType<${type}> ` : `: ${effectiveReturnType} `);
5995
6004
  insert(returnPos, returnInsertion);
5996
6005
  }
5997
6006
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte2tsx",
3
- "version": "0.7.51",
3
+ "version": "0.7.53",
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",