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