svelte-origin 1.0.0-next.22 → 1.0.0-next.24

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.

Potentially problematic release.


This version of svelte-origin might be problematic. Click here for more details.

package/dist/plugin.js CHANGED
@@ -2249,6 +2249,79 @@ function transformStandaloneAttrsDefinition(content) {
2249
2249
  }`;
2250
2250
  }
2251
2251
 
2252
+ // src/transform/reserved-keywords.ts
2253
+ var RESERVED_KEYWORDS = new Set([
2254
+ "break",
2255
+ "case",
2256
+ "catch",
2257
+ "continue",
2258
+ "debugger",
2259
+ "default",
2260
+ "delete",
2261
+ "do",
2262
+ "else",
2263
+ "finally",
2264
+ "for",
2265
+ "function",
2266
+ "if",
2267
+ "in",
2268
+ "instanceof",
2269
+ "new",
2270
+ "return",
2271
+ "switch",
2272
+ "this",
2273
+ "throw",
2274
+ "try",
2275
+ "typeof",
2276
+ "var",
2277
+ "void",
2278
+ "while",
2279
+ "with",
2280
+ "class",
2281
+ "const",
2282
+ "enum",
2283
+ "export",
2284
+ "extends",
2285
+ "import",
2286
+ "super",
2287
+ "implements",
2288
+ "interface",
2289
+ "let",
2290
+ "package",
2291
+ "private",
2292
+ "protected",
2293
+ "public",
2294
+ "static",
2295
+ "yield",
2296
+ "await",
2297
+ "async",
2298
+ "true",
2299
+ "false",
2300
+ "null",
2301
+ "undefined",
2302
+ "arguments",
2303
+ "eval"
2304
+ ]);
2305
+ function getInternalVarName(propName, forcePrefix = true) {
2306
+ return `___${propName}`;
2307
+ }
2308
+ function generatePropDestructure(propName, defaultValue) {
2309
+ const internalName = getInternalVarName(propName);
2310
+ return `${propName}: ${internalName} = ${defaultValue}`;
2311
+ }
2312
+ function generatePropDestructureNoDefault(propName) {
2313
+ const internalName = getInternalVarName(propName);
2314
+ return `${propName}: ${internalName}`;
2315
+ }
2316
+ function generateGetter(propName) {
2317
+ const internalName = getInternalVarName(propName);
2318
+ return `get ${propName}() { return ${internalName} }`;
2319
+ }
2320
+ function generateSetter(propName) {
2321
+ const internalName = getInternalVarName(propName);
2322
+ return `set ${propName}(v) { ${internalName} = v }`;
2323
+ }
2324
+
2252
2325
  // src/transform/schema.ts
2253
2326
  function parseOriginSchemaFromSource(source, exportName) {
2254
2327
  const sourceResult = parseSourceOrigin(source, exportName);
@@ -2587,14 +2660,14 @@ function generatePropsDestructuring(attrs, factoryName) {
2587
2660
  for (const attr of attrs) {
2588
2661
  if (attr.bindable) {
2589
2662
  if (attr.hasDefault) {
2590
- parts.push(`${attr.key} = $bindable(${attr.defaultValue})`);
2663
+ parts.push(generatePropDestructure(attr.key, `$bindable(${attr.defaultValue})`));
2591
2664
  } else {
2592
- parts.push(`${attr.key} = $bindable()`);
2665
+ parts.push(generatePropDestructure(attr.key, "$bindable()"));
2593
2666
  }
2594
2667
  } else if (attr.hasDefault) {
2595
- parts.push(`${attr.key} = ${attr.defaultValue}`);
2668
+ parts.push(generatePropDestructure(attr.key, attr.defaultValue));
2596
2669
  } else {
2597
- parts.push(attr.key);
2670
+ parts.push(generatePropDestructureNoDefault(attr.key));
2598
2671
  }
2599
2672
  }
2600
2673
  return `let { ${parts.join(", ")} }: $attrs.Of<typeof ${factoryName}> = $props()`;
@@ -2602,9 +2675,9 @@ function generatePropsDestructuring(attrs, factoryName) {
2602
2675
  function generateFactoryCallFromAttrs(factoryName, attrs) {
2603
2676
  const parts = [];
2604
2677
  for (const attr of attrs) {
2605
- parts.push(`get ${attr.key}() { return ${attr.key} }`);
2678
+ parts.push(generateGetter(attr.key));
2606
2679
  if (attr.bindable) {
2607
- parts.push(`set ${attr.key}(v) { ${attr.key} = v }`);
2680
+ parts.push(generateSetter(attr.key));
2608
2681
  }
2609
2682
  }
2610
2683
  return `${factoryName}({ ${parts.join(", ")} })`;
@@ -2738,7 +2811,10 @@ function transformAttrsOriginCallsSync(s, source, neededImports) {
2738
2811
  let firstFactoryExpr = null;
2739
2812
  let propsTypeDeclaration = null;
2740
2813
  let isFirstDeclaration = true;
2741
- const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source);
2814
+ const originInstances = new Map;
2815
+ const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
2816
+ const hasExistingProps = !!existingPropsMatch;
2817
+ const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source) && !hasExistingProps;
2742
2818
  for (const decl of declarations) {
2743
2819
  const { varName, startIndex, macroOpenParen } = decl;
2744
2820
  const closeParenIndex = findMatchingBracket(source, macroOpenParen);
@@ -2759,20 +2835,47 @@ function transformAttrsOriginCallsSync(s, source, neededImports) {
2759
2835
  expansionLines.push(`type $$Props = $attrs.Of<${typeExpr}>`);
2760
2836
  propsTypeDeclaration = "";
2761
2837
  }
2762
- expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
2838
+ if (hasExistingProps) {
2839
+ const existingContent = existingPropsMatch[1];
2840
+ const hasRestSpread = /\.\.\.\s*(\w+)\s*$/.test(existingContent);
2841
+ if (!hasRestSpread && isFirstDeclaration) {
2842
+ const restVarName = "___originAttrs";
2843
+ const newContent = existingContent.trimEnd() ? `${existingContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
2844
+ const braceEnd = source.indexOf("}", existingPropsMatch.index + 5);
2845
+ if (braceEnd !== -1) {
2846
+ s.overwrite(existingPropsMatch.index + 5, braceEnd, ` ${newContent} `);
2847
+ }
2848
+ expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVarName}))`);
2849
+ } else {
2850
+ const restMatch = existingContent.match(/\.\.\.\s*(\w+)\s*$/);
2851
+ const restVar = restMatch ? restMatch[1] : "___originAttrs";
2852
+ expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVar}))`);
2853
+ }
2854
+ } else {
2855
+ expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
2856
+ }
2763
2857
  s.overwrite(startIndex, endIndex, expansionLines.join(`
2764
2858
  `));
