svelte2tsx 0.7.10 → 0.7.12

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 +33 -19
  2. package/index.mjs +33 -19
  3. package/package.json +2 -2
package/index.js CHANGED
@@ -3092,22 +3092,21 @@ function handleTransitionDirective(str, attr, element) {
3092
3092
  * ```
3093
3093
  */
3094
3094
  function handleSnippet(str, snippetBlock, component) {
3095
- var _a, _b, _c, _d, _e, _f;
3095
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
3096
3096
  const isImplicitProp = component !== undefined;
3097
3097
  const endSnippet = str.original.lastIndexOf('{', snippetBlock.end - 1);
3098
3098
  // Return something to silence the "snippet type not assignable to return type void" error
3099
3099
  str.overwrite(endSnippet, snippetBlock.end, `};return __sveltets_2_any(0)}${isImplicitProp ? '' : ';'}`, {
3100
3100
  contentOnly: true
3101
3101
  });
3102
- const startEnd = str.original.indexOf('}',
3103
- // context was the first iteration in a .next release, remove at some point
3104
- ((_b = (_a = snippetBlock.parameters) === null || _a === void 0 ? void 0 : _a.at(-1)) === null || _b === void 0 ? void 0 : _b.end) || snippetBlock.expression.end) + 1;
3102
+ const lastParameter = (_a = snippetBlock.parameters) === null || _a === void 0 ? void 0 : _a.at(-1);
3103
+ const startEnd = str.original.indexOf('}', (_d = (_c = (_b = lastParameter === null || lastParameter === void 0 ? void 0 : lastParameter.typeAnnotation) === null || _b === void 0 ? void 0 : _b.end) !== null && _c !== void 0 ? _c : lastParameter === null || lastParameter === void 0 ? void 0 : lastParameter.end) !== null && _d !== void 0 ? _d : snippetBlock.expression.end) + 1;
3105
3104
  if (isImplicitProp) {
3106
3105
  str.overwrite(snippetBlock.start, snippetBlock.expression.start, '', { contentOnly: true });
3107
3106
  const transforms = ['('];
3108
- if ((_c = snippetBlock.parameters) === null || _c === void 0 ? void 0 : _c.length) {
3109
- const start = (_d = snippetBlock.parameters) === null || _d === void 0 ? void 0 : _d[0].start;
3110
- const end = snippetBlock.parameters.at(-1).end;
3107
+ if ((_e = snippetBlock.parameters) === null || _e === void 0 ? void 0 : _e.length) {
3108
+ const start = snippetBlock.parameters[0].start;
3109
+ const end = (_g = (_f = lastParameter.typeAnnotation) === null || _f === void 0 ? void 0 : _f.end) !== null && _g !== void 0 ? _g : lastParameter.end;
3111
3110
  transforms.push([start, end]);
3112
3111
  str.overwrite(snippetBlock.expression.end, start, '', {
3113
3112
  contentOnly: true
@@ -3123,14 +3122,26 @@ function handleSnippet(str, snippetBlock, component) {
3123
3122
  }
3124
3123
  else {
3125
3124
  let generic = '';
3126
- if ((_e = snippetBlock.parameters) === null || _e === void 0 ? void 0 : _e.length) {
3125
+ if ((_h = snippetBlock.parameters) === null || _h === void 0 ? void 0 : _h.length) {
3127
3126
  generic = `<[${snippetBlock.parameters
3128
3127
  .map((p) => {
3129
- var _a;
3130
- return ((_a = p.typeAnnotation) === null || _a === void 0 ? void 0 : _a.typeAnnotation)
3131
- ? str.original.slice(p.typeAnnotation.typeAnnotation.start, p.typeAnnotation.typeAnnotation.end)
3132
- : // slap any on to it to silence "implicit any" errors; JSDoc people can't add types to snippets
3133
- 'any';
3128
+ var _a, _b;
3129
+ let typeAnnotation = p.typeAnnotation;
3130
+ if (!typeAnnotation && p.type === 'AssignmentPattern') {
3131
+ typeAnnotation = (_a = p.left) === null || _a === void 0 ? void 0 : _a.typeAnnotation;
3132
+ if (!typeAnnotation) {
3133
+ typeAnnotation = (_b = p.right) === null || _b === void 0 ? void 0 : _b.typeAnnotation;
3134
+ }
3135
+ }
3136
+ // fall back to `any` to silence "implicit any" errors; JSDoc people can't add types to snippets
3137
+ let type = 'any';
3138
+ if (typeAnnotation === null || typeAnnotation === void 0 ? void 0 : typeAnnotation.typeAnnotation) {
3139
+ type = str.original.slice(typeAnnotation.typeAnnotation.start, typeAnnotation.typeAnnotation.end);
3140
+ }
3141
+ if (p.optional || p.type === 'AssignmentPattern') {
3142
+ type += '?';
3143
+ }
3144
+ return type;
3134
3145
  })
3135
3146
  .join(', ')}]>`;
3136
3147
  }
@@ -3140,9 +3151,9 @@ function handleSnippet(str, snippetBlock, component) {
3140
3151
  [snippetBlock.expression.start, snippetBlock.expression.end],
3141
3152
  typeAnnotation + ' = ('
3142
3153
  ];
3143
- if ((_f = snippetBlock.parameters) === null || _f === void 0 ? void 0 : _f.length) {
3154
+ if ((_j = snippetBlock.parameters) === null || _j === void 0 ? void 0 : _j.length) {
3144
3155
  const start = snippetBlock.parameters[0].start;
3145
- const end = snippetBlock.parameters.at(-1).end;
3156
+ const end = (_l = (_k = lastParameter.typeAnnotation) === null || _k === void 0 ? void 0 : _k.end) !== null && _l !== void 0 ? _l : lastParameter.end;
3146
3157
  transforms.push([start, end]);
3147
3158
  }
3148
3159
  transforms.push(') => {async () => {'); // inner async function for potential #await blocks
@@ -3235,7 +3246,7 @@ function convertHtmlxToJsx(str, ast, onWalk = null, onLeave = null, options = {
3235
3246
  estreeTypedParent.type === 'InlineComponent'
3236
3247
  ? element
3237
3248
  : undefined);
3238
- if (!element) {
3249
+ if (parent === ast) {
3239
3250
  // root snippet -> move to instance script
3240
3251
  rootSnippets.push([node.start, node.end]);
3241
3252
  }
@@ -6040,7 +6051,7 @@ function processInstanceScriptContent(str, script, events, implicitStoreValues,
6040
6051
  }
6041
6052
  };
6042
6053
  const walk = (node, parent) => {
6043
- var _a;
6054
+ var _a, _b, _c;
6044
6055
  const onLeaveCallbacks = [];
6045
6056
  generics.addIfIsGeneric(node);
6046
6057
  if (is$$EventsDeclaration(node)) {
@@ -6083,7 +6094,10 @@ function processInstanceScriptContent(str, script, events, implicitStoreValues,
6083
6094
  if (ts.isVariableDeclaration(node)) {
6084
6095
  events.checkIfIsStringLiteralDeclaration(node);
6085
6096
  events.checkIfDeclarationInstantiatedEventDispatcher(node);
6086
- implicitStoreValues.addVariableDeclaration(node);
6097
+ // Only top level declarations can be stores
6098
+ if (((_b = (_a = node.parent) === null || _a === void 0 ? void 0 : _a.parent) === null || _b === void 0 ? void 0 : _b.parent) === tsAst) {
6099
+ implicitStoreValues.addVariableDeclaration(node);
6100
+ }
6087
6101
  }
6088
6102
  if (ts.isCallExpression(node)) {
6089
6103
  events.checkIfCallExpressionIsDispatch(node);
@@ -6128,7 +6142,7 @@ function processInstanceScriptContent(str, script, events, implicitStoreValues,
6128
6142
  // Defensively call function (checking for undefined) because it got added only recently (TS 4.0)
6129
6143
  // and therefore might break people using older TS versions
6130
6144
  // Don't transform in ts mode because <type>value type assertions are valid in this case
6131
- if (mode !== 'ts' && ((_a = ts.isTypeAssertionExpression) === null || _a === void 0 ? void 0 : _a.call(ts, node))) {
6145
+ if (mode !== 'ts' && ((_c = ts.isTypeAssertionExpression) === null || _c === void 0 ? void 0 : _c.call(ts, node))) {
6132
6146
  handleTypeAssertion(str, node, astOffset);
6133
6147
  }
6134
6148
  //to save a bunch of condition checks on each node, we recurse into processChild which skips all the checks for top level items
package/index.mjs CHANGED
@@ -3072,22 +3072,21 @@ function handleTransitionDirective(str, attr, element) {
3072
3072
  * ```
