svelte-origin 1.0.0-next.21 → 1.0.0-next.23
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 +233 -6
- package/dist/index.js +240 -39
- package/dist/plugin.js +354 -6
- package/dist/post-process.js +233 -6
- package/dist/preprocess.js +356 -6
- package/dist/runtime/index.js +7 -33
- package/dist/transform/attrs-for-transform.d.ts +36 -5
- package/dist/transform/schema.d.ts +17 -0
- package/dist/vite-dts.js +77 -1
- package/package.json +1 -1
package/dist/post-process.js
CHANGED
|
@@ -1973,6 +1973,7 @@ function transformOriginDefinition(content, svelteImports) {
|
|
|
1973
1973
|
let attrsContent = "";
|
|
1974
1974
|
let contentWithoutAttrs = bodyContent;
|
|
1975
1975
|
let attrPropertyName = "props";
|
|
1976
|
+
let propsRef = null;
|
|
1976
1977
|
if (attrsMatch) {
|
|
1977
1978
|
attrPropertyName = attrsMatch[1];
|
|
1978
1979
|
const attrsStart = attrsMatch.index;
|
|
@@ -2003,13 +2004,31 @@ function transformOriginDefinition(content, svelteImports) {
|
|
|
2003
2004
|
}
|
|
2004
2005
|
contentWithoutAttrs = bodyContent.slice(0, attrsStart) + bodyContent.slice(cutEnd);
|
|
2005
2006
|
}
|
|
2007
|
+
} else {
|
|
2008
|
+
const externalRefMatch = bodyContent.match(/(\w+)\s*:\s*(\w+)\s*,/);
|
|
2009
|
+
if (externalRefMatch) {
|
|
2010
|
+
const propName = externalRefMatch[1];
|
|
2011
|
+
const refName = externalRefMatch[2];
|
|
2012
|
+
if ((propName === "props" || propName === "attrs") && /^[A-Z]/.test(refName) && !["$state", "$derived", "$effect", "$bindable"].includes(refName)) {
|
|
2013
|
+
attrPropertyName = propName;
|
|
2014
|
+
propsRef = refName;
|
|
2015
|
+
const refStart = externalRefMatch.index;
|
|
2016
|
+
const refEnd = refStart + externalRefMatch[0].length;
|
|
2017
|
+
contentWithoutAttrs = bodyContent.slice(0, refStart) + bodyContent.slice(refEnd);
|
|
2018
|
+
}
|
|
2019
|
+
}
|
|
2006
2020
|
}
|
|
2007
2021
|
const attrDetails = parseAttrsSource(attrsContent);
|
|
2008
2022
|
const { body: createFnBody, attachments } = transformOriginBody(contentWithoutAttrs.trim(), parents, attrDetails, attrPropertyName, svelteImports);
|
|
2009
2023
|
const parentsCode = parents.length > 0 ? `[${parents.join(", ")}]` : "undefined";
|
|
2010
2024
|
let configCode = `{
|
|
2011
2025
|
__attrSchema: ${attrSchemaCode},
|
|
2012
|
-
__parents: ${parentsCode}
|
|
2026
|
+
__parents: ${parentsCode},`;
|
|
2027
|
+
if (propsRef) {
|
|
2028
|
+
configCode += `
|
|
2029
|
+
__propsRef: ${propsRef},`;
|
|
2030
|
+
}
|
|
2031
|
+
configCode += `
|
|
2013
2032
|
__create: (__inputAttrs${parents.length > 0 ? ", __super" : ""}) => {
|
|
2014
2033
|
${createFnBody}
|
|
2015
2034
|
}`;
|
|
@@ -2244,6 +2263,73 @@ function parseOriginSchemaFromSource(source, exportName) {
|
|
|
2244
2263
|
return compiledResult;
|
|
2245
2264
|
return null;
|
|
2246
2265
|
}
|
|
2266
|
+
function parseAttrsSchemaFromSource(source, exportName) {
|
|
2267
|
+
const sourceResult = parseSourceAttrs(source, exportName);
|
|
2268
|
+
if (sourceResult)
|
|
2269
|
+
return sourceResult;
|
|
2270
|
+
const compiledResult = parseCompiledAttrs(source, exportName);
|
|
2271
|
+
if (compiledResult)
|
|
2272
|
+
return compiledResult;
|
|
2273
|
+
return null;
|
|
2274
|
+
}
|
|
2275
|
+
function parseSourceAttrs(source, exportName) {
|
|
2276
|
+
const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*\\$attrs\\s*\\(`, "s");
|
|
2277
|
+
const match = exportPattern.exec(source);
|
|
2278
|
+
if (!match)
|
|
2279
|
+
return null;
|
|
2280
|
+
const attrsStart = match.index + match[0].length - 1;
|
|
2281
|
+
const attrsEnd = findMatchingBracket(source, attrsStart);
|
|
2282
|
+
if (attrsEnd === -1)
|
|
2283
|
+
return null;
|
|
2284
|
+
let attrsContent = source.slice(attrsStart + 1, attrsEnd).trim();
|
|
2285
|
+
let parentNames = [];
|
|
2286
|
+
if (attrsContent.startsWith("[")) {
|
|
2287
|
+
const closeBracket = findMatchingBracket(attrsContent, 0, "[", "]");
|
|
2288
|
+
if (closeBracket !== -1) {
|
|
2289
|
+
const parentsContent = attrsContent.slice(1, closeBracket);
|
|
2290
|
+
parentNames = parentsContent.split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
|
|
2291
|
+
let i = closeBracket + 1;
|
|
2292
|
+
while (i < attrsContent.length && /[\s,]/.test(attrsContent[i]))
|
|
2293
|
+
i++;
|
|
2294
|
+
attrsContent = attrsContent.slice(i);
|
|
2295
|
+
}
|
|
2296
|
+
}
|
|
2297
|
+
if (!attrsContent.startsWith("{")) {
|
|
2298
|
+
return { attrs: [], parentNames };
|
|
2299
|
+
}
|
|
2300
|
+
const objEnd = findMatchingBracket(attrsContent, 0, "{", "}");
|
|
2301
|
+
if (objEnd === -1)
|
|
2302
|
+
return { attrs: [], parentNames };
|
|
2303
|
+
const objContent = attrsContent.slice(1, objEnd);
|
|
2304
|
+
const attrs = parseAttrsContent(objContent);
|
|
2305
|
+
return { attrs, parentNames };
|
|
2306
|
+
}
|
|
2307
|
+
function parseCompiledAttrs(source, exportName) {
|
|
2308
|
+
const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*__createAttrs\\s*\\(\\s*\\{`, "s");
|
|
2309
|
+
const match = exportPattern.exec(source);
|
|
2310
|
+
if (!match)
|
|
2311
|
+
return null;
|
|
2312
|
+
const braceStart = match.index + match[0].length - 1;
|
|
2313
|
+
const braceEnd = findMatchingBracket(source, braceStart, "{", "}");
|
|
2314
|
+
if (braceEnd === -1)
|
|
2315
|
+
return null;
|
|
2316
|
+
const configContent = source.slice(braceStart + 1, braceEnd);
|
|
2317
|
+
const schemaMatch = configContent.match(/__attrSchema\s*:\s*\{/);
|
|
2318
|
+
if (!schemaMatch)
|
|
2319
|
+
return { attrs: [], parentNames: [] };
|
|
2320
|
+
const schemaStart = schemaMatch.index + schemaMatch[0].length - 1;
|
|
2321
|
+
const schemaEnd = findMatchingBracket(configContent, schemaStart, "{", "}");
|
|
2322
|
+
if (schemaEnd === -1)
|
|
2323
|
+
return { attrs: [], parentNames: [] };
|
|
2324
|
+
const schemaContent = configContent.slice(schemaStart + 1, schemaEnd);
|
|
2325
|
+
const attrs = parseCompiledAttrSchema(schemaContent);
|
|
2326
|
+
const parentsMatch = configContent.match(/__parents\s*:\s*\[([^\]]*)\]/);
|
|
2327
|
+
let parentNames = [];
|
|
2328
|
+
if (parentsMatch && parentsMatch[1].trim()) {
|
|
2329
|
+
parentNames = parentsMatch[1].split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
|
|
2330
|
+
}
|
|
2331
|
+
return { attrs, parentNames };
|
|
2332
|
+
}
|
|
2247
2333
|
function parseSourceOrigin(source, exportName) {
|
|
2248
2334
|
const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*\\$origin\\s*\\(`, "s");
|
|
2249
2335
|
const match = exportPattern.exec(source);
|
|
@@ -2269,6 +2355,13 @@ function parseSourceOrigin(source, exportName) {
|
|
|
2269
2355
|
}
|
|
2270
2356
|
const attrsMatch = bodyContent.match(/(\w+)\s*:\s*\$attrs\s*\(\s*\{/);
|
|
2271
2357
|
if (!attrsMatch) {
|
|
2358
|
+
const externalRefMatch = bodyContent.match(/(?:props|attrs)\s*:\s*([A-Z]\w*)\b/);
|
|
2359
|
+
if (externalRefMatch) {
|
|
2360
|
+
const propsRef = externalRefMatch[1];
|
|
2361
|
+
if (!["Object", "Array", "String", "Number", "Boolean"].includes(propsRef)) {
|
|
2362
|
+
return { attrs: [], parentNames, propsRef };
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2272
2365
|
return { attrs: [], parentNames };
|
|
2273
2366
|
}
|
|
2274
2367
|
const attrsStart = attrsMatch.index;
|
|
@@ -2305,7 +2398,9 @@ function parseCompiledOrigin(source, exportName) {
|
|
|
2305
2398
|
if (parentsMatch && parentsMatch[1].trim()) {
|
|
2306
2399
|
parentNames = parentsMatch[1].split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
|
|
2307
2400
|
}
|
|
2308
|
-
|
|
2401
|
+
const propsRefMatch = configContent.match(/__propsRef\s*:\s*(\w+)/);
|
|
2402
|
+
const propsRef = propsRefMatch ? propsRefMatch[1] : undefined;
|
|
2403
|
+
return { attrs, parentNames, propsRef };
|
|
2309
2404
|
}
|
|
2310
2405
|
function parseCompiledAttrSchema(content) {
|
|
2311
2406
|
const result = [];
|
|
@@ -2745,7 +2840,34 @@ function getElementTypeImport(element) {
|
|
|
2745
2840
|
}
|
|
2746
2841
|
|
|
2747
2842
|
// src/transform/attrs-for-transform.ts
|
|
2748
|
-
function
|
|
2843
|
+
function generateReactiveAttrsWrapper(attrs) {
|
|
2844
|
+
const parts = [];
|
|
2845
|
+
for (const attr of attrs) {
|
|
2846
|
+
parts.push(`get ${attr.key}() { return ${attr.key} }`);
|
|
2847
|
+
if (attr.bindable) {
|
|
2848
|
+
parts.push(`set ${attr.key}(v) { ${attr.key} = v }`);
|
|
2849
|
+
}
|
|
2850
|
+
}
|
|
2851
|
+
return `{ ${parts.join(", ")} }`;
|
|
2852
|
+
}
|
|
2853
|
+
function generateAttrsForMerge(attrs) {
|
|
2854
|
+
const parts = [];
|
|
2855
|
+
for (const attr of attrs) {
|
|
2856
|
+
if (attr.bindable) {
|
|
2857
|
+
if (attr.hasDefault) {
|
|
2858
|
+
parts.push(`${attr.key} = $bindable(${attr.defaultValue})`);
|
|
2859
|
+
} else {
|
|
2860
|
+
parts.push(`${attr.key} = $bindable()`);
|
|
2861
|
+
}
|
|
2862
|
+
} else if (attr.hasDefault) {
|
|
2863
|
+
parts.push(`${attr.key} = ${attr.defaultValue}`);
|
|
2864
|
+
} else {
|
|
2865
|
+
parts.push(attr.key);
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
return parts.join(", ");
|
|
2869
|
+
}
|
|
2870
|
+
function transformAttrsForCallsSync(s, source, neededImports) {
|
|
2749
2871
|
let changed = false;
|
|
2750
2872
|
const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
2751
2873
|
let restVarName = null;
|
|
@@ -2811,6 +2933,111 @@ function transformAttrsForCalls(s, source, neededImports) {
|
|
|
2811
2933
|
}
|
|
2812
2934
|
return changed;
|
|
2813
2935
|
}
|
|
2936
|
+
async function transformAttrsForCalls(s, source, neededImports, schemaResolver) {
|
|
2937
|
+
let changed = false;
|
|
2938
|
+
const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
2939
|
+
let restVarName = null;
|
|
2940
|
+
let existingPropsStart = -1;
|
|
2941
|
+
let existingDestructureContent = "";
|
|
2942
|
+
if (existingPropsMatch) {
|
|
2943
|
+
existingPropsStart = existingPropsMatch.index;
|
|
2944
|
+
existingDestructureContent = existingPropsMatch[1];
|
|
2945
|
+
const restMatch = existingDestructureContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
2946
|
+
if (restMatch) {
|
|
2947
|
+
restVarName = restMatch[1];
|
|
2948
|
+
}
|
|
2949
|
+
}
|
|
2950
|
+
const declarations = findVariableDeclarationsWithMacro(source, "$attrs.for");
|
|
2951
|
+
for (const decl of declarations) {
|
|
2952
|
+
const { varName, startIndex, macroOpenParen } = decl;
|
|
2953
|
+
const closeParenIndex = findMatchingBracket(source, macroOpenParen);
|
|
2954
|
+
if (closeParenIndex === -1)
|
|
2955
|
+
continue;
|
|
2956
|
+
const arg = source.slice(macroOpenParen + 1, closeParenIndex).trim();
|
|
2957
|
+
const endIndex = closeParenIndex + 1;
|
|
2958
|
+
const stringMatch = arg.match(/^['"](\w+)['"]$/);
|
|
2959
|
+
if (stringMatch) {
|
|
2960
|
+
const element = stringMatch[1];
|
|
2961
|
+
const typeImport = getElementTypeImport(element);
|
|
2962
|
+
if (existingPropsMatch && existingPropsStart !== -1) {
|
|
2963
|
+
if (!restVarName) {
|
|
2964
|
+
restVarName = "___restAttrs";
|
|
2965
|
+
const newDestructure = existingDestructureContent.trimEnd() ? `${existingDestructureContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
2966
|
+
const braceEnd = source.indexOf("}", existingPropsStart + 5);
|
|
2967
|
+
if (braceEnd !== -1) {
|
|
2968
|
+
s.overwrite(existingPropsStart + 5, braceEnd, ` ${newDestructure} `);
|
|
2969
|
+
}
|
|
2970
|
+
}
|
|
2971
|
+
s.overwrite(startIndex, endIndex, `let ${varName} = ${restVarName} as ${typeImport}`);
|
|
2972
|
+
} else {
|
|
2973
|
+
const expansion = `let { ...${varName} } = $props<${typeImport}>()`;
|
|
2974
|
+
s.overwrite(startIndex, endIndex, expansion);
|
|
2975
|
+
}
|
|
2976
|
+
} else {
|
|
2977
|
+
const factoryName = arg;
|
|
2978
|
+
let transformed = false;
|
|
2979
|
+
if (schemaResolver) {
|
|
2980
|
+
const importPath = findImportPath(source, factoryName);
|
|
2981
|
+
if (importPath) {
|
|
2982
|
+
const schema = await schemaResolver.resolve(importPath, factoryName);
|
|
2983
|
+
if (schema && schema.attrs.length > 0) {
|
|
2984
|
+
if (existingPropsMatch && existingPropsStart !== -1) {
|
|
2985
|
+
const additionalAttrs = generateAttrsForMerge(schema.attrs);
|
|
2986
|
+
const restMatch = existingDestructureContent.match(/,?\s*\.\.\.\s*(\w+)\s*$/);
|
|
2987
|
+
let baseContent = existingDestructureContent;
|
|
2988
|
+
let restPart = "";
|
|
2989
|
+
if (restMatch) {
|
|
2990
|
+
restPart = restMatch[0];
|
|
2991
|
+
baseContent = existingDestructureContent.slice(0, restMatch.index);
|
|
2992
|
+
}
|
|
2993
|
+
const mergedDestructure = baseContent.trimEnd() ? `${baseContent.trimEnd()}, ${additionalAttrs}${restPart}` : `${additionalAttrs}${restPart}`;
|
|
2994
|
+
const braceEnd = source.indexOf("}", existingPropsStart + 5);
|
|
2995
|
+
if (braceEnd !== -1) {
|
|
2996
|
+
s.overwrite(existingPropsStart + 5, braceEnd, ` ${mergedDestructure} `);
|
|
2997
|
+
}
|
|
2998
|
+
const wrapper = generateReactiveAttrsWrapper(schema.attrs);
|
|
2999
|
+
s.overwrite(startIndex, endIndex, `let ${varName} = __attrsFor(${factoryName}, ${wrapper})`);
|
|
3000
|
+
transformed = true;
|
|
3001
|
+
} else {
|
|
3002
|
+
const propsDestructure = generatePropsDestructuring(schema.attrs, factoryName);
|
|
3003
|
+
const wrapper = generateReactiveAttrsWrapper(schema.attrs);
|
|
3004
|
+
const expansion = [
|
|
3005
|
+
propsDestructure,
|
|
3006
|
+
`let ${varName} = __attrsFor(${factoryName}, ${wrapper})`
|
|
3007
|
+
].join(`
|
|
3008
|
+
`);
|
|
3009
|
+
s.overwrite(startIndex, endIndex, expansion);
|
|
3010
|
+
transformed = true;
|
|
3011
|
+
}
|
|
3012
|
+
}
|
|
3013
|
+
}
|
|
3014
|
+
}
|
|
3015
|
+
if (!transformed) {
|
|
3016
|
+
if (existingPropsMatch && existingPropsStart !== -1) {
|
|
3017
|
+
if (!restVarName) {
|
|
3018
|
+
restVarName = "___restAttrs";
|
|
3019
|
+
const newDestructure = existingDestructureContent.trimEnd() ? `${existingDestructureContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
3020
|
+
const braceEnd = source.indexOf("}", existingPropsStart + 5);
|
|
3021
|
+
if (braceEnd !== -1) {
|
|
3022
|
+
s.overwrite(existingPropsStart + 5, braceEnd, ` ${newDestructure} `);
|
|
3023
|
+
}
|
|
3024
|
+
}
|
|
3025
|
+
s.overwrite(startIndex, endIndex, `let ${varName} = __attrsFor(${factoryName}, ${restVarName})`);
|
|
3026
|
+
} else {
|
|
3027
|
+
const expansion = [
|
|
3028
|
+
`let ___attrs: $attrs.Of<typeof ${factoryName}> = $props()`,
|
|
3029
|
+
`let ${varName} = __attrsFor(${factoryName}, ___attrs)`
|
|
3030
|
+
].join(`
|
|
3031
|
+
`);
|
|
3032
|
+
s.overwrite(startIndex, endIndex, expansion);
|
|
3033
|
+
}
|
|
3034
|
+
}
|
|
3035
|
+
neededImports.add("__attrsFor");
|
|
3036
|
+
}
|
|
3037
|
+
changed = true;
|
|
3038
|
+
}
|
|
3039
|
+
return changed;
|
|
3040
|
+
}
|
|
2814
3041
|
|
|
2815
3042
|
// src/transform/core.ts
|
|
2816
3043
|
function transformScript(source, options = {}) {
|
|
@@ -2836,7 +3063,7 @@ function transformScript(source, options = {}) {
|
|
|
2836
3063
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
2837
3064
|
}
|
|
2838
3065
|
if (isComponent) {
|
|
2839
|
-
changed =
|
|
3066
|
+
changed = transformAttrsForCallsSync(s, source, neededImports) || changed;
|
|
2840
3067
|
}
|
|
2841
3068
|
const needsPropsInjection = propsTypeDeclaration && propsTypeDeclaration.length > 0;
|
|
2842
3069
|
if (isComponent && (neededImports.size > 0 || needsPropsInjection)) {
|
|
@@ -2907,7 +3134,7 @@ async function transformScriptAsync(source, options) {
|
|
|
2907
3134
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
2908
3135
|
}
|
|
2909
3136
|
if (isComponent) {
|
|
2910
|
-
changed = transformAttrsForCalls(s, source, neededImports) || changed;
|
|
3137
|
+
changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
|
|
2911
3138
|
}
|
|
2912
3139
|
const needsPropsInjectionAsync = propsTypeDeclaration && propsTypeDeclaration.length > 0;
|
|
2913
3140
|
if (isComponent && (neededImports.size > 0 || needsPropsInjectionAsync)) {
|
|
@@ -2977,7 +3204,7 @@ async function transformScriptContent(source, options) {
|
|
|
2977
3204
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
2978
3205
|
}
|
|
2979
3206
|
if (isComponent) {
|
|
2980
|
-
changed = transformAttrsForCalls(s, source, neededImports) || changed;
|
|
3207
|
+
changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
|
|
2981
3208
|
}
|
|
2982
3209
|
if (neededImports.size > 0) {
|
|
2983
3210
|
const importStatement = generateRuntimeImport([...neededImports]);
|