svelte-origin 1.0.0-next.23 → 1.0.0-next.25
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/LLM.md +96 -4
- package/README.md +61 -7
- package/dist/cli.js +578 -37
- package/dist/index.js +578 -37
- package/dist/plugin.js +578 -37
- package/dist/post-process.js +578 -37
- package/dist/preprocess.js +578 -37
- 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 +130 -6
- package/package.json +1 -1
package/dist/plugin.js
CHANGED
|
@@ -1545,11 +1545,93 @@ function findPropsInjectionPosition(source) {
|
|
|
1545
1545
|
}
|
|
1546
1546
|
|
|
1547
1547
|
// src/transform/attrs-schema.ts
|
|
1548
|
+
function stripLeadingComments(str) {
|
|
1549
|
+
let result = str.trim();
|
|
1550
|
+
while (true) {
|
|
1551
|
+
if (result.startsWith("/*")) {
|
|
1552
|
+
const endComment = result.indexOf("*/");
|
|
1553
|
+
if (endComment !== -1) {
|
|
1554
|
+
result = result.slice(endComment + 2).trim();
|
|
1555
|
+
continue;
|
|
1556
|
+
}
|
|
1557
|
+
break;
|
|
1558
|
+
}
|
|
1559
|
+
if (result.startsWith("//")) {
|
|
1560
|
+
const endLine = result.indexOf(`
|
|
1561
|
+
`);
|
|
1562
|
+
if (endLine !== -1) {
|
|
1563
|
+
result = result.slice(endLine + 1).trim();
|
|
1564
|
+
continue;
|
|
1565
|
+
}
|
|
1566
|
+
result = "";
|
|
1567
|
+
break;
|
|
1568
|
+
}
|
|
1569
|
+
break;
|
|
1570
|
+
}
|
|
1571
|
+
return result;
|
|
1572
|
+
}
|
|
1573
|
+
function stripTrailingComments(str) {
|
|
1574
|
+
let result = str.trim();
|
|
1575
|
+
let i = 0;
|
|
1576
|
+
let lastValidEnd = result.length;
|
|
1577
|
+
while (i < result.length) {
|
|
1578
|
+
const char = result[i];
|
|
1579
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
1580
|
+
const quote = char;
|
|
1581
|
+
i++;
|
|
1582
|
+
while (i < result.length && result[i] !== quote) {
|
|
1583
|
+
if (result[i] === "\\")
|
|
1584
|
+
i++;
|
|
1585
|
+
i++;
|
|
1586
|
+
}
|
|
1587
|
+
i++;
|
|
1588
|
+
lastValidEnd = i;
|
|
1589
|
+
continue;
|
|
1590
|
+
}
|
|
1591
|
+
if (char === "{" || char === "(" || char === "[") {
|
|
1592
|
+
let depth = 1;
|
|
1593
|
+
i++;
|
|
1594
|
+
while (i < result.length && depth > 0) {
|
|
1595
|
+
const c = result[i];
|
|
1596
|
+
if (c === "{" || c === "(" || c === "[")
|
|
1597
|
+
depth++;
|
|
1598
|
+
else if (c === "}" || c === ")" || c === "]")
|
|
1599
|
+
depth--;
|
|
1600
|
+
else if (c === '"' || c === "'" || c === "`") {
|
|
1601
|
+
const quote = c;
|
|
1602
|
+
i++;
|
|
1603
|
+
while (i < result.length && result[i] !== quote) {
|
|
1604
|
+
if (result[i] === "\\")
|
|
1605
|
+
i++;
|
|
1606
|
+
i++;
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
i++;
|
|
1610
|
+
}
|
|
1611
|
+
lastValidEnd = i;
|
|
1612
|
+
continue;
|
|
1613
|
+
}
|
|
1614
|
+
if (char === "/" && result[i + 1] === "*") {
|
|
1615
|
+
result = result.slice(0, i).trim();
|
|
1616
|
+
break;
|
|
1617
|
+
}
|
|
1618
|
+
if (char === "/" && result[i + 1] === "/") {
|
|
1619
|
+
result = result.slice(0, i).trim();
|
|
1620
|
+
break;
|
|
1621
|
+
}
|
|
1622
|
+
i++;
|
|
1623
|
+
lastValidEnd = i;
|
|
1624
|
+
}
|
|
1625
|
+
return result;
|
|
1626
|
+
}
|
|
1548
1627
|
function generateAttrSchemaFromSource(attrsSource) {
|
|
1549
1628
|
const entries = [];
|
|
1550
1629
|
const attrs = splitAttrsSource(attrsSource);
|
|
1551
1630
|
for (const attr of attrs) {
|
|
1552
|
-
|
|
1631
|
+
let trimmed = attr.trim();
|
|
1632
|
+
if (!trimmed)
|
|
1633
|
+
continue;
|
|
1634
|
+
trimmed = stripLeadingComments(trimmed);
|
|
1553
1635
|
if (!trimmed)
|
|
1554
1636
|
continue;
|
|
1555
1637
|
const colonIndex = trimmed.indexOf(":");
|
|
@@ -1557,6 +1639,7 @@ function generateAttrSchemaFromSource(attrsSource) {
|
|
|
1557
1639
|
continue;
|
|
1558
1640
|
const key = trimmed.slice(0, colonIndex).trim();
|
|
1559
1641
|
let value = trimmed.slice(colonIndex + 1).trim();
|
|
1642
|
+
value = stripTrailingComments(value);
|
|
1560
1643
|
const asIndex = findTopLevelAs(value);
|
|
1561
1644
|
if (asIndex !== -1) {
|
|
1562
1645
|
value = value.slice(0, asIndex).trim();
|
|
@@ -1580,24 +1663,7 @@ function parseAttrsSource(attrsSource) {
|
|
|
1580
1663
|
let trimmed = attr.trim();
|
|
1581
1664
|
if (!trimmed)
|
|
1582
1665
|
continue;
|
|
1583
|
-
|
|
1584
|
-
const endComment = trimmed.indexOf("*/");
|
|
1585
|
-
if (endComment !== -1) {
|
|
1586
|
-
trimmed = trimmed.slice(endComment + 2).trim();
|
|
1587
|
-
} else {
|
|
1588
|
-
break;
|
|
1589
|
-
}
|
|
1590
|
-
}
|
|
1591
|
-
while (trimmed.startsWith("//")) {
|
|
1592
|
-
const endLine = trimmed.indexOf(`
|
|
1593
|
-
`);
|
|
1594
|
-
if (endLine !== -1) {
|
|
1595
|
-
trimmed = trimmed.slice(endLine + 1).trim();
|
|
1596
|
-
} else {
|
|
1597
|
-
trimmed = "";
|
|
1598
|
-
break;
|
|
1599
|
-
}
|
|
1600
|
-
}
|
|
1666
|
+
trimmed = stripLeadingComments(trimmed);
|
|
1601
1667
|
if (!trimmed)
|
|
1602
1668
|
continue;
|
|
1603
1669
|
const colonIndex = trimmed.indexOf(":");
|
|
@@ -1605,6 +1671,7 @@ function parseAttrsSource(attrsSource) {
|
|
|
1605
1671
|
continue;
|
|
1606
1672
|
const key = trimmed.slice(0, colonIndex).trim();
|
|
1607
1673
|
let value = trimmed.slice(colonIndex + 1).trim();
|
|
1674
|
+
value = stripTrailingComments(value);
|
|
1608
1675
|
const asIndex = findTopLevelAs(value);
|
|
1609
1676
|
if (asIndex !== -1) {
|
|
1610
1677
|
value = value.slice(0, asIndex).trim();
|
|
@@ -2249,6 +2316,79 @@ function transformStandaloneAttrsDefinition(content) {
|
|
|
2249
2316
|
}`;
|
|
2250
2317
|
}
|
|
2251
2318
|
|
|
2319
|
+
// src/transform/reserved-keywords.ts
|
|
2320
|
+
var RESERVED_KEYWORDS = new Set([
|
|
2321
|
+
"break",
|
|
2322
|
+
"case",
|
|
2323
|
+
"catch",
|
|
2324
|
+
"continue",
|
|
2325
|
+
"debugger",
|
|
2326
|
+
"default",
|
|
2327
|
+
"delete",
|
|
2328
|
+
"do",
|
|
2329
|
+
"else",
|
|
2330
|
+
"finally",
|
|
2331
|
+
"for",
|
|
2332
|
+
"function",
|
|
2333
|
+
"if",
|
|
2334
|
+
"in",
|
|
2335
|
+
"instanceof",
|
|
2336
|
+
"new",
|
|
2337
|
+
"return",
|
|
2338
|
+
"switch",
|
|
2339
|
+
"this",
|
|
2340
|
+
"throw",
|
|
2341
|
+
"try",
|
|
2342
|
+
"typeof",
|
|
2343
|
+
"var",
|
|
2344
|
+
"void",
|
|
2345
|
+
"while",
|
|
2346
|
+
"with",
|
|
2347
|
+
"class",
|
|
2348
|
+
"const",
|
|
2349
|
+
"enum",
|
|
2350
|
+
"export",
|
|
2351
|
+
"extends",
|
|
2352
|
+
"import",
|
|
2353
|
+
"super",
|
|
2354
|
+
"implements",
|
|
2355
|
+
"interface",
|
|
2356
|
+
"let",
|
|
2357
|
+
"package",
|
|
2358
|
+
"private",
|
|
2359
|
+
"protected",
|
|
2360
|
+
"public",
|
|
2361
|
+
"static",
|
|
2362
|
+
"yield",
|
|
2363
|
+
"await",
|
|
2364
|
+
"async",
|
|
2365
|
+
"true",
|
|
2366
|
+
"false",
|
|
2367
|
+
"null",
|
|
2368
|
+
"undefined",
|
|
2369
|
+
"arguments",
|
|
2370
|
+
"eval"
|
|
2371
|
+
]);
|
|
2372
|
+
function getInternalVarName(propName, forcePrefix = true) {
|
|
2373
|
+
return `___${propName}`;
|
|
2374
|
+
}
|
|
2375
|
+
function generatePropDestructure(propName, defaultValue) {
|
|
2376
|
+
const internalName = getInternalVarName(propName);
|
|
2377
|
+
return `${propName}: ${internalName} = ${defaultValue}`;
|
|
2378
|
+
}
|
|
2379
|
+
function generatePropDestructureNoDefault(propName) {
|
|
2380
|
+
const internalName = getInternalVarName(propName);
|
|
2381
|
+
return `${propName}: ${internalName}`;
|
|
2382
|
+
}
|
|
2383
|
+
function generateGetter(propName) {
|
|
2384
|
+
const internalName = getInternalVarName(propName);
|
|
2385
|
+
return `get ${propName}() { return ${internalName} }`;
|
|
2386
|
+
}
|
|
2387
|
+
function generateSetter(propName) {
|
|
2388
|
+
const internalName = getInternalVarName(propName);
|
|
2389
|
+
return `set ${propName}(v) { ${internalName} = v }`;
|
|
2390
|
+
}
|
|
2391
|
+
|
|
2252
2392
|
// src/transform/schema.ts
|
|
2253
2393
|
function parseOriginSchemaFromSource(source, exportName) {
|
|
2254
2394
|
const sourceResult = parseSourceOrigin(source, exportName);
|
|
@@ -2475,6 +2615,7 @@ function parseAttrsContent(content) {
|
|
|
2475
2615
|
continue;
|
|
2476
2616
|
const key = trimmed.slice(0, colonIndex).trim();
|
|
2477
2617
|
let value = trimmed.slice(colonIndex + 1).trim();
|
|
2618
|
+
value = stripTrailingComments2(value);
|
|
2478
2619
|
const asIndex = findTopLevelAs(value);
|
|
2479
2620
|
if (asIndex !== -1) {
|
|
2480
2621
|
value = value.slice(0, asIndex).trim();
|
|
@@ -2582,19 +2723,69 @@ function stripComments(str) {
|
|
|
2582
2723
|
}
|
|
2583
2724
|
return result;
|
|
2584
2725
|
}
|
|
2726
|
+
function stripTrailingComments2(str) {
|
|
2727
|
+
let result = str.trim();
|
|
2728
|
+
let i = 0;
|
|
2729
|
+
while (i < result.length) {
|
|
2730
|
+
const char = result[i];
|
|
2731
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
2732
|
+
const quote = char;
|
|
2733
|
+
i++;
|
|
2734
|
+
while (i < result.length && result[i] !== quote) {
|
|
2735
|
+
if (result[i] === "\\")
|
|
2736
|
+
i++;
|
|
2737
|
+
i++;
|
|
2738
|
+
}
|
|
2739
|
+
i++;
|
|
2740
|
+
continue;
|
|
2741
|
+
}
|
|
2742
|
+
if (char === "{" || char === "(" || char === "[") {
|
|
2743
|
+
let depth = 1;
|
|
2744
|
+
i++;
|
|
2745
|
+
while (i < result.length && depth > 0) {
|
|
2746
|
+
const c = result[i];
|
|
2747
|
+
if (c === "{" || c === "(" || c === "[")
|
|
2748
|
+
depth++;
|
|
2749
|
+
else if (c === "}" || c === ")" || c === "]")
|
|
2750
|
+
depth--;
|
|
2751
|
+
else if (c === '"' || c === "'" || c === "`") {
|
|
2752
|
+
const quote = c;
|
|
2753
|
+
i++;
|
|
2754
|
+
while (i < result.length && result[i] !== quote) {
|
|
2755
|
+
if (result[i] === "\\")
|
|
2756
|
+
i++;
|
|
2757
|
+
i++;
|
|
2758
|
+
}
|
|
2759
|
+
}
|
|
2760
|
+
i++;
|
|
2761
|
+
}
|
|
2762
|
+
continue;
|
|
2763
|
+
}
|
|
2764
|
+
if (char === "/" && result[i + 1] === "*") {
|
|
2765
|
+
result = result.slice(0, i).trim();
|
|
2766
|
+
break;
|
|
2767
|
+
}
|
|
2768
|
+
if (char === "/" && result[i + 1] === "/") {
|
|
2769
|
+
result = result.slice(0, i).trim();
|
|
2770
|
+
break;
|
|
2771
|
+
}
|
|
2772
|
+
i++;
|
|
2773
|
+
}
|
|
2774
|
+
return result;
|
|
2775
|
+
}
|
|
2585
2776
|
function generatePropsDestructuring(attrs, factoryName) {
|
|
2586
2777
|
const parts = [];
|
|
2587
2778
|
for (const attr of attrs) {
|
|
2588
2779
|
if (attr.bindable) {
|
|
2589
2780
|
if (attr.hasDefault) {
|
|
2590
|
-
parts.push(
|
|
2781
|
+
parts.push(generatePropDestructure(attr.key, `$bindable(${attr.defaultValue})`));
|
|
2591
2782
|
} else {
|
|
2592
|
-
parts.push(
|
|
2783
|
+
parts.push(generatePropDestructure(attr.key, "$bindable()"));
|
|
2593
2784
|
}
|
|
2594
2785
|
} else if (attr.hasDefault) {
|
|
2595
|
-
parts.push(
|
|
2786
|
+
parts.push(generatePropDestructure(attr.key, attr.defaultValue));
|
|
2596
2787
|
} else {
|
|
2597
|
-
parts.push(attr.key);
|
|
2788
|
+
parts.push(generatePropDestructureNoDefault(attr.key));
|
|
2598
2789
|
}
|
|
2599
2790
|
}
|
|
2600
2791
|
return `let { ${parts.join(", ")} }: $attrs.Of<typeof ${factoryName}> = $props()`;
|
|
@@ -2602,9 +2793,9 @@ function generatePropsDestructuring(attrs, factoryName) {
|
|
|
2602
2793
|
function generateFactoryCallFromAttrs(factoryName, attrs) {
|
|
2603
2794
|
const parts = [];
|
|
2604
2795
|
for (const attr of attrs) {
|
|
2605
|
-
parts.push(
|
|
2796
|
+
parts.push(generateGetter(attr.key));
|
|
2606
2797
|
if (attr.bindable) {
|
|
2607
|
-
parts.push(
|
|
2798
|
+
parts.push(generateSetter(attr.key));
|
|
2608
2799
|
}
|
|
2609
2800
|
}
|
|
2610
2801
|
return `${factoryName}({ ${parts.join(", ")} })`;
|
|
@@ -2738,7 +2929,10 @@ function transformAttrsOriginCallsSync(s, source, neededImports) {
|
|
|
2738
2929
|
let firstFactoryExpr = null;
|
|
2739
2930
|
let propsTypeDeclaration = null;
|
|
2740
2931
|
let isFirstDeclaration = true;
|
|
2741
|
-
const
|
|
2932
|
+
const originInstances = new Map;
|
|
2933
|
+
const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
2934
|
+
const hasExistingProps = !!existingPropsMatch;
|
|
2935
|
+
const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source) && !hasExistingProps;
|
|
2742
2936
|
for (const decl of declarations) {
|
|
2743
2937
|
const { varName, startIndex, macroOpenParen } = decl;
|
|
2744
2938
|
const closeParenIndex = findMatchingBracket(source, macroOpenParen);
|
|
@@ -2759,20 +2953,47 @@ function transformAttrsOriginCallsSync(s, source, neededImports) {
|
|
|
2759
2953
|
expansionLines.push(`type $$Props = $attrs.Of<${typeExpr}>`);
|
|
2760
2954
|
propsTypeDeclaration = "";
|
|
2761
2955
|
}
|
|
2762
|
-
|
|
2956
|
+
if (hasExistingProps) {
|
|
2957
|
+
const existingContent = existingPropsMatch[1];
|
|
2958
|
+
const hasRestSpread = /\.\.\.\s*(\w+)\s*$/.test(existingContent);
|
|
2959
|
+
if (!hasRestSpread && isFirstDeclaration) {
|
|
2960
|
+
const restVarName = "___originAttrs";
|
|
2961
|
+
const newContent = existingContent.trimEnd() ? `${existingContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
2962
|
+
const braceEnd = source.indexOf("}", existingPropsMatch.index + 5);
|
|
2963
|
+
if (braceEnd !== -1) {
|
|
2964
|
+
s.overwrite(existingPropsMatch.index + 5, braceEnd, ` ${newContent} `);
|
|
2965
|
+
}
|
|
2966
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVarName}))`);
|
|
2967
|
+
} else {
|
|
2968
|
+
const restMatch = existingContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
2969
|
+
const restVar = restMatch ? restMatch[1] : "___originAttrs";
|
|
2970
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVar}))`);
|
|
2971
|
+
}
|
|
2972
|
+
} else {
|
|
2973
|
+
expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
|
|
2974
|
+
}
|
|
2763
2975
|
s.overwrite(startIndex, endIndex, expansionLines.join(`
|
|
2764
2976
|
`));
|
|
2977
|
+
originInstances.set(varName, {
|
|
2978
|
+
varName,
|
|
2979
|
+
factoryExpr,
|
|
2980
|
+
startPos: startIndex,
|
|
2981
|
+
endPos: endIndex
|
|
2982
|
+
});
|
|
2765
2983
|
neededImports.add("__attrsFor");
|
|
2766
2984
|
changed = true;
|
|
2767
2985
|
isFirstDeclaration = false;
|
|
2768
2986
|
}
|
|
2769
|
-
return { changed, propsTypeDeclaration };
|
|
2987
|
+
return { changed, propsTypeDeclaration, originInstances };
|
|
2770
2988
|
}
|
|
2771
2989
|
async function transformAttrsOriginCalls(s, source, neededImports, schemaResolver) {
|
|
2772
2990
|
const declarations = findVariableDeclarationsWithMacro(source, "$attrs.origin");
|
|
2773
2991
|
let changed = false;
|
|
2774
2992
|
let propsTypeDeclaration = null;
|
|
2775
|
-
const
|
|
2993
|
+
const originInstances = new Map;
|
|
2994
|
+
const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
2995
|
+
const hasExistingProps = !!existingPropsMatch;
|
|
2996
|
+
const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source) && !hasExistingProps;
|
|
2776
2997
|
const matches = [];
|
|
2777
2998
|
for (const decl of declarations) {
|
|
2778
2999
|
const { varName, startIndex, macroOpenParen } = decl;
|
|
@@ -2792,6 +3013,7 @@ async function transformAttrsOriginCalls(s, source, neededImports, schemaResolve
|
|
|
2792
3013
|
}
|
|
2793
3014
|
let firstFactoryExpr = null;
|
|
2794
3015
|
let isFirstDeclaration = true;
|
|
3016
|
+
let restVarInjected = false;
|
|
2795
3017
|
for (const { varName, factoryExpr, startIndex, endIndex, isGeneric } of matches) {
|
|
2796
3018
|
const expansionLines = [];
|
|
2797
3019
|
let usesFallback = true;
|
|
@@ -2819,15 +3041,40 @@ async function transformAttrsOriginCalls(s, source, neededImports, schemaResolve
|
|
|
2819
3041
|
}
|
|
2820
3042
|
if (usesFallback) {
|
|
2821
3043
|
const normalizedFactoryCall = normalizeFactoryCall(factoryExpr);
|
|
2822
|
-
|
|
3044
|
+
if (hasExistingProps) {
|
|
3045
|
+
const existingContent = existingPropsMatch[1];
|
|
3046
|
+
const hasRestSpread = /\.\.\.\s*(\w+)\s*$/.test(existingContent);
|
|
3047
|
+
if (!hasRestSpread && !restVarInjected) {
|
|
3048
|
+
const restVarName = "___originAttrs";
|
|
3049
|
+
const newContent = existingContent.trimEnd() ? `${existingContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
3050
|
+
const braceEnd = source.indexOf("}", existingPropsMatch.index + 5);
|
|
3051
|
+
if (braceEnd !== -1) {
|
|
3052
|
+
s.overwrite(existingPropsMatch.index + 5, braceEnd, ` ${newContent} `);
|
|
3053
|
+
restVarInjected = true;
|
|
3054
|
+
}
|
|
3055
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVarName}))`);
|
|
3056
|
+
} else {
|
|
3057
|
+
const restMatch = existingContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
3058
|
+
const restVar = restMatch ? restMatch[1] : "___originAttrs";
|
|
3059
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVar}))`);
|
|
3060
|
+
}
|
|
3061
|
+
} else {
|
|
3062
|
+
expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
|
|
3063
|
+
}
|
|
2823
3064
|
neededImports.add("__attrsFor");
|
|
2824
3065
|
}
|
|
2825
3066
|
s.overwrite(startIndex, endIndex, expansionLines.join(`
|
|
2826
3067
|
`));
|
|
3068
|
+
originInstances.set(varName, {
|
|
3069
|
+
varName,
|
|
3070
|
+
factoryExpr,
|
|
3071
|
+
startPos: startIndex,
|
|
3072
|
+
endPos: endIndex
|
|
3073
|
+
});
|
|
2827
3074
|
changed = true;
|
|
2828
3075
|
isFirstDeclaration = false;
|
|
2829
3076
|
}
|
|
2830
|
-
return { changed, propsTypeDeclaration };
|
|
3077
|
+
return { changed, propsTypeDeclaration, originInstances };
|
|
2831
3078
|
}
|
|
2832
3079
|
|
|
2833
3080
|
// src/transform/element-types.ts
|
|
@@ -2839,9 +3086,9 @@ function getElementTypeImport(element) {
|
|
|
2839
3086
|
function generateReactiveAttrsWrapper(attrs) {
|
|
2840
3087
|
const parts = [];
|
|
2841
3088
|
for (const attr of attrs) {
|
|
2842
|
-
parts.push(
|
|
3089
|
+
parts.push(generateGetter(attr.key));
|
|
2843
3090
|
if (attr.bindable) {
|
|
2844
|
-
parts.push(
|
|
3091
|
+
parts.push(generateSetter(attr.key));
|
|
2845
3092
|
}
|
|
2846
3093
|
}
|
|
2847
3094
|
return `{ ${parts.join(", ")} }`;
|
|
@@ -2851,14 +3098,14 @@ function generateAttrsForMerge(attrs) {
|
|
|
2851
3098
|
for (const attr of attrs) {
|
|
2852
3099
|
if (attr.bindable) {
|
|
2853
3100
|
if (attr.hasDefault) {
|
|
2854
|
-
parts.push(
|
|
3101
|
+
parts.push(generatePropDestructure(attr.key, `$bindable(${attr.defaultValue})`));
|
|
2855
3102
|
} else {
|
|
2856
|
-
parts.push(
|
|
3103
|
+
parts.push(generatePropDestructure(attr.key, "$bindable()"));
|
|
2857
3104
|
}
|
|
2858
3105
|
} else if (attr.hasDefault) {
|
|
2859
|
-
parts.push(
|
|
3106
|
+
parts.push(generatePropDestructure(attr.key, attr.defaultValue));
|
|
2860
3107
|
} else {
|
|
2861
|
-
parts.push(attr.key);
|
|
3108
|
+
parts.push(generatePropDestructureNoDefault(attr.key));
|
|
2862
3109
|
}
|
|
2863
3110
|
}
|
|
2864
3111
|
return parts.join(", ");
|
|
@@ -3035,6 +3282,291 @@ async function transformAttrsForCalls(s, source, neededImports, schemaResolver)
|
|
|
3035
3282
|
return changed;
|
|
3036
3283
|
}
|
|
3037
3284
|
|
|
3285
|
+
// src/transform/origin-destructure-transform.ts
|
|
3286
|
+
function transformOriginDestructuring(s, source, originInstances) {
|
|
3287
|
+
let changed = false;
|
|
3288
|
+
const destructures = findOriginDestructures(source, originInstances);
|
|
3289
|
+
for (const d of destructures.reverse()) {
|
|
3290
|
+
const replacement = generateDestructureReplacement(d, originInstances);
|
|
3291
|
+
if (replacement) {
|
|
3292
|
+
s.overwrite(d.startPos, d.endPos, replacement);
|
|
3293
|
+
changed = true;
|
|
3294
|
+
}
|
|
3295
|
+
}
|
|
3296
|
+
return changed;
|
|
3297
|
+
}
|
|
3298
|
+
function findOriginDestructures(source, originInstances) {
|
|
3299
|
+
const results = [];
|
|
3300
|
+
const destructurePattern = /\b(let|const|var)\s*\{/g;
|
|
3301
|
+
let match;
|
|
3302
|
+
while ((match = destructurePattern.exec(source)) !== null) {
|
|
3303
|
+
if (isInsideStringOrComment(source, match.index))
|
|
3304
|
+
continue;
|
|
3305
|
+
const declKeyword = match[1];
|
|
3306
|
+
const braceStart = match.index + match[0].length - 1;
|
|
3307
|
+
const braceEnd = findMatchingBracket(source, braceStart, "{", "}");
|
|
3308
|
+
if (braceEnd === -1)
|
|
3309
|
+
continue;
|
|
3310
|
+
let i = braceEnd + 1;
|
|
3311
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3312
|
+
i++;
|
|
3313
|
+
if (source[i] !== "=")
|
|
3314
|
+
continue;
|
|
3315
|
+
i++;
|
|
3316
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3317
|
+
i++;
|
|
3318
|
+
const exprStart = i;
|
|
3319
|
+
let sourceVar = "";
|
|
3320
|
+
while (i < source.length && /[\w$]/.test(source[i])) {
|
|
3321
|
+
sourceVar += source[i];
|
|
3322
|
+
i++;
|
|
3323
|
+
}
|
|
3324
|
+
if (!sourceVar)
|
|
3325
|
+
continue;
|
|
3326
|
+
if (!originInstances.has(sourceVar))
|
|
3327
|
+
continue;
|
|
3328
|
+
let isPropsAccess = false;
|
|
3329
|
+
const afterVarPos = i;
|
|
3330
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3331
|
+
i++;
|
|
3332
|
+
if (source[i] === ".") {
|
|
3333
|
+
i++;
|
|
3334
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3335
|
+
i++;
|
|
3336
|
+
const propName = readIdentifier(source, i);
|
|
3337
|
+
if (propName === "props") {
|
|
3338
|
+
isPropsAccess = true;
|
|
3339
|
+
i += propName.length;
|
|
3340
|
+
} else {
|
|
3341
|
+
continue;
|
|
3342
|
+
}
|
|
3343
|
+
}
|
|
3344
|
+
let endPos = i;
|
|
3345
|
+
while (endPos < source.length && /\s/.test(source[endPos]) && source[endPos] !== `
|
|
3346
|
+
`)
|
|
3347
|
+
endPos++;
|
|
3348
|
+
if (source[endPos] === ";")
|
|
3349
|
+
endPos++;
|
|
3350
|
+
if (source[endPos] === `
|
|
3351
|
+
`)
|
|
3352
|
+
endPos++;
|
|
3353
|
+
const patternContent = source.slice(braceStart + 1, braceEnd);
|
|
3354
|
+
const parsed = parseDestructurePattern(patternContent, isPropsAccess);
|
|
3355
|
+
results.push({
|
|
3356
|
+
startPos: match.index,
|
|
3357
|
+
endPos,
|
|
3358
|
+
sourceVar,
|
|
3359
|
+
isPropsAccess,
|
|
3360
|
+
methods: parsed.methods,
|
|
3361
|
+
props: parsed.props,
|
|
3362
|
+
nestedPropsPattern: parsed.nestedProps
|
|
3363
|
+
});
|
|
3364
|
+
}
|
|
3365
|
+
return results;
|
|
3366
|
+
}
|
|
3367
|
+
function parseDestructurePattern(content, isPropsAccess) {
|
|
3368
|
+
const methods = [];
|
|
3369
|
+
const props = [];
|
|
3370
|
+
let nestedProps = null;
|
|
3371
|
+
const parts = splitByTopLevelCommas2(content);
|
|
3372
|
+
for (const part of parts) {
|
|
3373
|
+
const trimmed = part.trim();
|
|
3374
|
+
if (!trimmed)
|
|
3375
|
+
continue;
|
|
3376
|
+
const propsMatch = trimmed.match(/^props\s*:\s*\{/);
|
|
3377
|
+
if (propsMatch) {
|
|
3378
|
+
const braceStart = trimmed.indexOf("{");
|
|
3379
|
+
const braceEnd = findMatchingBracket(trimmed, braceStart, "{", "}");
|
|
3380
|
+
if (braceEnd !== -1) {
|
|
3381
|
+
const nestedContent = trimmed.slice(braceStart + 1, braceEnd);
|
|
3382
|
+
const nestedParts = splitByTopLevelCommas2(nestedContent);
|
|
3383
|
+
const nestedPropsList = [];
|
|
3384
|
+
for (const np of nestedParts) {
|
|
3385
|
+
const parsed = parseDestructuredProp(np.trim());
|
|
3386
|
+
if (parsed)
|
|
3387
|
+
nestedPropsList.push(parsed);
|
|
3388
|
+
}
|
|
3389
|
+
nestedProps = {
|
|
3390
|
+
startPos: 0,
|
|
3391
|
+
endPos: 0,
|
|
3392
|
+
props: nestedPropsList
|
|
3393
|
+
};
|
|
3394
|
+
}
|
|
3395
|
+
continue;
|
|
3396
|
+
}
|
|
3397
|
+
if (isPropsAccess) {
|
|
3398
|
+
const parsed = parseDestructuredProp(trimmed);
|
|
3399
|
+
if (parsed)
|
|
3400
|
+
props.push(parsed);
|
|
3401
|
+
} else {
|
|
3402
|
+
const parsed = parseDestructuredItem(trimmed);
|
|
3403
|
+
if (parsed)
|
|
3404
|
+
methods.push(parsed);
|
|
3405
|
+
}
|
|
3406
|
+
}
|
|
3407
|
+
return { methods, props, nestedProps };
|
|
3408
|
+
}
|
|
3409
|
+
function parseDestructuredItem(part) {
|
|
3410
|
+
const trimmed = part.trim();
|
|
3411
|
+
if (!trimmed)
|
|
3412
|
+
return null;
|
|
3413
|
+
const colonIdx = findTopLevelColon(trimmed);
|
|
3414
|
+
let key;
|
|
3415
|
+
let alias = null;
|
|
3416
|
+
let rest = trimmed;
|
|
3417
|
+
if (colonIdx !== -1) {
|
|
3418
|
+
key = trimmed.slice(0, colonIdx).trim();
|
|
3419
|
+
rest = trimmed.slice(colonIdx + 1).trim();
|
|
3420
|
+
const eqIdx2 = findTopLevelEquals(rest);
|
|
3421
|
+
if (eqIdx2 !== -1) {
|
|
3422
|
+
alias = rest.slice(0, eqIdx2).trim();
|
|
3423
|
+
return {
|
|
3424
|
+
key,
|
|
3425
|
+
alias,
|
|
3426
|
+
defaultValue: rest.slice(eqIdx2 + 1).trim()
|
|
3427
|
+
};
|
|
3428
|
+
} else {
|
|
3429
|
+
alias = rest;
|
|
3430
|
+
return { key, alias, defaultValue: null };
|
|
3431
|
+
}
|
|
3432
|
+
}
|
|
3433
|
+
const eqIdx = findTopLevelEquals(trimmed);
|
|
3434
|
+
if (eqIdx !== -1) {
|
|
3435
|
+
key = trimmed.slice(0, eqIdx).trim();
|
|
3436
|
+
return {
|
|
3437
|
+
key,
|
|
3438
|
+
alias: null,
|
|
3439
|
+
defaultValue: trimmed.slice(eqIdx + 1).trim()
|
|
3440
|
+
};
|
|
3441
|
+
}
|
|
3442
|
+
return { key: trimmed, alias: null, defaultValue: null };
|
|
3443
|
+
}
|
|
3444
|
+
function parseDestructuredProp(part) {
|
|
3445
|
+
const item = parseDestructuredItem(part);
|
|
3446
|
+
if (!item)
|
|
3447
|
+
return null;
|
|
3448
|
+
let isBindable = false;
|
|
3449
|
+
let bindableDefault = null;
|
|
3450
|
+
if (item.defaultValue) {
|
|
3451
|
+
const bindableMatch = item.defaultValue.match(/^\$bindable\s*\(/);
|
|
3452
|
+
if (bindableMatch) {
|
|
3453
|
+
isBindable = true;
|
|
3454
|
+
const parenStart = item.defaultValue.indexOf("(");
|
|
3455
|
+
const parenEnd = findMatchingBracket(item.defaultValue, parenStart, "(", ")");
|
|
3456
|
+
if (parenEnd !== -1) {
|
|
3457
|
+
bindableDefault = item.defaultValue.slice(parenStart + 1, parenEnd).trim() || null;
|
|
3458
|
+
}
|
|
3459
|
+
}
|
|
3460
|
+
}
|
|
3461
|
+
return {
|
|
3462
|
+
...item,
|
|
3463
|
+
isBindable,
|
|
3464
|
+
bindableDefault
|
|
3465
|
+
};
|
|
3466
|
+
}
|
|
3467
|
+
function generateDestructureReplacement(d, originInstances) {
|
|
3468
|
+
const lines = [];
|
|
3469
|
+
const instance = originInstances.get(d.sourceVar);
|
|
3470
|
+
if (!instance)
|
|
3471
|
+
return null;
|
|
3472
|
+
for (const m of d.methods) {
|
|
3473
|
+
const varName = m.alias || m.key;
|
|
3474
|
+
if (m.defaultValue) {
|
|
3475
|
+
lines.push(`let ${varName} = ${d.sourceVar}.${m.key}?.bind(${d.sourceVar}) ?? ${m.defaultValue}`);
|
|
3476
|
+
} else {
|
|
3477
|
+
lines.push(`let ${varName} = ${d.sourceVar}.${m.key}.bind(${d.sourceVar})`);
|
|
3478
|
+
}
|
|
3479
|
+
}
|
|
3480
|
+
if (d.nestedPropsPattern) {
|
|
3481
|
+
for (const p of d.nestedPropsPattern.props) {
|
|
3482
|
+
lines.push(...generatePropAccessors(p, d.sourceVar));
|
|
3483
|
+
}
|
|
3484
|
+
}
|
|
3485
|
+
if (d.isPropsAccess) {
|
|
3486
|
+
for (const p of d.props) {
|
|
3487
|
+
lines.push(...generatePropAccessors(p, d.sourceVar));
|
|
3488
|
+
}
|
|
3489
|
+
}
|
|
3490
|
+
return lines.join(`
|
|
3491
|
+
`) + `
|
|
3492
|
+
`;
|
|
3493
|
+
}
|
|
3494
|
+
function generatePropAccessors(p, sourceVar) {
|
|
3495
|
+
const lines = [];
|
|
3496
|
+
const varName = p.alias || p.key;
|
|
3497
|
+
if (p.isBindable) {
|
|
3498
|
+
const defaultVal = p.bindableDefault || "undefined";
|
|
3499
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key} ?? ${defaultVal})`);
|
|
3500
|
+
} else if (p.defaultValue) {
|
|
3501
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key} ?? ${p.defaultValue})`);
|
|
3502
|
+
} else {
|
|
3503
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key})`);
|
|
3504
|
+
}
|
|
3505
|
+
return lines;
|
|
3506
|
+
}
|
|
3507
|
+
function splitByTopLevelCommas2(content) {
|
|
3508
|
+
const parts = [];
|
|
3509
|
+
let current = "";
|
|
3510
|
+
let depth = 0;
|
|
3511
|
+
for (let i = 0;i < content.length; i++) {
|
|
3512
|
+
const char = content[i];
|
|
3513
|
+
if (char === "{" || char === "(" || char === "[" || char === "<") {
|
|
3514
|
+
depth++;
|
|
3515
|
+
current += char;
|
|
3516
|
+
} else if (char === "}" || char === ")" || char === "]" || char === ">") {
|
|
3517
|
+
depth--;
|
|
3518
|
+
current += char;
|
|
3519
|
+
} else if (char === "," && depth === 0) {
|
|
3520
|
+
parts.push(current);
|
|
3521
|
+
current = "";
|
|
3522
|
+
} else {
|
|
3523
|
+
current += char;
|
|
3524
|
+
}
|
|
3525
|
+
}
|
|
3526
|
+
if (current.trim()) {
|
|
3527
|
+
parts.push(current);
|
|
3528
|
+
}
|
|
3529
|
+
return parts;
|
|
3530
|
+
}
|
|
3531
|
+
function findTopLevelColon(str) {
|
|
3532
|
+
let depth = 0;
|
|
3533
|
+
for (let i = 0;i < str.length; i++) {
|
|
3534
|
+
const char = str[i];
|
|
3535
|
+
if (char === "{" || char === "(" || char === "[" || char === "<")
|
|
3536
|
+
depth++;
|
|
3537
|
+
else if (char === "}" || char === ")" || char === "]" || char === ">")
|
|
3538
|
+
depth--;
|
|
3539
|
+
else if (char === ":" && depth === 0)
|
|
3540
|
+
return i;
|
|
3541
|
+
}
|
|
3542
|
+
return -1;
|
|
3543
|
+
}
|
|
3544
|
+
function findTopLevelEquals(str) {
|
|
3545
|
+
let depth = 0;
|
|
3546
|
+
for (let i = 0;i < str.length; i++) {
|
|
3547
|
+
const char = str[i];
|
|
3548
|
+
if (char === "{" || char === "(" || char === "[" || char === "<")
|
|
3549
|
+
depth++;
|
|
3550
|
+
else if (char === "}" || char === ")" || char === "]" || char === ">")
|
|
3551
|
+
depth--;
|
|
3552
|
+
else if (char === "=" && depth === 0) {
|
|
3553
|
+
if (str[i + 1] !== "=" && str[i + 1] !== ">") {
|
|
3554
|
+
return i;
|
|
3555
|
+
}
|
|
3556
|
+
}
|
|
3557
|
+
}
|
|
3558
|
+
return -1;
|
|
3559
|
+
}
|
|
3560
|
+
function readIdentifier(str, start) {
|
|
3561
|
+
let result = "";
|
|
3562
|
+
let i = start;
|
|
3563
|
+
while (i < str.length && /[\w$]/.test(str[i])) {
|
|
3564
|
+
result += str[i];
|
|
3565
|
+
i++;
|
|
3566
|
+
}
|
|
3567
|
+
return result;
|
|
3568
|
+
}
|
|
3569
|
+
|
|
3038
3570
|
// src/transform/core.ts
|
|
3039
3571
|
function transformScript(source, options = {}) {
|
|
3040
3572
|
if (options.schemaResolver) {
|
|
@@ -3057,6 +3589,9 @@ function transformScript(source, options = {}) {
|
|
|
3057
3589
|
const result = transformAttrsOriginCallsSync(s, source, neededImports);
|
|
3058
3590
|
changed = result.changed || changed;
|
|
3059
3591
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3592
|
+
if (result.originInstances.size > 0) {
|
|
3593
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3594
|
+
}
|
|
3060
3595
|
}
|
|
3061
3596
|
if (isComponent) {
|
|
3062
3597
|
changed = transformAttrsForCallsSync(s, source, neededImports) || changed;
|
|
@@ -3128,6 +3663,9 @@ async function transformScriptAsync(source, options) {
|
|
|
3128
3663
|
const result = await transformAttrsOriginCalls(s, source, neededImports, schemaResolver);
|
|
3129
3664
|
changed = result.changed || changed;
|
|
3130
3665
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3666
|
+
if (result.originInstances.size > 0) {
|
|
3667
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3668
|
+
}
|
|
3131
3669
|
}
|
|
3132
3670
|
if (isComponent) {
|
|
3133
3671
|
changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
|
|
@@ -3198,6 +3736,9 @@ async function transformScriptContent(source, options) {
|
|
|
3198
3736
|
const result = await transformAttrsOriginCalls(s, source, neededImports, schemaResolver);
|
|
3199
3737
|
changed = result.changed || changed;
|
|
3200
3738
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3739
|
+
if (result.originInstances.size > 0) {
|
|
3740
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3741
|
+
}
|
|
3201
3742
|
}
|
|
3202
3743
|
if (isComponent) {
|
|
3203
3744
|
changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
|