3073
3073
  */
3074
3074
  function handleSnippet(str, snippetBlock, component) {
3075
- var _a, _b, _c, _d, _e, _f;
3075
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
3076
3076
  const isImplicitProp = component !== undefined;
3077
3077
  const endSnippet = str.original.lastIndexOf('{', snippetBlock.end - 1);
3078
3078
  // Return something to silence the "snippet type not assignable to return type void" error
3079
3079
  str.overwrite(endSnippet, snippetBlock.end, `};return __sveltets_2_any(0)}${isImplicitProp ? '' : ';'}`, {
3080
3080
  contentOnly: true
3081
3081
  });
3082
- const startEnd = str.original.indexOf('}',
3083
- // context was the first iteration in a .next release, remove at some point
3084
- ((_b = (_a = snippetBlock.parameters) === null || _a === void 0 ? void 0 : _a.at(-1)) === null || _b === void 0 ? void 0 : _b.end) || snippetBlock.expression.end) + 1;
3082
+ const lastParameter = (_a = snippetBlock.parameters) === null || _a === void 0 ? void 0 : _a.at(-1);
3083
+ const startEnd = str.original.indexOf('}', (_d = (_c = (_b = lastParameter === null || lastParameter === void 0 ? void 0 : lastParameter.typeAnnotation) === null || _b === void 0 ? void 0 : _b.end) !== null && _c !== void 0 ? _c : lastParameter === null || lastParameter === void 0 ? void 0 : lastParameter.end) !== null && _d !== void 0 ? _d : snippetBlock.expression.end) + 1;
3085
3084
  if (isImplicitProp) {
3086
3085
  str.overwrite(snippetBlock.start, snippetBlock.expression.start, '', { contentOnly: true });
3087
3086
  const transforms = ['('];
3088
- if ((_c = snippetBlock.parameters) === null || _c === void 0 ? void 0 : _c.length) {
3089
- const start = (_d = snippetBlock.parameters) === null || _d === void 0 ? void 0 : _d[0].start;
3090
- const end = snippetBlock.parameters.at(-1).end;
3087
+ if ((_e = snippetBlock.parameters) === null || _e === void 0 ? void 0 : _e.length) {
3088
+ const start = snippetBlock.parameters[0].start;
3089
+ const end = (_g = (_f = lastParameter.typeAnnotation) === null || _f === void 0 ? void 0 : _f.end) !== null && _g !== void 0 ? _g : lastParameter.end;
3091
3090
  transforms.push([start, end]);
3092
3091
  str.overwrite(snippetBlock.expression.end, start, '', {
3093
3092
  contentOnly: true
@@ -3103,14 +3102,26 @@ function handleSnippet(str, snippetBlock, component) {
3103
3102
  }
3104
3103
  else {
3105
3104
  let generic = '';
3106
- if ((_e = snippetBlock.parameters) === null || _e === void 0 ? void 0 : _e.length) {
3105
+ if ((_h = snippetBlock.parameters) === null || _h === void 0 ? void 0 : _h.length) {
3107
3106
  generic = `<[${snippetBlock.parameters
3108
3107
  .map((p) => {
3109
- var _a;
3110
- return ((_a = p.typeAnnotation) === null || _a === void 0 ? void 0 : _a.typeAnnotation)
3111
- ? str.original.slice(p.typeAnnotation.typeAnnotation.start, p.typeAnnotation.typeAnnotation.end)
3112
- : // slap any on to it to silence "implicit any" errors; JSDoc people can't add types to snippets
3113
- 'any';
3108
+ var _a, _b;
3109
+ let typeAnnotation = p.typeAnnotation;
3110
+ if (!typeAnnotation && p.type === 'AssignmentPattern') {
3111
+ typeAnnotation = (_a = p.left) === null || _a === void 0 ? void 0 : _a.typeAnnotation;
3112
+ if (!typeAnnotation) {
3113
+ typeAnnotation = (_b = p.right) === null || _b === void 0 ? void 0 : _b.typeAnnotation;
3114
+ }
3115
+ }
3116
+ // fall back to `any` to silence "implicit any" errors; JSDoc people can't add types to snippets
3117
+ let type = 'any';
3118
+ if (typeAnnotation === null || typeAnnotation === void 0 ? void 0 : typeAnnotation.typeAnnotation) {
3119
+ type = str.original.slice(typeAnnotation.typeAnnotation.start, typeAnnotation.typeAnnotation.end);
3120
+ }
3121
+ if (p.optional || p.type === 'AssignmentPattern') {
3122
+ type += '?';
3123
+ }
3124
+ return type;
3114
3125
  })
3115
3126
  .join(', ')}]>`;
3116
3127
  }
@@ -3120,9 +3131,9 @@ function handleSnippet(str, snippetBlock, component) {
3120
3131
  [snippetBlock.expression.start, snippetBlock.expression.end],
3121
3132
  typeAnnotation + ' = ('
3122
3133
  ];
3123
- if ((_f = snippetBlock.parameters) === null || _f === void 0 ? void 0 : _f.length) {
3134
+ if ((_j = snippetBlock.parameters) === null || _j === void 0 ? void 0 : _j.length) {
3124
3135
  const start = snippetBlock.parameters[0].start;
3125
- const end = snippetBlock.parameters.at(-1).end;
3136
+ const end = (_l = (_k = lastParameter.typeAnnotation) === null || _k === void 0 ? void 0 : _k.end) !== null && _l !== void 0 ? _l : lastParameter.end;
3126
3137
  transforms.push([start, end]);
3127
3138
  }
3128
3139
  transforms.push(') => {async () => {'); // inner async function for potential #await blocks
@@ -3215,7 +3226,7 @@ function convertHtmlxToJsx(str, ast, onWalk = null, onLeave = null, options = {
3215
3226
  estreeTypedParent.type === 'InlineComponent'
3216
3227
  ? element
3217
3228
  : undefined);
3218
- if (!element) {
3229
+ if (parent === ast) {
3219
3230
  // root snippet -> move to instance script
3220
3231
  rootSnippets.push([node.start, node.end]);
3221
3232
  }
@@ -6020,7 +6031,7 @@ function processInstanceScriptContent(str, script, events, implicitStoreValues,
6020
6031
  }
6021
6032
  };
6022
6033
  const walk = (node, parent) => {
6023
- var _a;
6034
+ var _a, _b, _c;
6024
6035
  const onLeaveCallbacks = [];
6025
6036
  generics.addIfIsGeneric(node);
6026
6037
  if (is$$EventsDeclaration(node)) {
@@ -6063,7 +6074,10 @@ function processInstanceScriptContent(str, script, events, implicitStoreValues,
6063
6074
  if (ts.isVariableDeclaration(node)) {
6064
6075
  events.checkIfIsStringLiteralDeclaration(node);
6065
6076
  events.checkIfDeclarationInstantiatedEventDispatcher(node);
6066
- implicitStoreValues.addVariableDeclaration(node);
6077
+ // Only top level declarations can be stores
6078
+ if (((_b = (_a = node.parent) === null || _a === void 0 ? void 0 : _a.parent) === null || _b === void 0 ? void 0 : _b.parent) === tsAst) {
6079
+ implicitStoreValues.addVariableDeclaration(node);
6080
+ }
6067
6081
  }
6068
6082
  if (ts.isCallExpression(node)) {
6069
6083
  events.checkIfCallExpressionIsDispatch(node);
@@ -6108,7 +6122,7 @@ function processInstanceScriptContent(str, script, events, implicitStoreValues,
6108
6122
  // Defensively call function (checking for undefined) because it got added only recently (TS 4.0)
6109
6123
  // and therefore might break people using older TS versions
6110
6124
  // Don't transform in ts mode because <type>value type assertions are valid in this case
6111
- if (mode !== 'ts' && ((_a = ts.isTypeAssertionExpression) === null || _a === void 0 ? void 0 : _a.call(ts, node))) {
6125
+ if (mode !== 'ts' && ((_c = ts.isTypeAssertionExpression) === null || _c === void 0 ? void 0 : _c.call(ts, node))) {
6112
6126
  handleTypeAssertion(str, node, astOffset);
6113
6127
  }
6114
6128
  //to save a bunch of condition checks on each node, we recurse into processChild which skips all the checks for top level items
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte2tsx",
3
- "version": "0.7.10",
3
+ "version": "0.7.12",
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": "~3.57.0",
41
41
  "tiny-glob": "^0.2.6",
42
42
  "tslib": "^2.4.0",
43
- "typescript": "^5.4.5"
43
+ "typescript": "^5.5.2"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "svelte": "^3.55 || ^4.0.0-next.0 || ^4.0 || ^5.0.0-next.0",