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/cli.js
CHANGED
|
@@ -1551,11 +1551,93 @@ function findPropsInjectionPosition(source) {
|
|
|
1551
1551
|
}
|
|
1552
1552
|
|
|
1553
1553
|
// src/transform/attrs-schema.ts
|
|
1554
|
+
function stripLeadingComments(str) {
|
|
1555
|
+
let result = str.trim();
|
|
1556
|
+
while (true) {
|
|
1557
|
+
if (result.startsWith("/*")) {
|
|
1558
|
+
const endComment = result.indexOf("*/");
|
|
1559
|
+
if (endComment !== -1) {
|
|
1560
|
+
result = result.slice(endComment + 2).trim();
|
|
1561
|
+
continue;
|
|
1562
|
+
}
|
|
1563
|
+
break;
|
|
1564
|
+
}
|
|
1565
|
+
if (result.startsWith("//")) {
|
|
1566
|
+
const endLine = result.indexOf(`
|
|
1567
|
+
`);
|
|
1568
|
+
if (endLine !== -1) {
|
|
1569
|
+
result = result.slice(endLine + 1).trim();
|
|
1570
|
+
continue;
|
|
1571
|
+
}
|
|
1572
|
+
result = "";
|
|
1573
|
+
break;
|
|
1574
|
+
}
|
|
1575
|
+
break;
|
|
1576
|
+
}
|
|
1577
|
+
return result;
|
|
1578
|
+
}
|
|
1579
|
+
function stripTrailingComments(str) {
|
|
1580
|
+
let result = str.trim();
|
|
1581
|
+
let i = 0;
|
|
1582
|
+
let lastValidEnd = result.length;
|
|
1583
|
+
while (i < result.length) {
|
|
1584
|
+
const char = result[i];
|
|
1585
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
1586
|
+
const quote = char;
|
|
1587
|
+
i++;
|
|
1588
|
+
while (i < result.length && result[i] !== quote) {
|
|
1589
|
+
if (result[i] === "\\")
|
|
1590
|
+
i++;
|
|
1591
|
+
i++;
|
|
1592
|
+
}
|
|
1593
|
+
i++;
|
|
1594
|
+
lastValidEnd = i;
|
|
1595
|
+
continue;
|
|
1596
|
+
}
|
|
1597
|
+
if (char === "{" || char === "(" || char === "[") {
|
|
1598
|
+
let depth = 1;
|
|
1599
|
+
i++;
|
|
1600
|
+
while (i < result.length && depth > 0) {
|
|
1601
|
+
const c = result[i];
|
|
1602
|
+
if (c === "{" || c === "(" || c === "[")
|
|
1603
|
+
depth++;
|
|
1604
|
+
else if (c === "}" || c === ")" || c === "]")
|
|
1605
|
+
depth--;
|
|
1606
|
+
else if (c === '"' || c === "'" || c === "`") {
|
|
1607
|
+
const quote = c;
|
|
1608
|
+
i++;
|
|
1609
|
+
while (i < result.length && result[i] !== quote) {
|
|
1610
|
+
if (result[i] === "\\")
|
|
1611
|
+
i++;
|
|
1612
|
+
i++;
|
|
1613
|
+
}
|
|
1614
|
+
}
|
|
1615
|
+
i++;
|
|
1616
|
+
}
|
|
1617
|
+
lastValidEnd = i;
|
|
1618
|
+
continue;
|
|
1619
|
+
}
|
|
1620
|
+
if (char === "/" && result[i + 1] === "*") {
|
|
1621
|
+
result = result.slice(0, i).trim();
|
|
1622
|
+
break;
|
|
1623
|
+
}
|
|
1624
|
+
if (char === "/" && result[i + 1] === "/") {
|
|
1625
|
+
result = result.slice(0, i).trim();
|
|
1626
|
+
break;
|
|
1627
|
+
}
|
|
1628
|
+
i++;
|
|
1629
|
+
lastValidEnd = i;
|
|
1630
|
+
}
|
|
1631
|
+
return result;
|
|
1632
|
+
}
|
|
1554
1633
|
function generateAttrSchemaFromSource(attrsSource) {
|
|
1555
1634
|
const entries = [];
|
|
1556
1635
|
const attrs = splitAttrsSource(attrsSource);
|
|
1557
1636
|
for (const attr of attrs) {
|
|
1558
|
-
|
|
1637
|
+
let trimmed = attr.trim();
|
|
1638
|
+
if (!trimmed)
|
|
1639
|
+
continue;
|
|
1640
|
+
trimmed = stripLeadingComments(trimmed);
|
|
1559
1641
|
if (!trimmed)
|
|
1560
1642
|
continue;
|
|
1561
1643
|
const colonIndex = trimmed.indexOf(":");
|
|
@@ -1563,6 +1645,7 @@ function generateAttrSchemaFromSource(attrsSource) {
|
|
|
1563
1645
|
continue;
|
|
1564
1646
|
const key = trimmed.slice(0, colonIndex).trim();
|
|
1565
1647
|
let value = trimmed.slice(colonIndex + 1).trim();
|
|
1648
|
+
value = stripTrailingComments(value);
|
|
1566
1649
|
const asIndex = findTopLevelAs(value);
|
|
1567
1650
|
if (asIndex !== -1) {
|
|
1568
1651
|
value = value.slice(0, asIndex).trim();
|
|
@@ -1586,24 +1669,7 @@ function parseAttrsSource(attrsSource) {
|
|
|
1586
1669
|
let trimmed = attr.trim();
|
|
1587
1670
|
if (!trimmed)
|
|
1588
1671
|
continue;
|
|
1589
|
-
|
|
1590
|
-
const endComment = trimmed.indexOf("*/");
|
|
1591
|
-
if (endComment !== -1) {
|
|
1592
|
-
trimmed = trimmed.slice(endComment + 2).trim();
|
|
1593
|
-
} else {
|
|
1594
|
-
break;
|
|
1595
|
-
}
|
|
1596
|
-
}
|
|
1597
|
-
while (trimmed.startsWith("//")) {
|
|
1598
|
-
const endLine = trimmed.indexOf(`
|
|
1599
|
-
`);
|
|
1600
|
-
if (endLine !== -1) {
|
|
1601
|
-
trimmed = trimmed.slice(endLine + 1).trim();
|
|
1602
|
-
} else {
|
|
1603
|
-
trimmed = "";
|
|
1604
|
-
break;
|
|
1605
|
-
}
|
|
1606
|
-
}
|
|
1672
|
+
trimmed = stripLeadingComments(trimmed);
|
|
1607
1673
|
if (!trimmed)
|
|
1608
1674
|
continue;
|
|
1609
1675
|
const colonIndex = trimmed.indexOf(":");
|
|
@@ -1611,6 +1677,7 @@ function parseAttrsSource(attrsSource) {
|
|
|
1611
1677
|
continue;
|
|
1612
1678
|
const key = trimmed.slice(0, colonIndex).trim();
|
|
1613
1679
|
let value = trimmed.slice(colonIndex + 1).trim();
|
|
1680
|
+
value = stripTrailingComments(value);
|
|
1614
1681
|
const asIndex = findTopLevelAs(value);
|
|
1615
1682
|
if (asIndex !== -1) {
|
|
1616
1683
|
value = value.slice(0, asIndex).trim();
|
|
@@ -2255,6 +2322,79 @@ function transformStandaloneAttrsDefinition(content) {
|
|
|
2255
2322
|
}`;
|
|
2256
2323
|
}
|
|
2257
2324
|
|
|
2325
|
+
// src/transform/reserved-keywords.ts
|
|
2326
|
+
var RESERVED_KEYWORDS = new Set([
|
|
2327
|
+
"break",
|
|
2328
|
+
"case",
|
|
2329
|
+
"catch",
|
|
2330
|
+
"continue",
|
|
2331
|
+
"debugger",
|
|
2332
|
+
"default",
|
|
2333
|
+
"delete",
|
|
2334
|
+
"do",
|
|
2335
|
+
"else",
|
|
2336
|
+
"finally",
|
|
2337
|
+
"for",
|
|
2338
|
+
"function",
|
|
2339
|
+
"if",
|
|
2340
|
+
"in",
|
|
2341
|
+
"instanceof",
|
|
2342
|
+
"new",
|
|
2343
|
+
"return",
|
|
2344
|
+
"switch",
|
|
2345
|
+
"this",
|
|
2346
|
+
"throw",
|
|
2347
|
+
"try",
|
|
2348
|
+
"typeof",
|
|
2349
|
+
"var",
|
|
2350
|
+
"void",
|
|
2351
|
+
"while",
|
|
2352
|
+
"with",
|
|
2353
|
+
"class",
|
|
2354
|
+
"const",
|
|
2355
|
+
"enum",
|
|
2356
|
+
"export",
|
|
2357
|
+
"extends",
|
|
2358
|
+
"import",
|
|
2359
|
+
"super",
|
|
2360
|
+
"implements",
|
|
2361
|
+
"interface",
|
|
2362
|
+
"let",
|
|
2363
|
+
"package",
|
|
2364
|
+
"private",
|
|
2365
|
+
"protected",
|
|
2366
|
+
"public",
|
|
2367
|
+
"static",
|
|
2368
|
+
"yield",
|
|
2369
|
+
"await",
|
|
2370
|
+
"async",
|
|
2371
|
+
"true",
|
|
2372
|
+
"false",
|
|
2373
|
+
"null",
|
|
2374
|
+
"undefined",
|
|
2375
|
+
"arguments",
|
|
2376
|
+
"eval"
|
|
2377
|
+
]);
|
|
2378
|
+
function getInternalVarName(propName, forcePrefix = true) {
|
|
2379
|
+
return `___${propName}`;
|
|
2380
|
+
}
|
|
2381
|
+
function generatePropDestructure(propName, defaultValue) {
|
|
2382
|
+
const internalName = getInternalVarName(propName);
|
|
2383
|
+
return `${propName}: ${internalName} = ${defaultValue}`;
|
|
2384
|
+
}
|
|
2385
|
+
function generatePropDestructureNoDefault(propName) {
|
|
2386
|
+
const internalName = getInternalVarName(propName);
|
|
2387
|
+
return `${propName}: ${internalName}`;
|
|
2388
|
+
}
|
|
2389
|
+
function generateGetter(propName) {
|
|
2390
|
+
const internalName = getInternalVarName(propName);
|
|
2391
|
+
return `get ${propName}() { return ${internalName} }`;
|
|
2392
|
+
}
|
|
2393
|
+
function generateSetter(propName) {
|
|
2394
|
+
const internalName = getInternalVarName(propName);
|
|
2395
|
+
return `set ${propName}(v) { ${internalName} = v }`;
|
|
2396
|
+
}
|
|
2397
|
+
|
|
2258
2398
|
// src/transform/schema.ts
|
|
2259
2399
|
function parseOriginSchemaFromSource(source, exportName) {
|
|
2260
2400
|
const sourceResult = parseSourceOrigin(source, exportName);
|
|
@@ -2481,6 +2621,7 @@ function parseAttrsContent(content) {
|
|
|
2481
2621
|
continue;
|
|
2482
2622
|
const key = trimmed.slice(0, colonIndex).trim();
|
|
2483
2623
|
let value = trimmed.slice(colonIndex + 1).trim();
|
|
2624
|
+
value = stripTrailingComments2(value);
|
|
2484
2625
|
const asIndex = findTopLevelAs(value);
|
|
2485
2626
|
if (asIndex !== -1) {
|
|
2486
2627
|
value = value.slice(0, asIndex).trim();
|
|
@@ -2588,19 +2729,69 @@ function stripComments(str) {
|
|
|
2588
2729
|
}
|
|
2589
2730
|
return result;
|
|
2590
2731
|
}
|
|
2732
|
+
function stripTrailingComments2(str) {
|
|
2733
|
+
let result = str.trim();
|
|
2734
|
+
let i = 0;
|
|
2735
|
+
while (i < result.length) {
|
|
2736
|
+
const char = result[i];
|
|
2737
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
2738
|
+
const quote = char;
|
|
2739
|
+
i++;
|
|
2740
|
+
while (i < result.length && result[i] !== quote) {
|
|
2741
|
+
if (result[i] === "\\")
|
|
2742
|
+
i++;
|
|
2743
|
+
i++;
|
|
2744
|
+
}
|
|
2745
|
+
i++;
|
|
2746
|
+
continue;
|
|
2747
|
+
}
|
|
2748
|
+
if (char === "{" || char === "(" || char === "[") {
|
|
2749
|
+
let depth = 1;
|
|
2750
|
+
i++;
|
|
2751
|
+
while (i < result.length && depth > 0) {
|
|
2752
|
+
const c = result[i];
|
|
2753
|
+
if (c === "{" || c === "(" || c === "[")
|
|
2754
|
+
depth++;
|
|
2755
|
+
else if (c === "}" || c === ")" || c === "]")
|
|
2756
|
+
depth--;
|
|
2757
|
+
else if (c === '"' || c === "'" || c === "`") {
|
|
2758
|
+
const quote = c;
|
|
2759
|
+
i++;
|
|
2760
|
+
while (i < result.length && result[i] !== quote) {
|
|
2761
|
+
if (result[i] === "\\")
|
|
2762
|
+
i++;
|
|
2763
|
+
i++;
|
|
2764
|
+
}
|
|
2765
|
+
}
|
|
2766
|
+
i++;
|
|
2767
|
+
}
|
|
2768
|
+
continue;
|
|
2769
|
+
}
|
|
2770
|
+
if (char === "/" && result[i + 1] === "*") {
|
|
2771
|
+
result = result.slice(0, i).trim();
|
|
2772
|
+
break;
|
|
2773
|
+
}
|
|
2774
|
+
if (char === "/" && result[i + 1] === "/") {
|
|
2775
|
+
result = result.slice(0, i).trim();
|
|
2776
|
+
break;
|
|
2777
|
+
}
|
|
2778
|
+
i++;
|
|
2779
|
+
}
|
|
2780
|
+
return result;
|
|
2781
|
+
}
|
|
2591
2782
|
function generatePropsDestructuring(attrs, factoryName) {
|
|
2592
2783
|
const parts = [];
|
|
2593
2784
|
for (const attr of attrs) {
|
|
2594
2785
|
if (attr.bindable) {
|
|
2595
2786
|
if (attr.hasDefault) {
|
|
2596
|
-
parts.push(
|
|
2787
|
+
parts.push(generatePropDestructure(attr.key, `$bindable(${attr.defaultValue})`));
|
|
2597
2788
|
} else {
|
|
2598
|
-
parts.push(
|
|
2789
|
+
parts.push(generatePropDestructure(attr.key, "$bindable()"));
|
|
2599
2790
|
}
|
|
2600
2791
|
} else if (attr.hasDefault) {
|
|
2601
|
-
parts.push(
|
|
2792
|
+
parts.push(generatePropDestructure(attr.key, attr.defaultValue));
|
|
2602
2793
|
} else {
|
|
2603
|
-
parts.push(attr.key);
|
|
2794
|
+
parts.push(generatePropDestructureNoDefault(attr.key));
|
|
2604
2795
|
}
|
|
2605
2796
|
}
|
|
2606
2797
|
return `let { ${parts.join(", ")} }: $attrs.Of<typeof ${factoryName}> = $props()`;
|
|
@@ -2608,9 +2799,9 @@ function generatePropsDestructuring(attrs, factoryName) {
|
|
|
2608
2799
|
function generateFactoryCallFromAttrs(factoryName, attrs) {
|
|
2609
2800
|
const parts = [];
|
|
2610
2801
|
for (const attr of attrs) {
|
|
2611
|
-
parts.push(
|
|
2802
|
+
parts.push(generateGetter(attr.key));
|
|
2612
2803
|
if (attr.bindable) {
|
|
2613
|
-
parts.push(
|
|
2804
|
+
parts.push(generateSetter(attr.key));
|
|
2614
2805
|
}
|
|
2615
2806
|
}
|
|
2616
2807
|
return `${factoryName}({ ${parts.join(", ")} })`;
|
|
@@ -2744,7 +2935,10 @@ function transformAttrsOriginCallsSync(s, source, neededImports) {
|
|
|
2744
2935
|
let firstFactoryExpr = null;
|
|
2745
2936
|
let propsTypeDeclaration = null;
|
|
2746
2937
|
let isFirstDeclaration = true;
|
|
2747
|
-
const
|
|
2938
|
+
const originInstances = new Map;
|
|
2939
|
+
const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
2940
|
+
const hasExistingProps = !!existingPropsMatch;
|
|
2941
|
+
const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source) && !hasExistingProps;
|
|
2748
2942
|
for (const decl of declarations) {
|
|
2749
2943
|
const { varName, startIndex, macroOpenParen } = decl;
|
|
2750
2944
|
const closeParenIndex = findMatchingBracket(source, macroOpenParen);
|
|
@@ -2765,20 +2959,47 @@ function transformAttrsOriginCallsSync(s, source, neededImports) {
|
|
|
2765
2959
|
expansionLines.push(`type $$Props = $attrs.Of<${typeExpr}>`);
|
|
2766
2960
|
propsTypeDeclaration = "";
|
|
2767
2961
|
}
|
|
2768
|
-
|
|
2962
|
+
if (hasExistingProps) {
|
|
2963
|
+
const existingContent = existingPropsMatch[1];
|
|
2964
|
+
const hasRestSpread = /\.\.\.\s*(\w+)\s*$/.test(existingContent);
|
|
2965
|
+
if (!hasRestSpread && isFirstDeclaration) {
|
|
2966
|
+
const restVarName = "___originAttrs";
|
|
2967
|
+
const newContent = existingContent.trimEnd() ? `${existingContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
2968
|
+
const braceEnd = source.indexOf("}", existingPropsMatch.index + 5);
|
|
2969
|
+
if (braceEnd !== -1) {
|
|
2970
|
+
s.overwrite(existingPropsMatch.index + 5, braceEnd, ` ${newContent} `);
|
|
2971
|
+
}
|
|
2972
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVarName}))`);
|
|
2973
|
+
} else {
|
|
2974
|
+
const restMatch = existingContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
2975
|
+
const restVar = restMatch ? restMatch[1] : "___originAttrs";
|
|
2976
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVar}))`);
|
|
2977
|
+
}
|
|
2978
|
+
} else {
|
|
2979
|
+
expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
|
|
2980
|
+
}
|
|
2769
2981
|
s.overwrite(startIndex, endIndex, expansionLines.join(`
|
|
2770
2982
|
`));
|
|
2983
|
+
originInstances.set(varName, {
|
|
2984
|
+
varName,
|
|
2985
|
+
factoryExpr,
|
|
2986
|
+
startPos: startIndex,
|
|
2987
|
+
endPos: endIndex
|
|
2988
|
+
});
|
|
2771
2989
|
neededImports.add("__attrsFor");
|
|
2772
2990
|
changed = true;
|
|
2773
2991
|
isFirstDeclaration = false;
|
|
2774
2992
|
}
|
|
2775
|
-
return { changed, propsTypeDeclaration };
|
|
2993
|
+
return { changed, propsTypeDeclaration, originInstances };
|
|
2776
2994
|
}
|
|
2777
2995
|
async function transformAttrsOriginCalls(s, source, neededImports, schemaResolver) {
|
|
2778
2996
|
const declarations = findVariableDeclarationsWithMacro(source, "$attrs.origin");
|
|
2779
2997
|
let changed = false;
|
|
2780
2998
|
let propsTypeDeclaration = null;
|
|
2781
|
-
const
|
|
2999
|
+
const originInstances = new Map;
|
|
3000
|
+
const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
3001
|
+
const hasExistingProps = !!existingPropsMatch;
|
|
3002
|
+
const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source) && !hasExistingProps;
|
|
2782
3003
|
const matches = [];
|
|
2783
3004
|
for (const decl of declarations) {
|
|
2784
3005
|
const { varName, startIndex, macroOpenParen } = decl;
|
|
@@ -2798,6 +3019,7 @@ async function transformAttrsOriginCalls(s, source, neededImports, schemaResolve
|
|
|
2798
3019
|
}
|
|
2799
3020
|
let firstFactoryExpr = null;
|
|
2800
3021
|
let isFirstDeclaration = true;
|
|
3022
|
+
let restVarInjected = false;
|
|
2801
3023
|
for (const { varName, factoryExpr, startIndex, endIndex, isGeneric } of matches) {
|
|
2802
3024
|
const expansionLines = [];
|
|
2803
3025
|
let usesFallback = true;
|
|
@@ -2825,15 +3047,40 @@ async function transformAttrsOriginCalls(s, source, neededImports, schemaResolve
|
|
|
2825
3047
|
}
|
|
2826
3048
|
if (usesFallback) {
|
|
2827
3049
|
const normalizedFactoryCall = normalizeFactoryCall(factoryExpr);
|
|
2828
|
-
|
|
3050
|
+
if (hasExistingProps) {
|
|
3051
|
+
const existingContent = existingPropsMatch[1];
|
|
3052
|
+
const hasRestSpread = /\.\.\.\s*(\w+)\s*$/.test(existingContent);
|
|
3053
|
+
if (!hasRestSpread && !restVarInjected) {
|
|
3054
|
+
const restVarName = "___originAttrs";
|
|
3055
|
+
const newContent = existingContent.trimEnd() ? `${existingContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
3056
|
+
const braceEnd = source.indexOf("}", existingPropsMatch.index + 5);
|
|
3057
|
+
if (braceEnd !== -1) {
|
|
3058
|
+
s.overwrite(existingPropsMatch.index + 5, braceEnd, ` ${newContent} `);
|
|
3059
|
+
restVarInjected = true;
|
|
3060
|
+
}
|
|
3061
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVarName}))`);
|
|
3062
|
+
} else {
|
|
3063
|
+
const restMatch = existingContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
3064
|
+
const restVar = restMatch ? restMatch[1] : "___originAttrs";
|
|
3065
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVar}))`);
|
|
3066
|
+
}
|
|
3067
|
+
} else {
|
|
3068
|
+
expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
|
|
3069
|
+
}
|
|
2829
3070
|
neededImports.add("__attrsFor");
|
|
2830
3071
|
}
|
|
2831
3072
|
s.overwrite(startIndex, endIndex, expansionLines.join(`
|
|
2832
3073
|
`));
|
|
3074
|
+
originInstances.set(varName, {
|
|
3075
|
+
varName,
|
|
3076
|
+
factoryExpr,
|
|
3077
|
+
startPos: startIndex,
|
|
3078
|
+
endPos: endIndex
|
|
3079
|
+
});
|
|
2833
3080
|
changed = true;
|
|
2834
3081
|
isFirstDeclaration = false;
|
|
2835
3082
|
}
|
|
2836
|
-
return { changed, propsTypeDeclaration };
|
|
3083
|
+
return { changed, propsTypeDeclaration, originInstances };
|
|
2837
3084
|
}
|
|
2838
3085
|
|
|
2839
3086
|
// src/transform/element-types.ts
|
|
@@ -2845,9 +3092,9 @@ function getElementTypeImport(element) {
|
|
|
2845
3092
|
function generateReactiveAttrsWrapper(attrs) {
|
|
2846
3093
|
const parts = [];
|
|
2847
3094
|
for (const attr of attrs) {
|
|
2848
|
-
parts.push(
|
|
3095
|
+
parts.push(generateGetter(attr.key));
|
|
2849
3096
|
if (attr.bindable) {
|
|
2850
|
-
parts.push(
|
|
3097
|
+
parts.push(generateSetter(attr.key));
|
|
2851
3098
|
}
|
|
2852
3099
|
}
|
|
2853
3100
|
return `{ ${parts.join(", ")} }`;
|
|
@@ -2857,14 +3104,14 @@ function generateAttrsForMerge(attrs) {
|
|
|
2857
3104
|
for (const attr of attrs) {
|
|
2858
3105
|
if (attr.bindable) {
|
|
2859
3106
|
if (attr.hasDefault) {
|
|
2860
|
-
parts.push(
|
|
3107
|
+
parts.push(generatePropDestructure(attr.key, `$bindable(${attr.defaultValue})`));
|
|
2861
3108
|
} else {
|
|
2862
|
-
parts.push(
|
|
3109
|
+
parts.push(generatePropDestructure(attr.key, "$bindable()"));
|
|
2863
3110
|
}
|
|
2864
3111
|
} else if (attr.hasDefault) {
|
|
2865
|
-
parts.push(
|
|
3112
|
+
parts.push(generatePropDestructure(attr.key, attr.defaultValue));
|
|
2866
3113
|
} else {
|
|
2867
|
-
parts.push(attr.key);
|
|
3114
|
+
parts.push(generatePropDestructureNoDefault(attr.key));
|
|
2868
3115
|
}
|
|
2869
3116
|
}
|
|
2870
3117
|
return parts.join(", ");
|
|
@@ -3041,6 +3288,291 @@ async function transformAttrsForCalls(s, source, neededImports, schemaResolver)
|
|
|
3041
3288
|
return changed;
|
|
3042
3289
|
}
|
|
3043
3290
|
|
|
3291
|
+
// src/transform/origin-destructure-transform.ts
|
|
3292
|
+
function transformOriginDestructuring(s, source, originInstances) {
|
|
3293
|
+
let changed = false;
|
|
3294
|
+
const destructures = findOriginDestructures(source, originInstances);
|
|
3295
|
+
for (const d of destructures.reverse()) {
|
|
3296
|
+
const replacement = generateDestructureReplacement(d, originInstances);
|
|
3297
|
+
if (replacement) {
|
|
3298
|
+
s.overwrite(d.startPos, d.endPos, replacement);
|
|
3299
|
+
changed = true;
|
|
3300
|
+
}
|
|
3301
|
+
}
|
|
3302
|
+
return changed;
|
|
3303
|
+
}
|
|
3304
|
+
function findOriginDestructures(source, originInstances) {
|
|
3305
|
+
const results = [];
|
|
3306
|
+
const destructurePattern = /\b(let|const|var)\s*\{/g;
|
|
3307
|
+
let match;
|
|
3308
|
+
while ((match = destructurePattern.exec(source)) !== null) {
|
|
3309
|
+
if (isInsideStringOrComment(source, match.index))
|
|
3310
|
+
continue;
|
|
3311
|
+
const declKeyword = match[1];
|
|
3312
|
+
const braceStart = match.index + match[0].length - 1;
|
|
3313
|
+
const braceEnd = findMatchingBracket(source, braceStart, "{", "}");
|
|
3314
|
+
if (braceEnd === -1)
|
|
3315
|
+
continue;
|
|
3316
|
+
let i = braceEnd + 1;
|
|
3317
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3318
|
+
i++;
|
|
3319
|
+
if (source[i] !== "=")
|
|
3320
|
+
continue;
|
|
3321
|
+
i++;
|
|
3322
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3323
|
+
i++;
|
|
3324
|
+
const exprStart = i;
|
|
3325
|
+
let sourceVar = "";
|
|
3326
|
+
while (i < source.length && /[\w$]/.test(source[i])) {
|
|
3327
|
+
sourceVar += source[i];
|
|
3328
|
+
i++;
|
|
3329
|
+
}
|
|
3330
|
+
if (!sourceVar)
|
|
3331
|
+
continue;
|
|
3332
|
+
if (!originInstances.has(sourceVar))
|
|
3333
|
+
continue;
|
|
3334
|
+
let isPropsAccess = false;
|
|
3335
|
+
const afterVarPos = i;
|
|
3336
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3337
|
+
i++;
|
|
3338
|
+
if (source[i] === ".") {
|
|
3339
|
+
i++;
|
|
3340
|
+
while (i < source.length && /\s/.test(source[i]))
|
|
3341
|
+
i++;
|
|
3342
|
+
const propName = readIdentifier(source, i);
|
|
3343
|
+
if (propName === "props") {
|
|
3344
|
+
isPropsAccess = true;
|
|
3345
|
+
i += propName.length;
|
|
3346
|
+
} else {
|
|
3347
|
+
continue;
|
|
3348
|
+
}
|
|
3349
|
+
}
|
|
3350
|
+
let endPos = i;
|
|
3351
|
+
while (endPos < source.length && /\s/.test(source[endPos]) && source[endPos] !== `
|
|
3352
|
+
`)
|
|
3353
|
+
endPos++;
|
|
3354
|
+
if (source[endPos] === ";")
|
|
3355
|
+
endPos++;
|
|
3356
|
+
if (source[endPos] === `
|
|
3357
|
+
`)
|
|
3358
|
+
endPos++;
|
|
3359
|
+
const patternContent = source.slice(braceStart + 1, braceEnd);
|
|
3360
|
+
const parsed = parseDestructurePattern(patternContent, isPropsAccess);
|
|
3361
|
+
results.push({
|
|
3362
|
+
startPos: match.index,
|
|
3363
|
+
endPos,
|
|
3364
|
+
sourceVar,
|
|
3365
|
+
isPropsAccess,
|
|
3366
|
+
methods: parsed.methods,
|
|
3367
|
+
props: parsed.props,
|
|
3368
|
+
nestedPropsPattern: parsed.nestedProps
|
|
3369
|
+
});
|
|
3370
|
+
}
|
|
3371
|
+
return results;
|
|
3372
|
+
}
|
|
3373
|
+
function parseDestructurePattern(content, isPropsAccess) {
|
|
3374
|
+
const methods = [];
|
|
3375
|
+
const props = [];
|
|
3376
|
+
let nestedProps = null;
|
|
3377
|
+
const parts = splitByTopLevelCommas2(content);
|
|
3378
|
+
for (const part of parts) {
|
|
3379
|
+
const trimmed = part.trim();
|
|
3380
|
+
if (!trimmed)
|
|
3381
|
+
continue;
|
|
3382
|
+
const propsMatch = trimmed.match(/^props\s*:\s*\{/);
|
|
3383
|
+
if (propsMatch) {
|
|
3384
|
+
const braceStart = trimmed.indexOf("{");
|
|
3385
|
+
const braceEnd = findMatchingBracket(trimmed, braceStart, "{", "}");
|
|
3386
|
+
if (braceEnd !== -1) {
|
|
3387
|
+
const nestedContent = trimmed.slice(braceStart + 1, braceEnd);
|
|
3388
|
+
const nestedParts = splitByTopLevelCommas2(nestedContent);
|
|
3389
|
+
const nestedPropsList = [];
|
|
3390
|
+
for (const np of nestedParts) {
|
|
3391
|
+
const parsed = parseDestructuredProp(np.trim());
|
|
3392
|
+
if (parsed)
|
|
3393
|
+
nestedPropsList.push(parsed);
|
|
3394
|
+
}
|
|
3395
|
+
nestedProps = {
|
|
3396
|
+
startPos: 0,
|
|
3397
|
+
endPos: 0,
|
|
3398
|
+
props: nestedPropsList
|
|
3399
|
+
};
|
|
3400
|
+
}
|
|
3401
|
+
continue;
|
|
3402
|
+
}
|
|
3403
|
+
if (isPropsAccess) {
|
|
3404
|
+
const parsed = parseDestructuredProp(trimmed);
|
|
3405
|
+
if (parsed)
|
|
3406
|
+
props.push(parsed);
|
|
3407
|
+
} else {
|
|
3408
|
+
const parsed = parseDestructuredItem(trimmed);
|
|
3409
|
+
if (parsed)
|
|
3410
|
+
methods.push(parsed);
|
|
3411
|
+
}
|
|
3412
|
+
}
|
|
3413
|
+
return { methods, props, nestedProps };
|
|
3414
|
+
}
|
|
3415
|
+
function parseDestructuredItem(part) {
|
|
3416
|
+
const trimmed = part.trim();
|
|
3417
|
+
if (!trimmed)
|
|
3418
|
+
return null;
|
|
3419
|
+
const colonIdx = findTopLevelColon(trimmed);
|
|
3420
|
+
let key;
|
|
3421
|
+
let alias = null;
|
|
3422
|
+
let rest = trimmed;
|
|
3423
|
+
if (colonIdx !== -1) {
|
|
3424
|
+
key = trimmed.slice(0, colonIdx).trim();
|
|
3425
|
+
rest = trimmed.slice(colonIdx + 1).trim();
|
|
3426
|
+
const eqIdx2 = findTopLevelEquals(rest);
|
|
3427
|
+
if (eqIdx2 !== -1) {
|
|
3428
|
+
alias = rest.slice(0, eqIdx2).trim();
|
|
3429
|
+
return {
|
|
3430
|
+
key,
|
|
3431
|
+
alias,
|
|
3432
|
+
defaultValue: rest.slice(eqIdx2 + 1).trim()
|
|
3433
|
+
};
|
|
3434
|
+
} else {
|
|
3435
|
+
alias = rest;
|
|
3436
|
+
return { key, alias, defaultValue: null };
|
|
3437
|
+
}
|
|
3438
|
+
}
|
|
3439
|
+
const eqIdx = findTopLevelEquals(trimmed);
|
|
3440
|
+
if (eqIdx !== -1) {
|
|
3441
|
+
key = trimmed.slice(0, eqIdx).trim();
|
|
3442
|
+
return {
|
|
3443
|
+
key,
|
|
3444
|
+
alias: null,
|
|
3445
|
+
defaultValue: trimmed.slice(eqIdx + 1).trim()
|
|
3446
|
+
};
|
|
3447
|
+
}
|
|
3448
|
+
return { key: trimmed, alias: null, defaultValue: null };
|
|
3449
|
+
}
|
|
3450
|
+
function parseDestructuredProp(part) {
|
|
3451
|
+
const item = parseDestructuredItem(part);
|
|
3452
|
+
if (!item)
|
|
3453
|
+
return null;
|
|
3454
|
+
let isBindable = false;
|
|
3455
|
+
let bindableDefault = null;
|
|
3456
|
+
if (item.defaultValue) {
|
|
3457
|
+
const bindableMatch = item.defaultValue.match(/^\$bindable\s*\(/);
|
|
3458
|
+
if (bindableMatch) {
|
|
3459
|
+
isBindable = true;
|
|
3460
|
+
const parenStart = item.defaultValue.indexOf("(");
|
|
3461
|
+
const parenEnd = findMatchingBracket(item.defaultValue, parenStart, "(", ")");
|
|
3462
|
+
if (parenEnd !== -1) {
|
|
3463
|
+
bindableDefault = item.defaultValue.slice(parenStart + 1, parenEnd).trim() || null;
|
|
3464
|
+
}
|
|
3465
|
+
}
|
|
3466
|
+
}
|
|
3467
|
+
return {
|
|
3468
|
+
...item,
|
|
3469
|
+
isBindable,
|
|
3470
|
+
bindableDefault
|
|
3471
|
+
};
|
|
3472
|
+
}
|
|
3473
|
+
function generateDestructureReplacement(d, originInstances) {
|
|
3474
|
+
const lines = [];
|
|
3475
|
+
const instance = originInstances.get(d.sourceVar);
|
|
3476
|
+
if (!instance)
|
|
3477
|
+
return null;
|
|
3478
|
+
for (const m of d.methods) {
|
|
3479
|
+
const varName = m.alias || m.key;
|
|
3480
|
+
if (m.defaultValue) {
|
|
3481
|
+
lines.push(`let ${varName} = ${d.sourceVar}.${m.key}?.bind(${d.sourceVar}) ?? ${m.defaultValue}`);
|
|
3482
|
+
} else {
|
|
3483
|
+
lines.push(`let ${varName} = ${d.sourceVar}.${m.key}.bind(${d.sourceVar})`);
|
|
3484
|
+
}
|
|
3485
|
+
}
|
|
3486
|
+
if (d.nestedPropsPattern) {
|
|
3487
|
+
for (const p of d.nestedPropsPattern.props) {
|
|
3488
|
+
lines.push(...generatePropAccessors(p, d.sourceVar));
|
|
3489
|
+
}
|
|
3490
|
+
}
|
|
3491
|
+
if (d.isPropsAccess) {
|
|
3492
|
+
for (const p of d.props) {
|
|
3493
|
+
lines.push(...generatePropAccessors(p, d.sourceVar));
|
|
3494
|
+
}
|
|
3495
|
+
}
|
|
3496
|
+
return lines.join(`
|
|
3497
|
+
`) + `
|
|
3498
|
+
`;
|
|
3499
|
+
}
|
|
3500
|
+
function generatePropAccessors(p, sourceVar) {
|
|
3501
|
+
const lines = [];
|
|
3502
|
+
const varName = p.alias || p.key;
|
|
3503
|
+
if (p.isBindable) {
|
|
3504
|
+
const defaultVal = p.bindableDefault || "undefined";
|
|
3505
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key} ?? ${defaultVal})`);
|
|
3506
|
+
} else if (p.defaultValue) {
|
|
3507
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key} ?? ${p.defaultValue})`);
|
|
3508
|
+
} else {
|
|
3509
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key})`);
|
|
3510
|
+
}
|
|
3511
|
+
return lines;
|
|
3512
|
+
}
|
|
3513
|
+
function splitByTopLevelCommas2(content) {
|
|
3514
|
+
const parts = [];
|
|
3515
|
+
let current = "";
|
|
3516
|
+
let depth = 0;
|
|
3517
|
+
for (let i = 0;i < content.length; i++) {
|
|
3518
|
+
const char = content[i];
|
|
3519
|
+
if (char === "{" || char === "(" || char === "[" || char === "<") {
|
|
3520
|
+
depth++;
|
|
3521
|
+
current += char;
|
|
3522
|
+
} else if (char === "}" || char === ")" || char === "]" || char === ">") {
|
|
3523
|
+
depth--;
|
|
3524
|
+
current += char;
|
|
3525
|
+
} else if (char === "," && depth === 0) {
|
|
3526
|
+
parts.push(current);
|
|
3527
|
+
current = "";
|
|
3528
|
+
} else {
|
|
3529
|
+
current += char;
|
|
3530
|
+
}
|
|
3531
|
+
}
|
|
3532
|
+
if (current.trim()) {
|
|
3533
|
+
parts.push(current);
|
|
3534
|
+
}
|
|
3535
|
+
return parts;
|
|
3536
|
+
}
|
|
3537
|
+
function findTopLevelColon(str) {
|
|
3538
|
+
let depth = 0;
|
|
3539
|
+
for (let i = 0;i < str.length; i++) {
|
|
3540
|
+
const char = str[i];
|
|
3541
|
+
if (char === "{" || char === "(" || char === "[" || char === "<")
|
|
3542
|
+
depth++;
|
|
3543
|
+
else if (char === "}" || char === ")" || char === "]" || char === ">")
|
|
3544
|
+
depth--;
|
|
3545
|
+
else if (char === ":" && depth === 0)
|
|
3546
|
+
return i;
|
|
3547
|
+
}
|
|
3548
|
+
return -1;
|
|
3549
|
+
}
|
|
3550
|
+
function findTopLevelEquals(str) {
|
|
3551
|
+
let depth = 0;
|
|
3552
|
+
for (let i = 0;i < str.length; i++) {
|
|
3553
|
+
const char = str[i];
|
|
3554
|
+
if (char === "{" || char === "(" || char === "[" || char === "<")
|
|
3555
|
+
depth++;
|
|
3556
|
+
else if (char === "}" || char === ")" || char === "]" || char === ">")
|
|
3557
|
+
depth--;
|
|
3558
|
+
else if (char === "=" && depth === 0) {
|
|
3559
|
+
if (str[i + 1] !== "=" && str[i + 1] !== ">") {
|
|
3560
|
+
return i;
|
|
3561
|
+
}
|
|
3562
|
+
}
|
|
3563
|
+
}
|
|
3564
|
+
return -1;
|
|
3565
|
+
}
|
|
3566
|
+
function readIdentifier(str, start) {
|
|
3567
|
+
let result = "";
|
|
3568
|
+
let i = start;
|
|
3569
|
+
while (i < str.length && /[\w$]/.test(str[i])) {
|
|
3570
|
+
result += str[i];
|
|
3571
|
+
i++;
|
|
3572
|
+
}
|
|
3573
|
+
return result;
|
|
3574
|
+
}
|
|
3575
|
+
|
|
3044
3576
|
// src/transform/core.ts
|
|
3045
3577
|
function transformScript(source, options = {}) {
|
|
3046
3578
|
if (options.schemaResolver) {
|
|
@@ -3063,6 +3595,9 @@ function transformScript(source, options = {}) {
|
|
|
3063
3595
|
const result = transformAttrsOriginCallsSync(s, source, neededImports);
|
|
3064
3596
|
changed = result.changed || changed;
|
|
3065
3597
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3598
|
+
if (result.originInstances.size > 0) {
|
|
3599
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3600
|
+
}
|
|
3066
3601
|
}
|
|
3067
3602
|
if (isComponent) {
|
|
3068
3603
|
changed = transformAttrsForCallsSync(s, source, neededImports) || changed;
|
|
@@ -3134,6 +3669,9 @@ async function transformScriptAsync(source, options) {
|
|
|
3134
3669
|
const result = await transformAttrsOriginCalls(s, source, neededImports, schemaResolver);
|
|
3135
3670
|
changed = result.changed || changed;
|
|
3136
3671
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3672
|
+
if (result.originInstances.size > 0) {
|
|
3673
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3674
|
+
}
|
|
3137
3675
|
}
|
|
3138
3676
|
if (isComponent) {
|
|
3139
3677
|
changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
|
|
@@ -3204,6 +3742,9 @@ async function transformScriptContent(source, options) {
|
|
|
3204
3742
|
const result = await transformAttrsOriginCalls(s, source, neededImports, schemaResolver);
|
|
3205
3743
|
changed = result.changed || changed;
|
|
3206
3744
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
3745
|
+
if (result.originInstances.size > 0) {
|
|
3746
|
+
changed = transformOriginDestructuring(s, source, result.originInstances) || changed;
|
|
3747
|
+
}
|
|
3207
3748
|
}
|
|
3208
3749
|
if (isComponent) {
|
|
3209
3750
|
changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
|