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/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(
|
|
2663
|
+
parts.push(generatePropDestructure(attr.key, `$bindable(${attr.defaultValue})`));
|
|
2591
2664
|
} else {
|
|
2592
|
-
parts.push(
|
|
2665
|
+
parts.push(generatePropDestructure(attr.key, "$bindable()"));
|
|
2593
2666
|
}
|
|
2594
2667
|
} else if (attr.hasDefault) {
|
|
2595
|
-
parts.push(
|
|
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(
|
|
2678
|
+
parts.push(generateGetter(attr.key));
|
|
2606
2679
|
if (attr.bindable) {
|
|
2607
|
-
parts.push(
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
@@ -2839,9 +2968,9 @@ function getElementTypeImport(element) {
|
|
|
2839
2968
|
function generateReactiveAttrsWrapper(attrs) {
|
|
2840
2969
|
const parts = [];
|
|
2841
2970
|
for (const attr of attrs) {
|
|
2842
|
-
parts.push(
|
|
2971
|
+
parts.push(generateGetter(attr.key));
|
|
2843
2972
|
if (attr.bindable) {
|
|
2844
|
-
parts.push(
|
|
2973
|
+
parts.push(generateSetter(attr.key));
|
|
2845
2974
|
}
|
|
2846
2975
|
}
|
|
2847
2976
|
return `{ ${parts.join(", ")} }`;
|
|
@@ -2851,14 +2980,14 @@ function generateAttrsForMerge(attrs) {
|
|
|
2851
2980
|
for (const attr of attrs) {
|
|
2852
2981
|
if (attr.bindable) {
|
|
2853
2982
|
if (attr.hasDefault) {
|
|
2854
|
-
parts.push(
|
|
2983
|
+
parts.push(generatePropDestructure(attr.key, `$bindable(${attr.defaultValue})`));
|
|
2855
2984
|
} else {
|
|
2856
|
-
parts.push(
|
|
2985
|
+
parts.push(generatePropDestructure(attr.key, "$bindable()"));
|
|
2857
2986
|
}
|
|
2858
2987
|
} else if (attr.hasDefault) {
|
|
2859
|
-
parts.push(
|
|
2988
|
+
parts.push(generatePropDestructure(attr.key, attr.defaultValue));
|
|
2860
2989
|
} else {
|
|
2861
|
-
parts.push(attr.key);
|
|
2990
|
+
parts.push(generatePropDestructureNoDefault(attr.key));
|
|
2862
2991
|
}
|
|
2863
2992
|
}
|
|
2864
2993
|
return parts.join(", ");
|
|
@@ -3035,6 +3164,291 @@ async function transformAttrsForCalls(s, source, neededImports, schemaResolver)
|
|
|
3035
3164
|
return changed;
|
|
3036
3165
|
}
|
|
3037
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
|
+
}
|
|
3451
|
+
|
|
3038
3452
|
// src/transform/core.ts
|
|
3039
3453
|
function transformScript(source, options = {}) {
|
|
3040
3454
|
if (options.schemaResolver) {
|
|
@@ -3057,6 +3471,9 @@ function transformScript(source, options = {}) {
|
|
|
3057
3471
|
const result = transformAttrsOriginCallsSync(s, source, neededImports);
|
|
3058
3472
|
changed = result.changed || changed;
|
|
3059
3473
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3474
|
+
if (result.originInstances.size > 0) {
|
|
3475
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3476
|
+
}
|
|
3060
3477
|
}
|
|
3061
3478
|
if (isComponent) {
|
|
3062
3479
|
changed = transformAttrsForCallsSync(s, source, neededImports) || changed;
|
|
@@ -3128,6 +3545,9 @@ async function transformScriptAsync(source, options) {
|
|
|
3128
3545
|
const result = await transformAttrsOriginCalls(s, source, neededImports, schemaResolver);
|
|
3129
3546
|
changed = result.changed || changed;
|
|
3130
3547
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3548
|
+
if (result.originInstances.size > 0) {
|
|
3549
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3550
|
+
}
|
|
3131
3551
|
}
|
|
3132
3552
|
if (isComponent) {
|
|
3133
3553
|
changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
|
|
@@ -3198,6 +3618,9 @@ async function transformScriptContent(source, options) {
|
|
|
3198
3618
|
const result = await transformAttrsOriginCalls(s, source, neededImports, schemaResolver);
|
|
3199
3619
|
changed = result.changed || changed;
|
|
3200
3620
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3621
|
+
if (result.originInstances.size > 0) {
|
|
3622
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3623
|
+
}
|
|
3201
3624
|
}
|
|
3202
3625
|
if (isComponent) {
|
|
3203
3626
|
changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
|