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.
- package/dist/cli.js +571 -16
- package/dist/index.js +578 -49
- package/dist/plugin.js +614 -21
- package/dist/post-process.js +571 -16
- package/dist/preprocess.js +613 -23
- package/dist/runtime/index.js +7 -33
- package/dist/transform/attrs-for-transform.d.ts +36 -5
- 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/cli.js
CHANGED
|
@@ -2255,6 +2255,79 @@ function transformStandaloneAttrsDefinition(content) {
|
|
|
2255
2255
|
}`;
|
|
2256
2256
|
}
|
|
2257
2257
|
|
|
2258
|
+
// src/transform/reserved-keywords.ts
|
|
2259
|
+
var RESERVED_KEYWORDS = new Set([
|
|
2260
|
+
"break",
|
|
2261
|
+
"case",
|
|
2262
|
+
"catch",
|
|
2263
|
+
"continue",
|
|
2264
|
+
"debugger",
|
|
2265
|
+
"default",
|
|
2266
|
+
"delete",
|
|
2267
|
+
"do",
|
|
2268
|
+
"else",
|
|
2269
|
+
"finally",
|
|
2270
|
+
"for",
|
|
2271
|
+
"function",
|
|
2272
|
+
"if",
|
|
2273
|
+
"in",
|
|
2274
|
+
"instanceof",
|
|
2275
|
+
"new",
|
|
2276
|
+
"return",
|
|
2277
|
+
"switch",
|
|
2278
|
+
"this",
|
|
2279
|
+
"throw",
|
|
2280
|
+
"try",
|
|
2281
|
+
"typeof",
|
|
2282
|
+
"var",
|
|
2283
|
+
"void",
|
|
2284
|
+
"while",
|
|
2285
|
+
"with",
|
|
2286
|
+
"class",
|
|
2287
|
+
"const",
|
|
2288
|
+
"enum",
|
|
2289
|
+
"export",
|
|
2290
|
+
"extends",
|
|
2291
|
+
"import",
|
|
2292
|
+
"super",
|
|
2293
|
+
"implements",
|
|
2294
|
+
"interface",
|
|
2295
|
+
"let",
|
|
2296
|
+
"package",
|
|
2297
|
+
"private",
|
|
2298
|
+
"protected",
|
|
2299
|
+
"public",
|
|
2300
|
+
"static",
|
|
2301
|
+
"yield",
|
|
2302
|
+
"await",
|
|
2303
|
+
"async",
|
|
2304
|
+
"true",
|
|
2305
|
+
"false",
|
|
2306
|
+
"null",
|
|
2307
|
+
"undefined",
|
|
2308
|
+
"arguments",
|
|
2309
|
+
"eval"
|
|
2310
|
+
]);
|
|
2311
|
+
function getInternalVarName(propName, forcePrefix = true) {
|
|
2312
|
+
return `___${propName}`;
|
|
2313
|
+
}
|
|
2314
|
+
function generatePropDestructure(propName, defaultValue) {
|
|
2315
|
+
const internalName = getInternalVarName(propName);
|
|
2316
|
+
return `${propName}: ${internalName} = ${defaultValue}`;
|
|
2317
|
+
}
|
|
2318
|
+
function generatePropDestructureNoDefault(propName) {
|
|
2319
|
+
const internalName = getInternalVarName(propName);
|
|
2320
|
+
return `${propName}: ${internalName}`;
|
|
2321
|
+
}
|
|
2322
|
+
function generateGetter(propName) {
|
|
2323
|
+
const internalName = getInternalVarName(propName);
|
|
2324
|
+
return `get ${propName}() { return ${internalName} }`;
|
|
2325
|
+
}
|
|
2326
|
+
function generateSetter(propName) {
|
|
2327
|
+
const internalName = getInternalVarName(propName);
|
|
2328
|
+
return `set ${propName}(v) { ${internalName} = v }`;
|
|
2329
|
+
}
|
|
2330
|
+
|
|
2258
2331
|
// src/transform/schema.ts
|
|
2259
2332
|
function parseOriginSchemaFromSource(source, exportName) {
|
|
2260
2333
|
const sourceResult = parseSourceOrigin(source, exportName);
|
|
@@ -2593,14 +2666,14 @@ function generatePropsDestructuring(attrs, factoryName) {
|
|
|
2593
2666
|
for (const attr of attrs) {
|
|
2594
2667
|
if (attr.bindable) {
|
|
2595
2668
|
if (attr.hasDefault) {
|
|
2596
|
-
parts.push(
|
|
2669
|
+
parts.push(generatePropDestructure(attr.key, `$bindable(${attr.defaultValue})`));
|
|
2597
2670
|
} else {
|
|
2598
|
-
parts.push(
|
|
2671
|
+
parts.push(generatePropDestructure(attr.key, "$bindable()"));
|
|
2599
2672
|
}
|
|
2600
2673
|
} else if (attr.hasDefault) {
|
|
2601
|
-
parts.push(
|
|
2674
|
+
parts.push(generatePropDestructure(attr.key, attr.defaultValue));
|
|
2602
2675
|
} else {
|
|
2603
|
-
parts.push(attr.key);
|
|
2676
|
+
parts.push(generatePropDestructureNoDefault(attr.key));
|
|
2604
2677
|
}
|
|
2605
2678
|
}
|
|
2606
2679
|
return `let { ${parts.join(", ")} }: $attrs.Of<typeof ${factoryName}> = $props()`;
|
|
@@ -2608,9 +2681,9 @@ function generatePropsDestructuring(attrs, factoryName) {
|
|
|
2608
2681
|
function generateFactoryCallFromAttrs(factoryName, attrs) {
|
|
2609
2682
|
const parts = [];
|
|
2610
2683
|
for (const attr of attrs) {
|
|
2611
|
-
parts.push(
|
|
2684
|
+
parts.push(generateGetter(attr.key));
|
|
2612
2685
|
if (attr.bindable) {
|
|
2613
|
-
parts.push(
|
|
2686
|
+
parts.push(generateSetter(attr.key));
|
|
2614
2687
|
}
|
|
2615
2688
|
}
|
|
2616
2689
|
return `${factoryName}({ ${parts.join(", ")} })`;
|
|
@@ -2744,7 +2817,10 @@ function transformAttrsOriginCallsSync(s, source, neededImports) {
|
|
|
2744
2817
|
let firstFactoryExpr = null;
|
|
2745
2818
|
let propsTypeDeclaration = null;
|
|
2746
2819
|
let isFirstDeclaration = true;
|
|
2747
|
-
const
|
|
2820
|
+
const originInstances = new Map;
|
|
2821
|
+
const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
2822
|
+
const hasExistingProps = !!existingPropsMatch;
|
|
2823
|
+
const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source) && !hasExistingProps;
|
|
2748
2824
|
for (const decl of declarations) {
|
|
2749
2825
|
const { varName, startIndex, macroOpenParen } = decl;
|
|
2750
2826
|
const closeParenIndex = findMatchingBracket(source, macroOpenParen);
|
|
@@ -2765,20 +2841,47 @@ function transformAttrsOriginCallsSync(s, source, neededImports) {
|
|
|
2765
2841
|
expansionLines.push(`type $$Props = $attrs.Of<${typeExpr}>`);
|
|
2766
2842
|
propsTypeDeclaration = "";
|
|
2767
2843
|
}
|
|
2768
|
-
|
|
2844
|
+
if (hasExistingProps) {
|
|
2845
|
+
const existingContent = existingPropsMatch[1];
|
|
2846
|
+
const hasRestSpread = /\.\.\.\s*(\w+)\s*$/.test(existingContent);
|
|
2847
|
+
if (!hasRestSpread && isFirstDeclaration) {
|
|
2848
|
+
const restVarName = "___originAttrs";
|
|
2849
|
+
const newContent = existingContent.trimEnd() ? `${existingContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
2850
|
+
const braceEnd = source.indexOf("}", existingPropsMatch.index + 5);
|
|
2851
|
+
if (braceEnd !== -1) {
|
|
2852
|
+
s.overwrite(existingPropsMatch.index + 5, braceEnd, ` ${newContent} `);
|
|
2853
|
+
}
|
|
2854
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVarName}))`);
|
|
2855
|
+
} else {
|
|
2856
|
+
const restMatch = existingContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
2857
|
+
const restVar = restMatch ? restMatch[1] : "___originAttrs";
|
|
2858
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVar}))`);
|
|
2859
|
+
}
|
|
2860
|
+
} else {
|
|
2861
|
+
expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
|
|
2862
|
+
}
|
|
2769
2863
|
s.overwrite(startIndex, endIndex, expansionLines.join(`
|
|
2770
2864
|
`));
|
|
2865
|
+
originInstances.set(varName, {
|
|
2866
|
+
varName,
|
|
2867
|
+
factoryExpr,
|
|
2868
|
+
startPos: startIndex,
|
|
2869
|
+
endPos: endIndex
|
|
2870
|
+
});
|
|
2771
2871
|
neededImports.add("__attrsFor");
|
|
2772
2872
|
changed = true;
|
|
2773
2873
|
isFirstDeclaration = false;
|
|
2774
2874
|
}
|
|
2775
|
-
return { changed, propsTypeDeclaration };
|
|
2875
|
+
return { changed, propsTypeDeclaration, originInstances };
|
|
2776
2876
|
}
|
|
2777
2877
|
async function transformAttrsOriginCalls(s, source, neededImports, schemaResolver) {
|
|
2778
2878
|
const declarations = findVariableDeclarationsWithMacro(source, "$attrs.origin");
|
|
2779
2879
|
let changed = false;
|
|
2780
2880
|
let propsTypeDeclaration = null;
|
|
2781
|
-
const
|
|
2881
|
+
const originInstances = new Map;
|
|
2882
|
+
const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
2883
|
+
const hasExistingProps = !!existingPropsMatch;
|
|
2884
|
+
const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source) && !hasExistingProps;
|
|
2782
2885
|
const matches = [];
|
|
2783
2886
|
for (const decl of declarations) {
|
|
2784
2887
|
const { varName, startIndex, macroOpenParen } = decl;
|
|
@@ -2798,6 +2901,7 @@ async function transformAttrsOriginCalls(s, source, neededImports, schemaResolve
|
|
|
2798
2901
|
}
|
|
2799
2902
|
let firstFactoryExpr = null;
|
|
2800
2903
|
let isFirstDeclaration = true;
|
|
2904
|
+
let restVarInjected = false;
|
|
2801
2905
|
for (const { varName, factoryExpr, startIndex, endIndex, isGeneric } of matches) {
|
|
2802
2906
|
const expansionLines = [];
|
|
2803
2907
|
let usesFallback = true;
|
|
@@ -2825,15 +2929,40 @@ async function transformAttrsOriginCalls(s, source, neededImports, schemaResolve
|
|
|
2825
2929
|
}
|
|
2826
2930
|
if (usesFallback) {
|
|
2827
2931
|
const normalizedFactoryCall = normalizeFactoryCall(factoryExpr);
|
|
2828
|
-
|
|
2932
|
+
if (hasExistingProps) {
|
|
2933
|
+
const existingContent = existingPropsMatch[1];
|
|
2934
|
+
const hasRestSpread = /\.\.\.\s*(\w+)\s*$/.test(existingContent);
|
|
2935
|
+
if (!hasRestSpread && !restVarInjected) {
|
|
2936
|
+
const restVarName = "___originAttrs";
|
|
2937
|
+
const newContent = existingContent.trimEnd() ? `${existingContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
2938
|
+
const braceEnd = source.indexOf("}", existingPropsMatch.index + 5);
|
|
2939
|
+
if (braceEnd !== -1) {
|
|
2940
|
+
s.overwrite(existingPropsMatch.index + 5, braceEnd, ` ${newContent} `);
|
|
2941
|
+
restVarInjected = true;
|
|
2942
|
+
}
|
|
2943
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVarName}))`);
|
|
2944
|
+
} else {
|
|
2945
|
+
const restMatch = existingContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
2946
|
+
const restVar = restMatch ? restMatch[1] : "___originAttrs";
|
|
2947
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVar}))`);
|
|
2948
|
+
}
|
|
2949
|
+
} else {
|
|
2950
|
+
expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
|
|
2951
|
+
}
|
|
2829
2952
|
neededImports.add("__attrsFor");
|
|
2830
2953
|
}
|
|
2831
2954
|
s.overwrite(startIndex, endIndex, expansionLines.join(`
|
|
2832
2955
|
`));
|
|
2956
|
+
originInstances.set(varName, {
|
|
2957
|
+
varName,
|
|
2958
|
+
factoryExpr,
|
|
2959
|
+
startPos: startIndex,
|
|
2960
|
+
endPos: endIndex
|
|
2961
|
+
});
|
|
2833
2962
|
changed = true;
|
|
2834
2963
|
isFirstDeclaration = false;
|
|
2835
2964
|
}
|
|
2836
|
-
return { changed, propsTypeDeclaration };
|
|
2965
|
+
return { changed, propsTypeDeclaration, originInstances };
|
|
2837
2966
|
}
|
|
2838
2967
|
|
|
2839
2968
|
// src/transform/element-types.ts
|
|
@@ -2842,7 +2971,34 @@ function getElementTypeImport(element) {
|
|
|
2842
2971
|
}
|
|
2843
2972
|
|
|
2844
2973
|
// src/transform/attrs-for-transform.ts
|
|
2845
|
-
function
|
|
2974
|
+
function generateReactiveAttrsWrapper(attrs) {
|
|
2975
|
+
const parts = [];
|
|
2976
|
+
for (const attr of attrs) {
|
|
2977
|
+
parts.push(generateGetter(attr.key));
|
|
2978
|
+
if (attr.bindable) {
|
|
2979
|
+
parts.push(generateSetter(attr.key));
|
|
2980
|
+
}
|
|
2981
|
+
}
|
|
2982
|
+
return `{ ${parts.join(", ")} }`;
|
|
2983
|
+
}
|
|
2984
|
+
function generateAttrsForMerge(attrs) {
|
|
2985
|
+
const parts = [];
|
|
2986
|
+
for (const attr of attrs) {
|
|
2987
|
+
if (attr.bindable) {
|
|
2988
|
+
if (attr.hasDefault) {
|
|
2989
|
+
parts.push(generatePropDestructure(attr.key, `$bindable(${attr.defaultValue})`));
|
|
2990
|
+
} else {
|
|
2991
|
+
parts.push(generatePropDestructure(attr.key, "$bindable()"));
|
|
2992
|
+
}
|
|
2993
|
+
} else if (attr.hasDefault) {
|
|
2994
|
+
parts.push(generatePropDestructure(attr.key, attr.defaultValue));
|
|
2995
|
+
} else {
|
|
2996
|
+
parts.push(generatePropDestructureNoDefault(attr.key));
|
|
2997
|
+
}
|
|
2998
|
+
}
|
|
2999
|
+
return parts.join(", ");
|
|
3000
|
+
}
|
|
3001
|
+
function transformAttrsForCallsSync(s, source, neededImports) {
|
|
2846
3002
|
let changed = false;
|
|
2847
3003
|
const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
2848
3004
|
let restVarName = null;
|
|
@@ -2908,6 +3064,396 @@ function transformAttrsForCalls(s, source, neededImports) {
|
|
|
2908
3064
|
}
|
|
2909
3065
|
return changed;
|
|
2910
3066
|
}
|
|
3067
|
+
async function transformAttrsForCalls(s, source, neededImports, schemaResolver) {
|
|
3068
|
+
let changed = false;
|
|
3069
|
+
const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
3070
|
+
let restVarName = null;
|
|
3071
|
+
let existingPropsStart = -1;
|
|
3072
|
+
let existingDestructureContent = "";
|
|
3073
|
+
if (existingPropsMatch) {
|
|
3074
|
+
existingPropsStart = existingPropsMatch.index;
|
|
3075
|
+
existingDestructureContent = existingPropsMatch[1];
|
|
3076
|
+
const restMatch = existingDestructureContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
3077
|
+
if (restMatch) {
|
|
3078
|
+
restVarName = restMatch[1];
|
|
3079
|
+
}
|
|
3080
|
+
}
|
|
3081
|
+
const declarations = findVariableDeclarationsWithMacro(source, "$attrs.for");
|
|
3082
|
+
for (const decl of declarations) {
|
|
3083
|
+
const { varName, startIndex, macroOpenParen } = decl;
|
|
3084
|
+
const closeParenIndex = findMatchingBracket(source, macroOpenParen);
|
|
3085
|
+
if (closeParenIndex === -1)
|
|
3086
|
+
continue;
|
|
3087
|
+
const arg = source.slice(macroOpenParen + 1, closeParenIndex).trim();
|
|
3088
|
+
const endIndex = closeParenIndex + 1;
|
|
3089
|
+
const stringMatch = arg.match(/^['"](\w+)['"]$/);
|
|
3090
|
+
if (stringMatch) {
|
|
3091
|
+
const element = stringMatch[1];
|
|
3092
|
+
const typeImport = getElementTypeImport(element);
|
|
3093
|
+
if (existingPropsMatch && existingPropsStart !== -1) {
|
|
3094
|
+
if (!restVarName) {
|
|
3095
|
+
restVarName = "___restAttrs";
|
|
3096
|
+
const newDestructure = existingDestructureContent.trimEnd() ? `${existingDestructureContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
3097
|
+
const braceEnd = source.indexOf("}", existingPropsStart + 5);
|
|
3098
|
+
if (braceEnd !== -1) {
|
|
3099
|
+
s.overwrite(existingPropsStart + 5, braceEnd, ` ${newDestructure} `);
|
|
3100
|
+
}
|
|
3101
|
+
}
|
|
3102
|
+
s.overwrite(startIndex, endIndex, `let ${varName} = ${restVarName} as ${typeImport}`);
|
|
3103
|
+
} else {
|
|
3104
|
+
const expansion = `let { ...${varName} } = $props<${typeImport}>()`;
|
|
3105
|
+
s.overwrite(startIndex, endIndex, expansion);
|
|
3106
|
+
}
|
|
3107
|
+
} else {
|
|
3108
|
+
const factoryName = arg;
|
|
3109
|
+
let transformed = false;
|
|
3110
|
+
if (schemaResolver) {
|
|
3111
|
+
const importPath = findImportPath(source, factoryName);
|
|
3112
|
+
if (importPath) {
|
|
3113
|
+
const schema = await schemaResolver.resolve(importPath, factoryName);
|
|
3114
|
+
if (schema && schema.attrs.length > 0) {
|
|
3115
|
+
if (existingPropsMatch && existingPropsStart !== -1) {
|
|
3116
|
+
const additionalAttrs = generateAttrsForMerge(schema.attrs);
|
|
3117
|
+
const restMatch = existingDestructureContent.match(/,?\s*\.\.\.\s*(\w+)\s*$/);
|
|
3118
|
+
let baseContent = existingDestructureContent;
|
|
3119
|
+
let restPart = "";
|
|
3120
|
+
if (restMatch) {
|
|
3121
|
+
restPart = restMatch[0];
|
|
3122
|
+
baseContent = existingDestructureContent.slice(0, restMatch.index);
|
|
3123
|
+
}
|
|
3124
|
+
const mergedDestructure = baseContent.trimEnd() ? `${baseContent.trimEnd()}, ${additionalAttrs}${restPart}` : `${additionalAttrs}${restPart}`;
|
|
3125
|
+
const braceEnd = source.indexOf("}", existingPropsStart + 5);
|
|
3126
|
+
if (braceEnd !== -1) {
|
|
3127
|
+
s.overwrite(existingPropsStart + 5, braceEnd, ` ${mergedDestructure} `);
|
|
3128
|
+
}
|
|
3129
|
+
const wrapper = generateReactiveAttrsWrapper(schema.attrs);
|
|
3130
|
+
s.overwrite(startIndex, endIndex, `let ${varName} = __attrsFor(${factoryName}, ${wrapper})`);
|
|
3131
|
+
transformed = true;
|
|
3132
|
+
} else {
|
|
3133
|
+
const propsDestructure = generatePropsDestructuring(schema.attrs, factoryName);
|
|
3134
|
+
const wrapper = generateReactiveAttrsWrapper(schema.attrs);
|
|
3135
|
+
const expansion = [
|
|
3136
|
+
propsDestructure,
|
|
3137
|
+
`let ${varName} = __attrsFor(${factoryName}, ${wrapper})`
|
|
3138
|
+
].join(`
|
|
3139
|
+
`);
|
|
3140
|
+
s.overwrite(startIndex, endIndex, expansion);
|
|
3141
|
+
transformed = true;
|
|
3142
|
+
}
|
|
3143
|
+
}
|
|
3144
|
+
}
|
|
3145
|
+
}
|
|
3146
|
+
if (!transformed) {
|
|
3147
|
+
if (existingPropsMatch && existingPropsStart !== -1) {
|
|
3148
|
+
if (!restVarName) {
|
|
3149
|
+
restVarName = "___restAttrs";
|
|
3150
|
+
const newDestructure = existingDestructureContent.trimEnd() ? `${existingDestructureContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
3151
|
+
const braceEnd = source.indexOf("}", existingPropsStart + 5);
|
|
3152
|
+
if (braceEnd !== -1) {
|
|
3153
|
+
s.overwrite(existingPropsStart + 5, braceEnd, ` ${newDestructure} `);
|
|
3154
|
+
}
|
|
3155
|
+
}
|
|
3156
|
+
s.overwrite(startIndex, endIndex, `let ${varName} = __attrsFor(${factoryName}, ${restVarName})`);
|
|
3157
|
+
} else {
|
|
3158
|
+
const expansion = [
|
|
3159
|
+
`let ___attrs: $attrs.Of<typeof ${factoryName}> = $props()`,
|
|
3160
|
+
`let ${varName} = __attrsFor(${factoryName}, ___attrs)`
|
|
3161
|
+
].join(`
|
|
3162
|
+
`);
|
|
3163
|
+
s.overwrite(startIndex, endIndex, expansion);
|
|
3164
|
+
}
|
|
3165
|
+
}
|
|
3166
|
+
neededImports.add("__attrsFor");
|
|
3167
|
+
}
|
|
3168
|
+
changed = true;
|
|
3169
|
+
}
|
|
3170
|
+
return changed;
|
|
3171
|
+
}
|
|
3172
|
+
|
|
3173
|
+
// src/transform/origin-destructure-transform.ts
|
|
3174
|
+
function transformOriginDestructuring(s, source, originInstances) {
|
|
3175
|
+
let changed = false;
|
|
3176
|
+
const destructures = findOriginDestructures(source, originInstances);
|
|
3177
|
+
for (const d of destructures.reverse()) {
|
|
3178
|
+
const replacement = generateDestructureReplacement(d, originInstances);
|
|
3179
|
+
if (replacement) {
|
|
3180
|
+
s.overwrite(d.startPos, d.endPos, replacement);
|
|
3181
|
+
changed = true;
|
|
3182
|
+
}
|
|
3183
|
+
}
|
|
3184
|
+
return changed;
|
|
3185
|
+
}
|
|
3186
|
+
function findOriginDestructures(source, originInstances) {
|
|
3187
|
+
const results = [];
|
|
3188
|
+
const destructurePattern = /\b(let|const|var)\s*\{/g;
|
|
3189
|
+
let match;
|
|
3190
|
+
while ((match = destructurePattern.exec(source)) !== null) {
|
|
3191
|
+
if (isInsideStringOrComment(source, match.index))
|
|
3192
|
+
continue;
|
|
3193
|
+
const declKeyword = match[1];
|
|
3194
|
+
const braceStart = match.index + match[0].length - 1;
|
|
3195
|
+
const braceEnd = findMatchingBracket(source, braceStart, "{", "}");
|
|
3196
|
+
if (braceEnd === -1)
|
|
3197
|
+
continue;
|
|
3198
|
+
let i = braceEnd + 1;
|
|
3199
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3200
|
+
i++;
|
|
3201
|
+
if (source[i] !== "=")
|
|
3202
|
+
continue;
|
|
3203
|
+
i++;
|
|
3204
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3205
|
+
i++;
|
|
3206
|
+
const exprStart = i;
|
|
3207
|
+
let sourceVar = "";
|
|
3208
|
+
while (i < source.length && /[\w$]/.test(source[i])) {
|
|
3209
|
+
sourceVar += source[i];
|
|
3210
|
+
i++;
|
|
3211
|
+
}
|
|
3212
|
+
if (!sourceVar)
|
|
3213
|
+
continue;
|
|
3214
|
+
if (!originInstances.has(sourceVar))
|
|
3215
|
+
continue;
|
|
3216
|
+
let isPropsAccess = false;
|
|
3217
|
+
const afterVarPos = i;
|
|
3218
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3219
|
+
i++;
|
|
3220
|
+
if (source[i] === ".") {
|
|
3221
|
+
i++;
|
|
3222
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3223
|
+
i++;
|
|
3224
|
+
const propName = readIdentifier(source, i);
|
|
3225
|
+
if (propName === "props") {
|
|
3226
|
+
isPropsAccess = true;
|
|
3227
|
+
i += propName.length;
|
|
3228
|
+
} else {
|
|
3229
|
+
continue;
|
|
3230
|
+
}
|
|
3231
|
+
}
|
|
3232
|
+
let endPos = i;
|
|
3233
|
+
while (endPos < source.length && /\s/.test(source[endPos]) && source[endPos] !== `
|
|
3234
|
+
`)
|
|
3235
|
+
endPos++;
|
|
3236
|
+
if (source[endPos] === ";")
|
|
3237
|
+
endPos++;
|
|
3238
|
+
if (source[endPos] === `
|
|
3239
|
+
`)
|
|
3240
|
+
endPos++;
|
|
3241
|
+
const patternContent = source.slice(braceStart + 1, braceEnd);
|
|
3242
|
+
const parsed = parseDestructurePattern(patternContent, isPropsAccess);
|
|
3243
|
+
results.push({
|
|
3244
|
+
startPos: match.index,
|
|
3245
|
+
endPos,
|
|
3246
|
+
sourceVar,
|
|
3247
|
+
isPropsAccess,
|
|
3248
|
+
methods: parsed.methods,
|
|
3249
|
+
props: parsed.props,
|
|
3250
|
+
nestedPropsPattern: parsed.nestedProps
|
|
3251
|
+
});
|
|
3252
|
+
}
|
|
3253
|
+
return results;
|
|
3254
|
+
}
|
|
3255
|
+
function parseDestructurePattern(content, isPropsAccess) {
|
|
3256
|
+
const methods = [];
|
|
3257
|
+
const props = [];
|
|
3258
|
+
let nestedProps = null;
|
|
3259
|
+
const parts = splitByTopLevelCommas2(content);
|
|
3260
|
+
for (const part of parts) {
|
|
3261
|
+
const trimmed = part.trim();
|
|
3262
|
+
if (!trimmed)
|
|
3263
|
+
continue;
|
|
3264
|
+
const propsMatch = trimmed.match(/^props\s*:\s*\{/);
|
|
3265
|
+
if (propsMatch) {
|
|
3266
|
+
const braceStart = trimmed.indexOf("{");
|
|
3267
|
+
const braceEnd = findMatchingBracket(trimmed, braceStart, "{", "}");
|
|
3268
|
+
if (braceEnd !== -1) {
|
|
3269
|
+
const nestedContent = trimmed.slice(braceStart + 1, braceEnd);
|
|
3270
|
+
const nestedParts = splitByTopLevelCommas2(nestedContent);
|
|
3271
|
+
const nestedPropsList = [];
|
|
3272
|
+
for (const np of nestedParts) {
|
|
3273
|
+
const parsed = parseDestructuredProp(np.trim());
|
|
3274
|
+
if (parsed)
|
|
3275
|
+
nestedPropsList.push(parsed);
|
|
3276
|
+
}
|
|
3277
|
+
nestedProps = {
|
|
3278
|
+
startPos: 0,
|
|
3279
|
+
endPos: 0,
|
|
3280
|
+
props: nestedPropsList
|
|
3281
|
+
};
|
|
3282
|
+
}
|
|
3283
|
+
continue;
|
|
3284
|
+
}
|
|
3285
|
+
if (isPropsAccess) {
|
|
3286
|
+
const parsed = parseDestructuredProp(trimmed);
|
|
3287
|
+
if (parsed)
|
|
3288
|
+
props.push(parsed);
|
|
3289
|
+
} else {
|
|
3290
|
+
const parsed = parseDestructuredItem(trimmed);
|
|
3291
|
+
if (parsed)
|
|
3292
|
+
methods.push(parsed);
|
|
3293
|
+
}
|
|
3294
|
+
}
|
|
3295
|
+
return { methods, props, nestedProps };
|
|
3296
|
+
}
|
|
3297
|
+
function parseDestructuredItem(part) {
|
|
3298
|
+
const trimmed = part.trim();
|
|
3299
|
+
if (!trimmed)
|
|
3300
|
+
return null;
|
|
3301
|
+
const colonIdx = findTopLevelColon(trimmed);
|
|
3302
|
+
let key;
|
|
3303
|
+
let alias = null;
|
|
3304
|
+
let rest = trimmed;
|
|
3305
|
+
if (colonIdx !== -1) {
|
|
3306
|
+
key = trimmed.slice(0, colonIdx).trim();
|
|
3307
|
+
rest = trimmed.slice(colonIdx + 1).trim();
|
|
3308
|
+
const eqIdx2 = findTopLevelEquals(rest);
|
|
3309
|
+
if (eqIdx2 !== -1) {
|
|
3310
|
+
alias = rest.slice(0, eqIdx2).trim();
|
|
3311
|
+
return {
|
|
3312
|
+
key,
|
|
3313
|
+
alias,
|
|
3314
|
+
defaultValue: rest.slice(eqIdx2 + 1).trim()
|
|
3315
|
+
};
|
|
3316
|
+
} else {
|
|
3317
|
+
alias = rest;
|
|
3318
|
+
return { key, alias, defaultValue: null };
|
|
3319
|
+
}
|
|
3320
|
+
}
|
|
3321
|
+
const eqIdx = findTopLevelEquals(trimmed);
|
|
3322
|
+
if (eqIdx !== -1) {
|
|
3323
|
+
key = trimmed.slice(0, eqIdx).trim();
|
|
3324
|
+
return {
|
|
3325
|
+
key,
|
|
3326
|
+
alias: null,
|
|
3327
|
+
defaultValue: trimmed.slice(eqIdx + 1).trim()
|
|
3328
|
+
};
|
|
3329
|
+
}
|
|
3330
|
+
return { key: trimmed, alias: null, defaultValue: null };
|
|
3331
|
+
}
|
|
3332
|
+
function parseDestructuredProp(part) {
|
|
3333
|
+
const item = parseDestructuredItem(part);
|
|
3334
|
+
if (!item)
|
|
3335
|
+
return null;
|
|
3336
|
+
let isBindable = false;
|
|
3337
|
+
let bindableDefault = null;
|
|
3338
|
+
if (item.defaultValue) {
|
|
3339
|
+
const bindableMatch = item.defaultValue.match(/^\$bindable\s*\(/);
|
|
3340
|
+
if (bindableMatch) {
|
|
3341
|
+
isBindable = true;
|
|
3342
|
+
const parenStart = item.defaultValue.indexOf("(");
|
|
3343
|
+
const parenEnd = findMatchingBracket(item.defaultValue, parenStart, "(", ")");
|
|
3344
|
+
if (parenEnd !== -1) {
|
|
3345
|
+
bindableDefault = item.defaultValue.slice(parenStart + 1, parenEnd).trim() || null;
|
|
3346
|
+
}
|
|
3347
|
+
}
|
|
3348
|
+
}
|
|
3349
|
+
return {
|
|
3350
|
+
...item,
|
|
3351
|
+
isBindable,
|
|
3352
|
+
bindableDefault
|
|
3353
|
+
};
|
|
3354
|
+
}
|
|
3355
|
+
function generateDestructureReplacement(d, originInstances) {
|
|
3356
|
+
const lines = [];
|
|
3357
|
+
const instance = originInstances.get(d.sourceVar);
|
|
3358
|
+
if (!instance)
|
|
3359
|
+
return null;
|
|
3360
|
+
for (const m of d.methods) {
|
|
3361
|
+
const varName = m.alias || m.key;
|
|
3362
|
+
if (m.defaultValue) {
|
|
3363
|
+
lines.push(`let ${varName} = ${d.sourceVar}.${m.key}?.bind(${d.sourceVar}) ?? ${m.defaultValue}`);
|
|
3364
|
+
} else {
|
|
3365
|
+
lines.push(`let ${varName} = ${d.sourceVar}.${m.key}.bind(${d.sourceVar})`);
|
|
3366
|
+
}
|
|
3367
|
+
}
|
|
3368
|
+
if (d.nestedPropsPattern) {
|
|
3369
|
+
for (const p of d.nestedPropsPattern.props) {
|
|
3370
|
+
lines.push(...generatePropAccessors(p, d.sourceVar));
|
|
3371
|
+
}
|
|
3372
|
+
}
|
|
3373
|
+
if (d.isPropsAccess) {
|
|
3374
|
+
for (const p of d.props) {
|
|
3375
|
+
lines.push(...generatePropAccessors(p, d.sourceVar));
|
|
3376
|
+
}
|
|
3377
|
+
}
|
|
3378
|
+
return lines.join(`
|
|
3379
|
+
`) + `
|
|
3380
|
+
`;
|
|
3381
|
+
}
|
|
3382
|
+
function generatePropAccessors(p, sourceVar) {
|
|
3383
|
+
const lines = [];
|
|
3384
|
+
const varName = p.alias || p.key;
|
|
3385
|
+
if (p.isBindable) {
|
|
3386
|
+
const defaultVal = p.bindableDefault || "undefined";
|
|
3387
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key} ?? ${defaultVal})`);
|
|
3388
|
+
} else if (p.defaultValue) {
|
|
3389
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key} ?? ${p.defaultValue})`);
|
|
3390
|
+
} else {
|
|
3391
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key})`);
|
|
3392
|
+
}
|
|
3393
|
+
return lines;
|
|
3394
|
+
}
|
|
3395
|
+
function splitByTopLevelCommas2(content) {
|
|
3396
|
+
const parts = [];
|
|
3397
|
+
let current = "";
|
|
3398
|
+
let depth = 0;
|
|
3399
|
+
for (let i = 0;i < content.length; i++) {
|
|
3400
|
+
const char = content[i];
|
|
3401
|
+
if (char === "{" || char === "(" || char === "[" || char === "<") {
|
|
3402
|
+
depth++;
|
|
3403
|
+
current += char;
|
|
3404
|
+
} else if (char === "}" || char === ")" || char === "]" || char === ">") {
|
|
3405
|
+
depth--;
|
|
3406
|
+
current += char;
|
|
3407
|
+
} else if (char === "," && depth === 0) {
|
|
3408
|
+
parts.push(current);
|
|
3409
|
+
current = "";
|
|
3410
|
+
} else {
|
|
3411
|
+
current += char;
|
|
3412
|
+
}
|
|
3413
|
+
}
|
|
3414
|
+
if (current.trim()) {
|
|
3415
|
+
parts.push(current);
|
|
3416
|
+
}
|
|
3417
|
+
return parts;
|
|
3418
|
+
}
|
|
3419
|
+
function findTopLevelColon(str) {
|
|
3420
|
+
let depth = 0;
|
|
3421
|
+
for (let i = 0;i < str.length; i++) {
|
|
3422
|
+
const char = str[i];
|
|
3423
|
+
if (char === "{" || char === "(" || char === "[" || char === "<")
|
|
3424
|
+
depth++;
|
|
3425
|
+
else if (char === "}" || char === ")" || char === "]" || char === ">")
|
|
3426
|
+
depth--;
|
|
3427
|
+
else if (char === ":" && depth === 0)
|
|
3428
|
+
return i;
|
|
3429
|
+
}
|
|
3430
|
+
return -1;
|
|
3431
|
+
}
|
|
3432
|
+
function findTopLevelEquals(str) {
|
|
3433
|
+
let depth = 0;
|
|
3434
|
+
for (let i = 0;i < str.length; i++) {
|
|
3435
|
+
const char = str[i];
|
|
3436
|
+
if (char === "{" || char === "(" || char === "[" || char === "<")
|
|
3437
|
+
depth++;
|
|
3438
|
+
else if (char === "}" || char === ")" || char === "]" || char === ">")
|
|
3439
|
+
depth--;
|
|
3440
|
+
else if (char === "=" && depth === 0) {
|
|
3441
|
+
if (str[i + 1] !== "=" && str[i + 1] !== ">") {
|
|
3442
|
+
return i;
|
|
3443
|
+
}
|
|
3444
|
+
}
|
|
3445
|
+
}
|
|
3446
|
+
return -1;
|
|
3447
|
+
}
|
|
3448
|
+
function readIdentifier(str, start) {
|
|
3449
|
+
let result = "";
|
|
3450
|
+
let i = start;
|
|
3451
|
+
while (i < str.length && /[\w$]/.test(str[i])) {
|
|
3452
|
+
result += str[i];
|
|
3453
|
+
i++;
|
|
3454
|
+
}
|
|
3455
|
+
return result;
|
|
3456
|
+
}
|
|
2911
3457
|
|
|
2912
3458
|
// src/transform/core.ts
|
|
2913
3459
|
function transformScript(source, options = {}) {
|
|
@@ -2931,9 +3477,12 @@ function transformScript(source, options = {}) {
|
|
|
2931
3477
|
const result = transformAttrsOriginCallsSync(s, source, neededImports);
|
|
2932
3478
|
changed = result.changed || changed;
|
|
2933
3479
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3480
|
+
if (result.originInstances.size > 0) {
|
|
3481
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3482
|
+
}
|
|
2934
3483
|
}
|
|
2935
3484
|
if (isComponent) {
|
|
2936
|
-
changed =
|
|
3485
|
+
changed = transformAttrsForCallsSync(s, source, neededImports) || changed;
|
|
2937
3486
|
}
|
|
2938
3487
|
const needsPropsInjection = propsTypeDeclaration && propsTypeDeclaration.length > 0;
|
|
2939
3488
|
if (isComponent && (neededImports.size > 0 || needsPropsInjection)) {
|
|
@@ -3002,9 +3551,12 @@ async function transformScriptAsync(source, options) {
|
|
|
3002
3551
|
const result = await transformAttrsOriginCalls(s, source, neededImports, schemaResolver);
|
|
3003
3552
|
changed = result.changed || changed;
|
|
3004
3553
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3554
|
+
if (result.originInstances.size > 0) {
|
|
3555
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3556
|
+
}
|
|
3005
3557
|
}
|
|
3006
3558
|
if (isComponent) {
|
|
3007
|
-
changed = transformAttrsForCalls(s, source, neededImports) || changed;
|
|
3559
|
+
changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
|
|
3008
3560
|
}
|
|
3009
3561
|
const needsPropsInjectionAsync = propsTypeDeclaration && propsTypeDeclaration.length > 0;
|
|
3010
3562
|
if (isComponent && (neededImports.size > 0 || needsPropsInjectionAsync)) {
|
|
@@ -3072,9 +3624,12 @@ async function transformScriptContent(source, options) {
|
|
|
3072
3624
|
const result = await transformAttrsOriginCalls(s, source, neededImports, schemaResolver);
|
|
3073
3625
|
changed = result.changed || changed;
|
|
3074
3626
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3627
|
+
if (result.originInstances.size > 0) {
|
|
3628
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3629
|
+
}
|
|
3075
3630
|
}
|
|
3076
3631
|
if (isComponent) {
|
|
3077
|
-
changed = transformAttrsForCalls(s, source, neededImports) || changed;
|
|
3632
|
+
changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
|
|
3078
3633
|
}
|
|
3079
3634
|
if (neededImports.size > 0) {
|
|
3080
3635
|
const importStatement = generateRuntimeImport([...neededImports]);
|