2859
+ originInstances.set(varName, {
2860
+ varName,
2861
+ factoryExpr,
2862
+ startPos: startIndex,
2863
+ endPos: endIndex
2864
+ });
2765
2865
  neededImports.add("__attrsFor");
2766
2866
  changed = true;
2767
2867
  isFirstDeclaration = false;
2768
2868
  }
2769
- return { changed, propsTypeDeclaration };
2869
+ return { changed, propsTypeDeclaration, originInstances };
2770
2870
  }
2771
2871
  async function transformAttrsOriginCalls(s, source, neededImports, schemaResolver) {
2772
2872
  const declarations = findVariableDeclarationsWithMacro(source, "$attrs.origin");
2773
2873
  let changed = false;
2774
2874
  let propsTypeDeclaration = null;
2775
- const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source);
2875
+ const originInstances = new Map;
2876
+ const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
2877
+ const hasExistingProps = !!existingPropsMatch;
2878
+ const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source) && !hasExistingProps;
2776
2879
  const matches = [];
2777
2880
  for (const decl of declarations) {
2778
2881
  const { varName, startIndex, macroOpenParen } = decl;
@@ -2792,6 +2895,7 @@ async function transformAttrsOriginCalls(s, source, neededImports, schemaResolve
2792
2895
  }
2793
2896
  let firstFactoryExpr = null;
2794
2897
  let isFirstDeclaration = true;
2898
+ let restVarInjected = false;
2795
2899
  for (const { varName, factoryExpr, startIndex, endIndex, isGeneric } of matches) {
2796
2900
  const expansionLines = [];
2797
2901
  let usesFallback = true;
@@ -2819,15 +2923,40 @@ async function transformAttrsOriginCalls(s, source, neededImports, schemaResolve
2819
2923
  }
2820
2924
  if (usesFallback) {
2821
2925
  const normalizedFactoryCall = normalizeFactoryCall(factoryExpr);
2822
- expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
2926
+ if (hasExistingProps) {
2927
+ const existingContent = existingPropsMatch[1];
2928
+ const hasRestSpread = /\.\.\.\s*(\w+)\s*$/.test(existingContent);
2929
+ if (!hasRestSpread && !restVarInjected) {
2930
+ const restVarName = "___originAttrs";
2931
+ const newContent = existingContent.trimEnd() ? `${existingContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
2932
+ const braceEnd = source.indexOf("}", existingPropsMatch.index + 5);
2933
+ if (braceEnd !== -1) {
2934
+ s.overwrite(existingPropsMatch.index + 5, braceEnd, ` ${newContent} `);
2935
+ restVarInjected = true;
2936
+ }
2937
+ expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVarName}))`);
2938
+ } else {
2939
+ const restMatch = existingContent.match(/\.\.\.\s*(\w+)\s*$/);
2940
+ const restVar = restMatch ? restMatch[1] : "___originAttrs";
2941
+ expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVar}))`);
2942
+ }
2943
+ } else {
2944
+ expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
2945
+ }
2823
2946
  neededImports.add("__attrsFor");
2824
2947
  }
2825
2948
  s.overwrite(startIndex, endIndex, expansionLines.join(`
2826
2949
  `));
2950
+ originInstances.set(varName, {
2951
+ varName,
2952
+ factoryExpr,
2953
+ startPos: startIndex,
2954
+ endPos: endIndex
2955
+ });
2827
2956
  changed = true;
2828
2957
  isFirstDeclaration = false;
2829
2958
  }
2830
- return { changed, propsTypeDeclaration };
2959
+ return { changed, propsTypeDeclaration, originInstances };
2831
2960
  }
2832
2961
 
2833
2962
  // src/transform/element-types.ts
@@ -2836,7 +2965,34 @@ function getElementTypeImport(element) {
2836
2965
  }
2837
2966
 
2838
2967
  // src/transform/attrs-for-transform.ts
2839
- function transformAttrsForCalls(s, source, neededImports) {
2968
+ function generateReactiveAttrsWrapper(attrs) {
2969
+ const parts = [];
2970
+ for (const attr of attrs) {
2971
+ parts.push(generateGetter(attr.key));
2972
+ if (attr.bindable) {
2973
+ parts.push(generateSetter(attr.key));
2974
+ }
2975
+ }
2976
+ return `{ ${parts.join(", ")} }`;
2977
+ }
2978
+ function generateAttrsForMerge(attrs) {
2979
+ const parts = [];
2980
+ for (const attr of attrs) {
2981
+ if (attr.bindable) {
2982
+ if (attr.hasDefault) {
2983
+ parts.push(generatePropDestructure(attr.key, `$bindable(${attr.defaultValue})`));
2984
+ } else {
2985
+ parts.push(generatePropDestructure(attr.key, "$bindable()"));
2986
+ }
2987
+ } else if (attr.hasDefault) {
2988
+ parts.push(generatePropDestructure(attr.key, attr.defaultValue));
2989
+ } else {
2990
+ parts.push(generatePropDestructureNoDefault(attr.key));
2991
+ }
2992
+ }
2993
+ return parts.join(", ");
2994
+ }
2995
+ function transformAttrsForCallsSync(s, source, neededImports) {
2840
2996
  let changed = false;
2841
2997
  const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
2842
2998
  let restVarName = null;
@@ -2902,6 +3058,396 @@ function transformAttrsForCalls(s, source, neededImports) {
2902
3058
  }
2903
3059
  return changed;
2904
3060
  }
3061
+ async function transformAttrsForCalls(s, source, neededImports, schemaResolver) {
3062
+ let changed = false;
3063
+ const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
3064
+ let restVarName = null;
3065
+ let existingPropsStart = -1;
3066
+ let existingDestructureContent = "";
3067
+ if (existingPropsMatch) {
3068
+ existingPropsStart = existingPropsMatch.index;
3069
+ existingDestructureContent = existingPropsMatch[1];
3070
+ const restMatch = existingDestructureContent.match(/\.\.\.\s*(\w+)\s*$/);
3071
+ if (restMatch) {
3072
+ restVarName = restMatch[1];
3073
+ }
3074
+ }
3075
+ const declarations = findVariableDeclarationsWithMacro(source, "$attrs.for");
3076
+ for (const decl of declarations) {
3077
+ const { varName, startIndex, macroOpenParen } = decl;
3078
+ const closeParenIndex = findMatchingBracket(source, macroOpenParen);
3079
+ if (closeParenIndex === -1)
3080
+ continue;
3081
+ const arg = source.slice(macroOpenParen + 1, closeParenIndex).trim();
3082
+ const endIndex = closeParenIndex + 1;
3083
+ const stringMatch = arg.match(/^['"](\w+)['"]$/);
3084
+ if (stringMatch) {
3085
+ const element = stringMatch[1];
3086
+ const typeImport = getElementTypeImport(element);
3087
+ if (existingPropsMatch && existingPropsStart !== -1) {
3088
+ if (!restVarName) {
3089
+ restVarName = "___restAttrs";
3090
+ const newDestructure = existingDestructureContent.trimEnd() ? `${existingDestructureContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
3091
+ const braceEnd = source.indexOf("}", existingPropsStart + 5);
3092
+ if (braceEnd !== -1) {
3093
+ s.overwrite(existingPropsStart + 5, braceEnd, ` ${newDestructure} `);
3094
+ }
3095
+ }
3096
+ s.overwrite(startIndex, endIndex, `let ${varName} = ${restVarName} as ${typeImport}`);
3097
+ } else {
3098
+ const expansion = `let { ...${varName} } = $props<${typeImport}>()`;
3099
+ s.overwrite(startIndex, endIndex, expansion);
3100
+ }
3101
+ } else {
3102
+ const factoryName = arg;
3103
+ let transformed = false;
3104
+ if (schemaResolver) {
3105
+ const importPath = findImportPath(source, factoryName);
3106
+ if (importPath) {
3107
+ const schema = await schemaResolver.resolve(importPath, factoryName);
3108
+ if (schema && schema.attrs.length > 0) {
3109
+ if (existingPropsMatch && existingPropsStart !== -1) {
3110
+ const additionalAttrs = generateAttrsForMerge(schema.attrs);
3111
+ const restMatch = existingDestructureContent.match(/,?\s*\.\.\.\s*(\w+)\s*$/);
3112
+ let baseContent = existingDestructureContent;
3113
+ let restPart = "";
3114
+ if (restMatch) {
3115
+ restPart = restMatch[0];
3116
+ baseContent = existingDestructureContent.slice(0, restMatch.index);
3117
+ }
3118
+ const mergedDestructure = baseContent.trimEnd() ? `${baseContent.trimEnd()}, ${additionalAttrs}${restPart}` : `${additionalAttrs}${restPart}`;
3119
+ const braceEnd = source.indexOf("}", existingPropsStart + 5);
3120
+ if (braceEnd !== -1) {
3121
+ s.overwrite(existingPropsStart + 5, braceEnd, ` ${mergedDestructure} `);
3122
+ }
3123
+ const wrapper = generateReactiveAttrsWrapper(schema.attrs);
3124
+ s.overwrite(startIndex, endIndex, `let ${varName} = __attrsFor(${factoryName}, ${wrapper})`);
3125
+ transformed = true;
3126
+ } else {
3127
+ const propsDestructure = generatePropsDestructuring(schema.attrs, factoryName);
3128
+ const wrapper = generateReactiveAttrsWrapper(schema.attrs);
3129
+ const expansion = [
3130
+ propsDestructure,
3131
+ `let ${varName} = __attrsFor(${factoryName}, ${wrapper})`
3132
+ ].join(`
3133
+ `);
3134
+ s.overwrite(startIndex, endIndex, expansion);
3135
+ transformed = true;
3136
+ }
3137
+ }
3138
+ }
3139
+ }
3140
+ if (!transformed) {
3141
+ if (existingPropsMatch && existingPropsStart !== -1) {
3142
+ if (!restVarName) {
3143
+ restVarName = "___restAttrs";
3144
+ const newDestructure = existingDestructureContent.trimEnd() ? `${existingDestructureContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
3145
+ const braceEnd = source.indexOf("}", existingPropsStart + 5);
3146
+ if (braceEnd !== -1) {
3147
+ s.overwrite(existingPropsStart + 5, braceEnd, ` ${newDestructure} `);
3148
+ }
3149
+ }
3150
+ s.overwrite(startIndex, endIndex, `let ${varName} = __attrsFor(${factoryName}, ${restVarName})`);
3151
+ } else {
3152
+ const expansion = [
3153
+ `let ___attrs: $attrs.Of<typeof ${factoryName}> = $props()`,
3154
+ `let ${varName} = __attrsFor(${factoryName}, ___attrs)`
3155
+ ].join(`
3156
+ `);
3157
+ s.overwrite(startIndex, endIndex, expansion);
3158
+ }
3159
+ }
3160
+ neededImports.add("__attrsFor");
3161
+ }
3162
+ changed = true;
3163
+ }
3164
+ return changed;
3165
+ }
3166
+
3167
+ // src/transform/origin-destructure-transform.ts
3168
+ function transformOriginDestructuring(s, source, originInstances) {
3169
+ let changed = false;
3170
+ const destructures = findOriginDestructures(source, originInstances);
3171
+ for (const d of destructures.reverse()) {
3172
+ const replacement = generateDestructureReplacement(d, originInstances);
3173
+ if (replacement) {
3174
+ s.overwrite(d.startPos, d.endPos, replacement);
3175
+ changed = true;
3176
+ }
3177
+ }
3178
+ return changed;
3179
+ }
3180
+ function findOriginDestructures(source, originInstances) {
3181
+ const results = [];
3182
+ const destructurePattern = /\b(let|const|var)\s*\{/g;
3183
+ let match;
3184
+ while ((match = destructurePattern.exec(source)) !== null) {
3185
+ if (isInsideStringOrComment(source, match.index))
3186
+ continue;
3187
+ const declKeyword = match[1];
3188
+ const braceStart = match.index + match[0].length - 1;
3189
+ const braceEnd = findMatchingBracket(source, braceStart, "{", "}");
3190
+ if (braceEnd === -1)
3191
+ continue;
3192
+ let i = braceEnd + 1;
3193
+ while (i < source.length && /\s/.test(source[i]))
3194
+ i++;
3195
+ if (source[i] !== "=")
3196
+ continue;
3197
+ i++;
3198
+ while (i < source.length && /\s/.test(source[i]))
3199
+ i++;
3200
+ const exprStart = i;
3201
+ let sourceVar = "";
3202
+ while (i < source.length && /[\w$]/.test(source[i])) {
3203
+ sourceVar += source[i];
3204
+ i++;
3205
+ }
3206
+ if (!sourceVar)
3207
+ continue;
3208
+ if (!originInstances.has(sourceVar))
3209
+ continue;
3210
+ let isPropsAccess = false;
3211
+ const afterVarPos = i;
3212
+ while (i < source.length && /\s/.test(source[i]))
3213
+ i++;
3214
+ if (source[i] === ".") {
3215
+ i++;
3216
+ while (i < source.length && /\s/.test(source[i]))
3217
+ i++;
3218
+ const propName = readIdentifier(source, i);
3219
+ if (propName === "props") {
3220
+ isPropsAccess = true;
3221
+ i += propName.length;
3222
+ } else {
3223
+ continue;
3224
+ }
3225
+ }
3226
+ let endPos = i;
3227
+ while (endPos < source.length && /\s/.test(source[endPos]) && source[endPos] !== `
3228
+ `)
3229
+ endPos++;
3230
+ if (source[endPos] === ";")
3231
+ endPos++;
3232
+ if (source[endPos] === `
3233
+ `)
3234
+ endPos++;
3235
+ const patternContent = source.slice(braceStart + 1, braceEnd);
3236
+ const parsed = parseDestructurePattern(patternContent, isPropsAccess);
3237
+ results.push({
3238
+ startPos: match.index,
3239
+ endPos,
3240
+ sourceVar,
3241
+ isPropsAccess,
3242
+ methods: parsed.methods,
3243
+ props: parsed.props,
3244
+ nestedPropsPattern: parsed.nestedProps
3245
+ });
3246
+ }
3247
+ return results;
3248
+ }
3249
+ function parseDestructurePattern(content, isPropsAccess) {
3250
+ const methods = [];
3251
+ const props = [];
3252
+ let nestedProps = null;
3253
+ const parts = splitByTopLevelCommas2(content);
3254
+ for (const part of parts) {
3255
+ const trimmed = part.trim();
3256
+ if (!trimmed)
3257
+ continue;
3258
+ const propsMatch = trimmed.match(/^props\s*:\s*\{/);
3259
+ if (propsMatch) {
3260
+ const braceStart = trimmed.indexOf("{");
3261
+ const braceEnd = findMatchingBracket(trimmed, braceStart, "{", "}");
3262
+ if (braceEnd !== -1) {
3263
+ const nestedContent = trimmed.slice(braceStart + 1, braceEnd);
3264
+ const nestedParts = splitByTopLevelCommas2(nestedContent);
3265
+ const nestedPropsList = [];
3266
+ for (const np of nestedParts) {
3267
+ const parsed = parseDestructuredProp(np.trim());
3268
+ if (parsed)
3269
+ nestedPropsList.push(parsed);
3270
+ }
3271
+ nestedProps = {
3272
+ startPos: 0,
3273
+ endPos: 0,
3274
+ props: nestedPropsList
3275
+ };
3276
+ }
3277
+ continue;
3278
+ }
3279
+ if (isPropsAccess) {
3280
+ const parsed = parseDestructuredProp(trimmed);
3281
+ if (parsed)
3282
+ props.push(parsed);
3283
+ } else {
3284
+ const parsed = parseDestructuredItem(trimmed);
3285
+ if (parsed)
3286
+ methods.push(parsed);
3287
+ }
3288
+ }
3289
+ return { methods, props, nestedProps };
3290
+ }
3291
+ function parseDestructuredItem(part) {
3292
+ const trimmed = part.trim();
3293
+ if (!trimmed)
3294
+ return null;
3295
+ const colonIdx = findTopLevelColon(trimmed);
3296
+ let key;
3297
+ let alias = null;
3298
+ let rest = trimmed;
3299
+ if (colonIdx !== -1) {
3300
+ key = trimmed.slice(0, colonIdx).trim();
3301
+ rest = trimmed.slice(colonIdx + 1).trim();
3302
+ const eqIdx2 = findTopLevelEquals(rest);
3303
+ if (eqIdx2 !== -1) {
3304
+ alias = rest.slice(0, eqIdx2).trim();
3305
+ return {
3306
+ key,
3307
+ alias,
3308
+ defaultValue: rest.slice(eqIdx2 + 1).trim()
3309
+ };
3310
+ } else {
3311
+ alias = rest;
3312
+ return { key, alias, defaultValue: null };
3313
+ }
3314
+ }
3315
+ const eqIdx = findTopLevelEquals(trimmed);
3316
+ if (eqIdx !== -1) {
3317
+ key = trimmed.slice(0, eqIdx).trim();
3318
+ return {
3319
+ key,
3320
+ alias: null,
3321
+ defaultValue: trimmed.slice(eqIdx + 1).trim()
3322
+ };
3323
+ }
3324
+ return { key: trimmed, alias: null, defaultValue: null };
3325
+ }
3326
+ function parseDestructuredProp(part) {
3327
+ const item = parseDestructuredItem(part);
3328
+ if (!item)
3329
+ return null;
3330
+ let isBindable = false;
3331
+ let bindableDefault = null;
3332
+ if (item.defaultValue) {
3333
+ const bindableMatch = item.defaultValue.match(/^\$bindable\s*\(/);
3334
+ if (bindableMatch) {
3335
+ isBindable = true;
3336
+ const parenStart = item.defaultValue.indexOf("(");
3337
+ const parenEnd = findMatchingBracket(item.defaultValue, parenStart, "(", ")");
3338
+ if (parenEnd !== -1) {
3339
+ bindableDefault = item.defaultValue.slice(parenStart + 1, parenEnd).trim() || null;
3340
+ }
3341
+ }
3342
+ }
3343
+ return {
3344
+ ...item,
3345
+ isBindable,
3346
+ bindableDefault
3347
+ };
3348
+ }
3349
+ function generateDestructureReplacement(d, originInstances) {
3350
+ const lines = [];
3351
+ const instance = originInstances.get(d.sourceVar);
3352
+ if (!instance)
3353
+ return null;
3354
+ for (const m of d.methods) {
3355
+ const varName = m.alias || m.key;
3356
+ if (m.defaultValue) {
3357
+ lines.push(`let ${varName} = ${d.sourceVar}.${m.key}?.bind(${d.sourceVar}) ?? ${m.defaultValue}`);
3358
+ } else {
3359
+ lines.push(`let ${varName} = ${d.sourceVar}.${m.key}.bind(${d.sourceVar})`);
3360
+ }
3361
+ }
3362
+ if (d.nestedPropsPattern) {
3363
+ for (const p of d.nestedPropsPattern.props) {
3364
+ lines.push(...generatePropAccessors(p, d.sourceVar));
3365
+ }
3366
+ }
3367
+ if (d.isPropsAccess) {
3368
+ for (const p of d.props) {
3369
+ lines.push(...generatePropAccessors(p, d.sourceVar));
3370
+ }
3371
+ }
3372
+ return lines.join(`
3373
+ `) + `
3374
+ `;
3375
+ }
3376
+ function generatePropAccessors(p, sourceVar) {
3377
+ const lines = [];
3378
+ const varName = p.alias || p.key;
3379
+ if (p.isBindable) {
3380
+ const defaultVal = p.bindableDefault || "undefined";
3381
+ lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key} ?? ${defaultVal})`);
3382
+ } else if (p.defaultValue) {
3383
+ lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key} ?? ${p.defaultValue})`);
3384
+ } else {
3385
+ lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key})`);
3386
+ }
3387
+ return lines;
3388
+ }
3389
+ function splitByTopLevelCommas2(content) {
3390
+ const parts = [];
3391
+ let current = "";
3392
+ let depth = 0;
3393
+ for (let i = 0;i < content.length; i++) {
3394
+ const char = content[i];
3395
+ if (char === "{" || char === "(" || char === "[" || char === "<") {
3396
+ depth++;
3397
+ current += char;
3398
+ } else if (char === "}" || char === ")" || char === "]" || char === ">") {
3399
+ depth--;
3400
+ current += char;
3401
+ } else if (char === "," && depth === 0) {
3402
+ parts.push(current);
3403
+ current = "";
3404
+ } else {
3405
+ current += char;
3406
+ }
3407
+ }
3408
+ if (current.trim()) {
3409
+ parts.push(current);
3410
+ }
3411
+ return parts;
3412
+ }
3413
+ function findTopLevelColon(str) {
3414
+ let depth = 0;
3415
+ for (let i = 0;i < str.length; i++) {
3416
+ const char = str[i];
3417
+ if (char === "{" || char === "(" || char === "[" || char === "<")
3418
+ depth++;
3419
+ else if (char === "}" || char === ")" || char === "]" || char === ">")
3420
+ depth--;
3421
+ else if (char === ":" && depth === 0)
3422
+ return i;
3423
+ }
3424
+ return -1;
3425
+ }
3426
+ function findTopLevelEquals(str) {
3427
+ let depth = 0;
3428
+ for (let i = 0;i < str.length; i++) {
3429
+ const char = str[i];
3430
+ if (char === "{" || char === "(" || char === "[" || char === "<")
3431
+ depth++;
3432
+ else if (char === "}" || char === ")" || char === "]" || char === ">")
3433
+ depth--;
3434
+ else if (char === "=" && depth === 0) {
3435
+ if (str[i + 1] !== "=" && str[i + 1] !== ">") {
3436
+ return i;
3437
+ }
3438
+ }
3439
+ }
3440
+ return -1;
3441
+ }
3442
+ function readIdentifier(str, start) {
3443
+ let result = "";
3444
+ let i = start;
3445
+ while (i < str.length && /[\w$]/.test(str[i])) {
3446
+ result += str[i];
3447
+ i++;
3448
+ }
3449
+ return result;
3450
+ }
2905
3451
 
