svelte-origin 1.0.0-next.23 → 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/cli.js +441 -18
- package/dist/index.js +441 -18
- package/dist/plugin.js +441 -18
- package/dist/post-process.js +441 -18
- package/dist/preprocess.js +441 -18
- package/dist/transform/attrs-origin-transform.d.ts +15 -0
- package/dist/transform/origin-destructure-transform.d.ts +62 -0
- package/dist/transform/reserved-keywords.d.ts +87 -0
- package/dist/transform/schema.d.ts +11 -4
- package/dist/vite-dts.js +79 -6
- package/package.json +1 -1
package/dist/post-process.js
CHANGED
|
@@ -2253,6 +2253,79 @@ function transformStandaloneAttrsDefinition(content) {
|
|
|
2253
2253
|
}`;
|
|
2254
2254
|
}
|
|
2255
2255
|
|
|
2256
|
+
// src/transform/reserved-keywords.ts
|
|
2257
|
+
var RESERVED_KEYWORDS = new Set([
|
|
2258
|
+
"break",
|
|
2259
|
+
"case",
|
|
2260
|
+
"catch",
|
|
2261
|
+
"continue",
|
|
2262
|
+
"debugger",
|
|
2263
|
+
"default",
|
|
2264
|
+
"delete",
|
|
2265
|
+
"do",
|
|
2266
|
+
"else",
|
|
2267
|
+
"finally",
|
|
2268
|
+
"for",
|
|
2269
|
+
"function",
|
|
2270
|
+
"if",
|
|
2271
|
+
"in",
|
|
2272
|
+
"instanceof",
|
|
2273
|
+
"new",
|
|
2274
|
+
"return",
|
|
2275
|
+
"switch",
|
|
2276
|
+
"this",
|
|
2277
|
+
"throw",
|
|
2278
|
+
"try",
|
|
2279
|
+
"typeof",
|
|
2280
|
+
"var",
|
|
2281
|
+
"void",
|
|
2282
|
+
"while",
|
|
2283
|
+
"with",
|
|
2284
|
+
"class",
|
|
2285
|
+
"const",
|
|
2286
|
+
"enum",
|
|
2287
|
+
"export",
|
|
2288
|
+
"extends",
|
|
2289
|
+
"import",
|
|
2290
|
+
"super",
|
|
2291
|
+
"implements",
|
|
2292
|
+
"interface",
|
|
2293
|
+
"let",
|
|
2294
|
+
"package",
|
|
2295
|
+
"private",
|
|
2296
|
+
"protected",
|
|
2297
|
+
"public",
|
|
2298
|
+
"static",
|
|
2299
|
+
"yield",
|
|
2300
|
+
"await",
|
|
2301
|
+
"async",
|
|
2302
|
+
"true",
|
|
2303
|
+
"false",
|
|
2304
|
+
"null",
|
|
2305
|
+
"undefined",
|
|
2306
|
+
"arguments",
|
|
2307
|
+
"eval"
|
|
2308
|
+
]);
|
|
2309
|
+
function getInternalVarName(propName, forcePrefix = true) {
|
|
2310
|
+
return `___${propName}`;
|
|
2311
|
+
}
|
|
2312
|
+
function generatePropDestructure(propName, defaultValue) {
|
|
2313
|
+
const internalName = getInternalVarName(propName);
|
|
2314
|
+
return `${propName}: ${internalName} = ${defaultValue}`;
|
|
2315
|
+
}
|
|
2316
|
+
function generatePropDestructureNoDefault(propName) {
|
|
2317
|
+
const internalName = getInternalVarName(propName);
|
|
2318
|
+
return `${propName}: ${internalName}`;
|
|
2319
|
+
}
|
|
2320
|
+
function generateGetter(propName) {
|
|
2321
|
+
const internalName = getInternalVarName(propName);
|
|
2322
|
+
return `get ${propName}() { return ${internalName} }`;
|
|
2323
|
+
}
|
|
2324
|
+
function generateSetter(propName) {
|
|
2325
|
+
const internalName = getInternalVarName(propName);
|
|
2326
|
+
return `set ${propName}(v) { ${internalName} = v }`;
|
|
2327
|
+
}
|
|
2328
|
+
|
|
2256
2329
|
// src/transform/schema.ts
|
|
2257
2330
|
function parseOriginSchemaFromSource(source, exportName) {
|
|
2258
2331
|
const sourceResult = parseSourceOrigin(source, exportName);
|
|
@@ -2591,14 +2664,14 @@ function generatePropsDestructuring(attrs, factoryName) {
|
|
|
2591
2664
|
for (const attr of attrs) {
|
|
2592
2665
|
if (attr.bindable) {
|
|
2593
2666
|
if (attr.hasDefault) {
|
|
2594
|
-
parts.push(
|
|
2667
|
+
parts.push(generatePropDestructure(attr.key, `$bindable(${attr.defaultValue})`));
|
|
2595
2668
|
} else {
|
|
2596
|
-
parts.push(
|
|
2669
|
+
parts.push(generatePropDestructure(attr.key, "$bindable()"));
|
|
2597
2670
|
}
|
|
2598
2671
|
} else if (attr.hasDefault) {
|
|
2599
|
-
parts.push(
|
|
2672
|
+
parts.push(generatePropDestructure(attr.key, attr.defaultValue));
|
|
2600
2673
|
} else {
|
|
2601
|
-
parts.push(attr.key);
|
|
2674
|
+
parts.push(generatePropDestructureNoDefault(attr.key));
|
|
2602
2675
|
}
|
|
2603
2676
|
}
|
|
2604
2677
|
return `let { ${parts.join(", ")} }: $attrs.Of<typeof ${factoryName}> = $props()`;
|
|
@@ -2606,9 +2679,9 @@ function generatePropsDestructuring(attrs, factoryName) {
|
|
|
2606
2679
|
function generateFactoryCallFromAttrs(factoryName, attrs) {
|
|
2607
2680
|
const parts = [];
|
|
2608
2681
|
for (const attr of attrs) {
|
|
2609
|
-
parts.push(
|
|
2682
|
+
parts.push(generateGetter(attr.key));
|
|
2610
2683
|
if (attr.bindable) {
|
|
2611
|
-
parts.push(
|
|
2684
|
+
parts.push(generateSetter(attr.key));
|
|
2612
2685
|
}
|
|
2613
2686
|
}
|
|
2614
2687
|
return `${factoryName}({ ${parts.join(", ")} })`;
|
|
@@ -2742,7 +2815,10 @@ function transformAttrsOriginCallsSync(s, source, neededImports) {
|
|
|
2742
2815
|
let firstFactoryExpr = null;
|
|
2743
2816
|
let propsTypeDeclaration = null;
|
|
2744
2817
|
let isFirstDeclaration = true;
|
|
2745
|
-
const
|
|
2818
|
+
const originInstances = new Map;
|
|
2819
|
+
const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
2820
|
+
const hasExistingProps = !!existingPropsMatch;
|
|
2821
|
+
const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source) && !hasExistingProps;
|
|
2746
2822
|
for (const decl of declarations) {
|
|
2747
2823
|
const { varName, startIndex, macroOpenParen } = decl;
|
|
2748
2824
|
const closeParenIndex = findMatchingBracket(source, macroOpenParen);
|
|
@@ -2763,20 +2839,47 @@ function transformAttrsOriginCallsSync(s, source, neededImports) {
|
|
|
2763
2839
|
expansionLines.push(`type $$Props = $attrs.Of<${typeExpr}>`);
|
|
2764
2840
|
propsTypeDeclaration = "";
|
|
2765
2841
|
}
|
|
2766
|
-
|
|
2842
|
+
if (hasExistingProps) {
|
|
2843
|
+
const existingContent = existingPropsMatch[1];
|
|
2844
|
+
const hasRestSpread = /\.\.\.\s*(\w+)\s*$/.test(existingContent);
|
|
2845
|
+
if (!hasRestSpread && isFirstDeclaration) {
|
|
2846
|
+
const restVarName = "___originAttrs";
|
|
2847
|
+
const newContent = existingContent.trimEnd() ? `${existingContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
2848
|
+
const braceEnd = source.indexOf("}", existingPropsMatch.index + 5);
|
|
2849
|
+
if (braceEnd !== -1) {
|
|
2850
|
+
s.overwrite(existingPropsMatch.index + 5, braceEnd, ` ${newContent} `);
|
|
2851
|
+
}
|
|
2852
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVarName}))`);
|
|
2853
|
+
} else {
|
|
2854
|
+
const restMatch = existingContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
2855
|
+
const restVar = restMatch ? restMatch[1] : "___originAttrs";
|
|
2856
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVar}))`);
|
|
2857
|
+
}
|
|
2858
|
+
} else {
|
|
2859
|
+
expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
|
|
2860
|
+
}
|
|
2767
2861
|
s.overwrite(startIndex, endIndex, expansionLines.join(`
|
|
2768
2862
|
`));
|
|
2863
|
+
originInstances.set(varName, {
|
|
2864
|
+
varName,
|
|
2865
|
+
factoryExpr,
|
|
2866
|
+
startPos: startIndex,
|
|
2867
|
+
endPos: endIndex
|
|
2868
|
+
});
|
|
2769
2869
|
neededImports.add("__attrsFor");
|
|
2770
2870
|
changed = true;
|
|
2771
2871
|
isFirstDeclaration = false;
|
|
2772
2872
|
}
|
|
2773
|
-
return { changed, propsTypeDeclaration };
|
|
2873
|
+
return { changed, propsTypeDeclaration, originInstances };
|
|
2774
2874
|
}
|
|
2775
2875
|
async function transformAttrsOriginCalls(s, source, neededImports, schemaResolver) {
|
|
2776
2876
|
const declarations = findVariableDeclarationsWithMacro(source, "$attrs.origin");
|
|
2777
2877
|
let changed = false;
|
|
2778
2878
|
let propsTypeDeclaration = null;
|
|
2779
|
-
const
|
|
2879
|
+
const originInstances = new Map;
|
|
2880
|
+
const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
2881
|
+
const hasExistingProps = !!existingPropsMatch;
|
|
2882
|
+
const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source) && !hasExistingProps;
|
|
2780
2883
|
const matches = [];
|
|
2781
2884
|
for (const decl of declarations) {
|
|
2782
2885
|
const { varName, startIndex, macroOpenParen } = decl;
|
|
@@ -2796,6 +2899,7 @@ async function transformAttrsOriginCalls(s, source, neededImports, schemaResolve
|
|
|
2796
2899
|
}
|
|
2797
2900
|
let firstFactoryExpr = null;
|
|
2798
2901
|
let isFirstDeclaration = true;
|
|
2902
|
+
let restVarInjected = false;
|
|
2799
2903
|
for (const { varName, factoryExpr, startIndex, endIndex, isGeneric } of matches) {
|
|
2800
2904
|
const expansionLines = [];
|
|
2801
2905
|
let usesFallback = true;
|
|
@@ -2823,15 +2927,40 @@ async function transformAttrsOriginCalls(s, source, neededImports, schemaResolve
|
|
|
2823
2927
|
}
|
|
2824
2928
|
if (usesFallback) {
|
|
2825
2929
|
const normalizedFactoryCall = normalizeFactoryCall(factoryExpr);
|
|
2826
|
-
|
|
2930
|
+
if (hasExistingProps) {
|
|
2931
|
+
const existingContent = existingPropsMatch[1];
|
|
2932
|
+
const hasRestSpread = /\.\.\.\s*(\w+)\s*$/.test(existingContent);
|
|
2933
|
+
if (!hasRestSpread && !restVarInjected) {
|
|
2934
|
+
const restVarName = "___originAttrs";
|
|
2935
|
+
const newContent = existingContent.trimEnd() ? `${existingContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
2936
|
+
const braceEnd = source.indexOf("}", existingPropsMatch.index + 5);
|
|
2937
|
+
if (braceEnd !== -1) {
|
|
2938
|
+
s.overwrite(existingPropsMatch.index + 5, braceEnd, ` ${newContent} `);
|
|
2939
|
+
restVarInjected = true;
|
|
2940
|
+
}
|
|
2941
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVarName}))`);
|
|
2942
|
+
} else {
|
|
2943
|
+
const restMatch = existingContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
2944
|
+
const restVar = restMatch ? restMatch[1] : "___originAttrs";
|
|
2945
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVar}))`);
|
|
2946
|
+
}
|
|
2947
|
+
} else {
|
|
2948
|
+
expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
|
|
2949
|
+
}
|
|
2827
2950
|
neededImports.add("__attrsFor");
|
|
2828
2951
|
}
|
|
2829
2952
|
s.overwrite(startIndex, endIndex, expansionLines.join(`
|
|
2830
2953
|
`));
|
|
2954
|
+
originInstances.set(varName, {
|
|
2955
|
+
varName,
|
|
2956
|
+
factoryExpr,
|
|
2957
|
+
startPos: startIndex,
|
|
2958
|
+
endPos: endIndex
|
|
2959
|
+
});
|
|
2831
2960
|
changed = true;
|
|
2832
2961
|
isFirstDeclaration = false;
|
|
2833
2962
|
}
|
|
2834
|
-
return { changed, propsTypeDeclaration };
|
|
2963
|
+
return { changed, propsTypeDeclaration, originInstances };
|
|
2835
2964
|
}
|
|
2836
2965
|
|
|
2837
2966
|
// src/transform/element-types.ts
|
|
@@ -2843,9 +2972,9 @@ function getElementTypeImport(element) {
|
|
|
2843
2972
|
function generateReactiveAttrsWrapper(attrs) {
|
|
2844
2973
|
const parts = [];
|
|
2845
2974
|
for (const attr of attrs) {
|
|
2846
|
-
parts.push(
|
|
2975
|
+
parts.push(generateGetter(attr.key));
|
|
2847
2976
|
if (attr.bindable) {
|
|
2848
|
-
parts.push(
|
|
2977
|
+
parts.push(generateSetter(attr.key));
|
|
2849
2978
|
}
|
|
2850
2979
|
}
|
|
2851
2980
|
return `{ ${parts.join(", ")} }`;
|
|
@@ -2855,14 +2984,14 @@ function generateAttrsForMerge(attrs) {
|
|
|
2855
2984
|
for (const attr of attrs) {
|
|
2856
2985
|
if (attr.bindable) {
|
|
2857
2986
|
if (attr.hasDefault) {
|
|
2858
|
-
parts.push(
|
|
2987
|
+
parts.push(generatePropDestructure(attr.key, `$bindable(${attr.defaultValue})`));
|
|
2859
2988
|
} else {
|
|
2860
|
-
parts.push(
|
|
2989
|
+
parts.push(generatePropDestructure(attr.key, "$bindable()"));
|
|
2861
2990
|
}
|
|
2862
2991
|
} else if (attr.hasDefault) {
|
|
2863
|
-
parts.push(
|
|
2992
|
+
parts.push(generatePropDestructure(attr.key, attr.defaultValue));
|
|
2864
2993
|
} else {
|
|
2865
|
-
parts.push(attr.key);
|
|
2994
|
+
parts.push(generatePropDestructureNoDefault(attr.key));
|
|
2866
2995
|
}
|
|
2867
2996
|
}
|
|
2868
2997
|
return parts.join(", ");
|
|
@@ -3039,6 +3168,291 @@ async function transformAttrsForCalls(s, source, neededImports, schemaResolver)
|
|
|
3039
3168
|
return changed;
|
|
3040
3169
|
}
|
|
3041
3170
|
|
|
3171
|
+
// src/transform/origin-destructure-transform.ts
|
|
3172
|
+
function transformOriginDestructuring(s, source, originInstances) {
|
|
3173
|
+
let changed = false;
|
|
3174
|
+
const destructures = findOriginDestructures(source, originInstances);
|
|
3175
|
+
for (const d of destructures.reverse()) {
|
|
3176
|
+
const replacement = generateDestructureReplacement(d, originInstances);
|
|
3177
|
+
if (replacement) {
|
|
3178
|
+
s.overwrite(d.startPos, d.endPos, replacement);
|
|
3179
|
+
changed = true;
|
|
3180
|
+
}
|
|
3181
|
+
}
|
|
3182
|
+
return changed;
|
|
3183
|
+
}
|
|
3184
|
+
function findOriginDestructures(source, originInstances) {
|
|
3185
|
+
const results = [];
|
|
3186
|
+
const destructurePattern = /\b(let|const|var)\s*\{/g;
|
|
3187
|
+
let match;
|
|
3188
|
+
while ((match = destructurePattern.exec(source)) !== null) {
|
|
3189
|
+
if (isInsideStringOrComment(source, match.index))
|
|
3190
|
+
continue;
|
|
3191
|
+
const declKeyword = match[1];
|
|
3192
|
+
const braceStart = match.index + match[0].length - 1;
|
|
3193
|
+
const braceEnd = findMatchingBracket(source, braceStart, "{", "}");
|
|
3194
|
+
if (braceEnd === -1)
|
|
3195
|
+
continue;
|
|
3196
|
+
let i = braceEnd + 1;
|
|
3197
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3198
|
+
i++;
|
|
3199
|
+
if (source[i] !== "=")
|
|
3200
|
+
continue;
|
|
3201
|
+
i++;
|
|
3202
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3203
|
+
i++;
|
|
3204
|
+
const exprStart = i;
|
|
3205
|
+
let sourceVar = "";
|
|
3206
|
+
while (i < source.length && /[\w$]/.test(source[i])) {
|
|
3207
|
+
sourceVar += source[i];
|
|
3208
|
+
i++;
|
|
3209
|
+
}
|
|
3210
|
+
if (!sourceVar)
|
|
3211
|
+
continue;
|
|
3212
|
+
if (!originInstances.has(sourceVar))
|
|
3213
|
+
continue;
|
|
3214
|
+
let isPropsAccess = false;
|
|
3215
|
+
const afterVarPos = i;
|
|
3216
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3217
|
+
i++;
|
|
3218
|
+
if (source[i] === ".") {
|
|
3219
|
+
i++;
|
|
3220
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3221
|
+
i++;
|
|
3222
|
+
const propName = readIdentifier(source, i);
|
|
3223
|
+
if (propName === "props") {
|
|
3224
|
+
isPropsAccess = true;
|
|
3225
|
+
i += propName.length;
|
|
3226
|
+
} else {
|
|
3227
|
+
continue;
|
|
3228
|
+
}
|
|
3229
|
+
}
|
|
3230
|
+
let endPos = i;
|
|
3231
|
+
while (endPos < source.length && /\s/.test(source[endPos]) && source[endPos] !== `
|
|
3232
|
+
`)
|
|
3233
|
+
endPos++;
|
|
3234
|
+
if (source[endPos] === ";")
|
|
3235
|
+
endPos++;
|
|
3236
|
+
if (source[endPos] === `
|
|
3237
|
+
`)
|
|
3238
|
+
endPos++;
|
|
3239
|
+
const patternContent = source.slice(braceStart + 1, braceEnd);
|
|
3240
|
+
const parsed = parseDestructurePattern(patternContent, isPropsAccess);
|
|
3241
|
+
results.push({
|
|
3242
|
+
startPos: match.index,
|
|
3243
|
+
endPos,
|
|
3244
|
+
sourceVar,
|
|
3245
|
+
isPropsAccess,
|
|
3246
|
+
methods: parsed.methods,
|
|
3247
|
+
props: parsed.props,
|
|
3248
|
+
nestedPropsPattern: parsed.nestedProps
|
|
3249
|
+
});
|
|
3250
|
+
}
|
|
3251
|
+
return results;
|
|
3252
|
+
}
|
|
3253
|
+
function parseDestructurePattern(content, isPropsAccess) {
|
|
3254
|
+
const methods = [];
|
|
3255
|
+
const props = [];
|
|
3256
|
+
let nestedProps = null;
|
|
3257
|
+
const parts = splitByTopLevelCommas2(content);
|
|
3258
|
+
for (const part of parts) {
|
|
3259
|
+
const trimmed = part.trim();
|
|
3260
|
+
if (!trimmed)
|
|
3261
|
+
continue;
|
|
3262
|
+
const propsMatch = trimmed.match(/^props\s*:\s*\{/);
|
|
3263
|
+
if (propsMatch) {
|
|
3264
|
+
const braceStart = trimmed.indexOf("{");
|
|
3265
|
+
const braceEnd = findMatchingBracket(trimmed, braceStart, "{", "}");
|
|
3266
|
+
if (braceEnd !== -1) {
|
|
3267
|
+
const nestedContent = trimmed.slice(braceStart + 1, braceEnd);
|
|
3268
|
+
const nestedParts = splitByTopLevelCommas2(nestedContent);
|
|
3269
|
+
const nestedPropsList = [];
|
|
3270
|
+
for (const np of nestedParts) {
|
|
3271
|
+
const parsed = parseDestructuredProp(np.trim());
|
|
3272
|
+
if (parsed)
|
|
3273
|
+
nestedPropsList.push(parsed);
|
|
3274
|
+
}
|
|
3275
|
+
nestedProps = {
|
|
3276
|
+
startPos: 0,
|
|
3277
|
+
endPos: 0,
|
|
3278
|
+
props: nestedPropsList
|
|
3279
|
+
};
|
|
3280
|
+
}
|
|
3281
|
+
continue;
|
|
3282
|
+
}
|
|
3283
|
+
if (isPropsAccess) {
|
|
3284
|
+
const parsed = parseDestructuredProp(trimmed);
|
|
3285
|
+
if (parsed)
|
|
3286
|
+
props.push(parsed);
|
|
3287
|
+
} else {
|
|
3288
|
+
const parsed = parseDestructuredItem(trimmed);
|
|
3289
|
+
if (parsed)
|
|
3290
|
+
methods.push(parsed);
|
|
3291
|
+
}
|
|
3292
|
+
}
|
|
3293
|
+
return { methods, props, nestedProps };
|
|
3294
|
+
}
|
|
3295
|
+
function parseDestructuredItem(part) {
|
|
3296
|
+
const trimmed = part.trim();
|
|
3297
|
+
if (!trimmed)
|
|
3298
|
+
return null;
|
|
3299
|
+
const colonIdx = findTopLevelColon(trimmed);
|
|
3300
|
+
let key;
|
|
3301
|
+
let alias = null;
|
|
3302
|
+
let rest = trimmed;
|
|
3303
|
+
if (colonIdx !== -1) {
|
|
3304
|
+
key = trimmed.slice(0, colonIdx).trim();
|
|
3305
|
+
rest = trimmed.slice(colonIdx + 1).trim();
|
|
3306
|
+
const eqIdx2 = findTopLevelEquals(rest);
|
|
3307
|
+
if (eqIdx2 !== -1) {
|
|
3308
|
+
alias = rest.slice(0, eqIdx2).trim();
|
|
3309
|
+
return {
|
|
3310
|
+
key,
|
|
3311
|
+
alias,
|
|
3312
|
+
defaultValue: rest.slice(eqIdx2 + 1).trim()
|
|
3313
|
+
};
|
|
3314
|
+
} else {
|
|
3315
|
+
alias = rest;
|
|
3316
|
+
return { key, alias, defaultValue: null };
|
|
3317
|
+
}
|
|
3318
|
+
}
|
|
3319
|
+
const eqIdx = findTopLevelEquals(trimmed);
|
|
3320
|
+
if (eqIdx !== -1) {
|
|
3321
|
+
key = trimmed.slice(0, eqIdx).trim();
|
|
3322
|
+
return {
|
|
3323
|
+
key,
|
|
3324
|
+
alias: null,
|
|
3325
|
+
defaultValue: trimmed.slice(eqIdx + 1).trim()
|
|
3326
|
+
};
|
|
3327
|
+
}
|
|
3328
|
+
return { key: trimmed, alias: null, defaultValue: null };
|
|
3329
|
+
}
|
|
3330
|
+
function parseDestructuredProp(part) {
|
|
3331
|
+
const item = parseDestructuredItem(part);
|
|
3332
|
+
if (!item)
|
|
3333
|
+
return null;
|
|
3334
|
+
let isBindable = false;
|
|
3335
|
+
let bindableDefault = null;
|
|
3336
|
+
if (item.defaultValue) {
|
|
3337
|
+
const bindableMatch = item.defaultValue.match(/^\$bindable\s*\(/);
|
|
3338
|
+
if (bindableMatch) {
|
|
3339
|
+
isBindable = true;
|
|
3340
|
+
const parenStart = item.defaultValue.indexOf("(");
|
|
3341
|
+
const parenEnd = findMatchingBracket(item.defaultValue, parenStart, "(", ")");
|
|
3342
|
+
if (parenEnd !== -1) {
|
|
3343
|
+
bindableDefault = item.defaultValue.slice(parenStart + 1, parenEnd).trim() || null;
|
|
3344
|
+
}
|
|
3345
|
+
}
|
|
3346
|
+
}
|
|
3347
|
+
return {
|
|
3348
|
+
...item,
|
|
3349
|
+
isBindable,
|
|
3350
|
+
bindableDefault
|
|
3351
|
+
};
|
|
3352
|
+
}
|
|
3353
|
+
function generateDestructureReplacement(d, originInstances) {
|
|
3354
|
+
const lines = [];
|
|
3355
|
+
const instance = originInstances.get(d.sourceVar);
|
|
3356
|
+
if (!instance)
|
|
3357
|
+
return null;
|
|
3358
|
+
for (const m of d.methods) {
|
|
3359
|
+
const varName = m.alias || m.key;
|
|
3360
|
+
if (m.defaultValue) {
|
|
3361
|
+
lines.push(`let ${varName} = ${d.sourceVar}.${m.key}?.bind(${d.sourceVar}) ?? ${m.defaultValue}`);
|
|
3362
|
+
} else {
|
|
3363
|
+
lines.push(`let ${varName} = ${d.sourceVar}.${m.key}.bind(${d.sourceVar})`);
|
|
3364
|
+
}
|
|
3365
|
+
}
|
|
3366
|
+
if (d.nestedPropsPattern) {
|
|
3367
|
+
for (const p of d.nestedPropsPattern.props) {
|
|
3368
|
+
lines.push(...generatePropAccessors(p, d.sourceVar));
|
|
3369
|
+
}
|
|
3370
|
+
}
|
|
3371
|
+
if (d.isPropsAccess) {
|
|
3372
|
+
for (const p of d.props) {
|
|
3373
|
+
lines.push(...generatePropAccessors(p, d.sourceVar));
|
|
3374
|
+
}
|
|
3375
|
+
}
|
|
3376
|
+
return lines.join(`
|
|
3377
|
+
`) + `
|
|
3378
|
+
`;
|
|
3379
|
+
}
|
|
3380
|
+
function generatePropAccessors(p, sourceVar) {
|
|
3381
|
+
const lines = [];
|
|
3382
|
+
const varName = p.alias || p.key;
|
|
3383
|
+
if (p.isBindable) {
|
|
3384
|
+
const defaultVal = p.bindableDefault || "undefined";
|
|
3385
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key} ?? ${defaultVal})`);
|
|
3386
|
+
} else if (p.defaultValue) {
|
|
3387
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key} ?? ${p.defaultValue})`);
|
|
3388
|
+
} else {
|
|
3389
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key})`);
|
|
3390
|
+
}
|
|
3391
|
+
return lines;
|
|
3392
|
+
}
|
|
3393
|
+
function splitByTopLevelCommas2(content) {
|
|
3394
|
+
const parts = [];
|
|
3395
|
+
let current = "";
|
|
3396
|
+
let depth = 0;
|
|
3397
|
+
for (let i = 0;i < content.length; i++) {
|
|
3398
|
+
const char = content[i];
|
|
3399
|
+
if (char === "{" || char === "(" || char === "[" || char === "<") {
|
|
3400
|
+
depth++;
|
|
3401
|
+
current += char;
|
|
3402
|
+
} else if (char === "}" || char === ")" || char === "]" || char === ">") {
|
|
3403
|
+
depth--;
|
|
3404
|
+
current += char;
|
|
3405
|
+
} else if (char === "," && depth === 0) {
|
|
3406
|
+
parts.push(current);
|
|
3407
|
+
current = "";
|
|
3408
|
+
} else {
|
|
3409
|
+
current += char;
|
|
3410
|
+
}
|
|
3411
|
+
}
|
|
3412
|
+
if (current.trim()) {
|
|
3413
|
+
parts.push(current);
|
|
3414
|
+
}
|
|
3415
|
+
return parts;
|
|
3416
|
+
}
|
|
3417
|
+
function findTopLevelColon(str) {
|
|
3418
|
+
let depth = 0;
|
|
3419
|
+
for (let i = 0;i < str.length; i++) {
|
|
3420
|
+
const char = str[i];
|
|
3421
|
+
if (char === "{" || char === "(" || char === "[" || char === "<")
|
|
3422
|
+
depth++;
|
|
3423
|
+
else if (char === "}" || char === ")" || char === "]" || char === ">")
|
|
3424
|
+
depth--;
|
|
3425
|
+
else if (char === ":" && depth === 0)
|
|
3426
|
+
return i;
|
|
3427
|
+
}
|
|
3428
|
+
return -1;
|
|
3429
|
+
}
|
|
3430
|
+
function findTopLevelEquals(str) {
|
|
3431
|
+
let depth = 0;
|
|
3432
|
+
for (let i = 0;i < str.length; i++) {
|
|
3433
|
+
const char = str[i];
|
|
3434
|
+
if (char === "{" || char === "(" || char === "[" || char === "<")
|
|
3435
|
+
depth++;
|
|
3436
|
+
else if (char === "}" || char === ")" || char === "]" || char === ">")
|
|
3437
|
+
depth--;
|
|
3438
|
+
else if (char === "=" && depth === 0) {
|
|
3439
|
+
if (str[i + 1] !== "=" && str[i + 1] !== ">") {
|
|
3440
|
+
return i;
|
|
3441
|
+
}
|
|
3442
|
+
}
|
|
3443
|
+
}
|
|
3444
|
+
return -1;
|
|
3445
|
+
}
|
|
3446
|
+
function readIdentifier(str, start) {
|
|
3447
|
+
let result = "";
|
|
3448
|
+
let i = start;
|
|
3449
|
+
while (i < str.length && /[\w$]/.test(str[i])) {
|
|
3450
|
+
result += str[i];
|
|
3451
|
+
i++;
|
|
3452
|
+
}
|
|
3453
|
+
return result;
|
|
3454
|
+
}
|
|
3455
|
+
|
|
3042
3456
|
// src/transform/core.ts
|
|
3043
3457
|
function transformScript(source, options = {}) {
|
|
3044
3458
|
if (options.schemaResolver) {
|
|
@@ -3061,6 +3475,9 @@ function transformScript(source, options = {}) {
|
|
|
3061
3475
|
const result = transformAttrsOriginCallsSync(s, source, neededImports);
|
|
3062
3476
|
changed = result.changed || changed;
|
|
3063
3477
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3478
|
+
if (result.originInstances.size > 0) {
|
|
3479
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3480
|
+
}
|
|
3064
3481
|
}
|
|
3065
3482
|
if (isComponent) {
|
|
3066
3483
|
changed = transformAttrsForCallsSync(s, source, neededImports) || changed;
|
|
@@ -3132,6 +3549,9 @@ async function transformScriptAsync(source, options) {
|
|
|
3132
3549
|
const result = await transformAttrsOriginCalls(s, source, neededImports, schemaResolver);
|
|
3133
3550
|
changed = result.changed || changed;
|
|
3134
3551
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3552
|
+
if (result.originInstances.size > 0) {
|
|
3553
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3554
|
+
}
|
|
3135
3555
|
}
|
|
3136
3556
|
if (isComponent) {
|
|
3137
3557
|
changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
|
|
@@ -3202,6 +3622,9 @@ async function transformScriptContent(source, options) {
|
|
|
3202
3622
|
const result = await transformAttrsOriginCalls(s, source, neededImports, schemaResolver);
|
|
3203
3623
|
changed = result.changed || changed;
|
|
3204
3624
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3625
|
+
if (result.originInstances.size > 0) {
|
|
3626
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3627
|
+
}
|
|
3205
3628
|
}
|
|
3206
3629
|
if (isComponent) {
|
|
3207
3630
|
changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
|