2906
3452
  // src/transform/core.ts
2907
3453
  function transformScript(source, options = {}) {
@@ -2925,9 +3471,12 @@ function transformScript(source, options = {}) {
2925
3471
  const result = transformAttrsOriginCallsSync(s, source, neededImports);
2926
3472
  changed = result.changed || changed;
2927
3473
  propsTypeDeclaration = result.propsTypeDeclaration;
3474
+ if (result.originInstances.size > 0) {
3475
+ changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
3476
+ }
2928
3477
  }
2929
3478
  if (isComponent) {
2930
- changed = transformAttrsForCalls(s, source, neededImports) || changed;
3479
+ changed = transformAttrsForCallsSync(s, source, neededImports) || changed;
2931
3480
  }
2932
3481
  const needsPropsInjection = propsTypeDeclaration && propsTypeDeclaration.length > 0;
2933
3482
  if (isComponent && (neededImports.size > 0 || needsPropsInjection)) {
@@ -2996,9 +3545,12 @@ async function transformScriptAsync(source, options) {
2996
3545
  const result = await transformAttrsOriginCalls(s, source, neededImports, schemaResolver);
2997
3546
  changed = result.changed || changed;
2998
3547
  propsTypeDeclaration = result.propsTypeDeclaration;
3548
+ if (result.originInstances.size > 0) {
3549
+ changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
3550
+ }
2999
3551
  }
3000
3552
  if (isComponent) {
3001
- changed = transformAttrsForCalls(s, source, neededImports) || changed;
3553
+ changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
3002
3554
  }
3003
3555
  const needsPropsInjectionAsync = propsTypeDeclaration && propsTypeDeclaration.length > 0;
3004
3556
  if (isComponent && (neededImports.size > 0 || needsPropsInjectionAsync)) {
@@ -3066,9 +3618,12 @@ async function transformScriptContent(source, options) {
3066
3618
  const result = await transformAttrsOriginCalls(s, source, neededImports, schemaResolver);
3067
3619
  changed = result.changed || changed;
3068
3620
  propsTypeDeclaration = result.propsTypeDeclaration;
3621
+ if (result.originInstances.size > 0) {
3622
+ changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
3623
+ }
3069
3624
  }
3070
3625
  if (isComponent) {
3071
- changed = transformAttrsForCalls(s, source, neededImports) || changed;
3626
+ changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
3072
3627
  }
3073
3628
  if (neededImports.size > 0) {
3074
3629
  const importStatement = generateRuntimeImport([...neededImports]);
@@ -3766,15 +4321,53 @@ async function resolveSchema(importPath, exportName, importerId, cache, debug, a
3766
4321
  }
3767
4322
  if (schema.propsRef) {
3768
4323
  const propsRefImportPath = findImportPath(source, schema.propsRef);
4324
+ let propsRefSchema = null;
3769
4325
  if (propsRefImportPath) {
3770
- const propsRefSchema = await resolveAttrsSchema(propsRefImportPath, schema.propsRef, filePath, cache, debug, aliases);
3771
- if (propsRefSchema) {
4326
+ if (debug) {
4327
+ console.log(`[svelte-origin] Resolving propsRef: ${schema.propsRef} from import ${propsRefImportPath}`);
4328
+ }
4329
+ propsRefSchema = await resolveAttrsSchema(propsRefImportPath, schema.propsRef, filePath, cache, debug, aliases);
4330
+ } else {
4331
+ if (debug) {
4332
+ console.log(`[svelte-origin] Resolving propsRef: ${schema.propsRef} from same file`);
4333
+ }
4334
+ propsRefSchema = parseAttrsSchemaFromSource(source, schema.propsRef);
4335
+ if (propsRefSchema && propsRefSchema.parentNames.length > 0) {
4336
+ const resolvedParentAttrs = [];
4337
+ for (const parentName of propsRefSchema.parentNames) {
4338
+ const parentImportPath = findImportPath(source, parentName);
4339
+ let parentSchema = null;
4340
+ if (parentImportPath) {
4341
+ parentSchema = await resolveAttrsSchema(parentImportPath, parentName, filePath, cache, debug, aliases);
4342
+ } else {
4343
+ parentSchema = parseAttrsSchemaFromSource(source, parentName);
4344
+ }
4345
+ if (parentSchema) {
4346
+ for (const attr of parentSchema.attrs) {
4347
+ const existingIndex = resolvedParentAttrs.findIndex((a) => a.key === attr.key);
4348
+ if (existingIndex === -1) {
4349
+ resolvedParentAttrs.push(attr);
4350
+ }
4351
+ }
4352
+ }
4353
+ }
3772
4354
  for (const attr of propsRefSchema.attrs) {
3773
- const existingIndex = mergedAttrs.findIndex((a) => a.key === attr.key);
3774
- if (existingIndex === -1) {
3775
- mergedAttrs.push(attr);
4355
+ const existingIndex = resolvedParentAttrs.findIndex((a) => a.key === attr.key);
4356
+ if (existingIndex !== -1) {
4357
+ resolvedParentAttrs[existingIndex] = attr;
4358
+ } else {
4359
+ resolvedParentAttrs.push(attr);
3776
4360
  }
3777
4361
  }
4362
+ propsRefSchema = { attrs: resolvedParentAttrs, parentNames: propsRefSchema.parentNames };
4363
+ }
4364
+ }
4365
+ if (propsRefSchema) {
4366
+ for (const attr of propsRefSchema.attrs) {
4367
+ const existingIndex = mergedAttrs.findIndex((a) => a.key === attr.key);
4368
+ if (existingIndex === -1) {
4369
+ mergedAttrs.push(attr);
4370
+ }
3778
4371
  }
3779
4372
  }
3780
4373
  }