svelte-origin 1.0.0-next.22 → 1.0.0-next.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of svelte-origin might be problematic. Click here for more details.
- package/dist/cli.js +571 -16
- package/dist/index.js +578 -49
- package/dist/plugin.js +614 -21
- package/dist/post-process.js +571 -16
- package/dist/preprocess.js +613 -23
- package/dist/runtime/index.js +7 -33
- package/dist/transform/attrs-for-transform.d.ts +36 -5
- package/dist/transform/attrs-origin-transform.d.ts +15 -0
- package/dist/transform/origin-destructure-transform.d.ts +62 -0
- package/dist/transform/reserved-keywords.d.ts +87 -0
- package/dist/transform/schema.d.ts +11 -4
- package/dist/vite-dts.js +79 -6
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3473,34 +3473,22 @@ function __attrsFor(factory, rawAttrs) {
|
|
|
3473
3473
|
const factoryWithSchema = factory;
|
|
3474
3474
|
const schema = getMergedAttrSchema(factoryWithSchema);
|
|
3475
3475
|
const wrapper = {};
|
|
3476
|
-
const localValues = {};
|
|
3477
|
-
const hasLocalValue = {};
|
|
3478
3476
|
const defineExposedProp = (key2, info) => {
|
|
3479
3477
|
if (Object.prototype.hasOwnProperty.call(wrapper, key2))
|
|
3480
3478
|
return;
|
|
3481
|
-
const
|
|
3479
|
+
const descriptor = {
|
|
3482
3480
|
get() {
|
|
3483
|
-
if (hasLocalValue[key2])
|
|
3484
|
-
return localValues[key2];
|
|
3485
3481
|
return rawAttrs[key2];
|
|
3486
3482
|
},
|
|
3487
3483
|
enumerable: true,
|
|
3488
3484
|
configurable: true
|
|
3489
3485
|
};
|
|
3490
3486
|
if (info.bindable) {
|
|
3491
|
-
|
|
3492
|
-
|
|
3493
|
-
|
|
3494
|
-
localValues[key2] = value;
|
|
3495
|
-
hasLocalValue[key2] = true;
|
|
3496
|
-
try {
|
|
3497
|
-
rawAttrs[key2] = value;
|
|
3498
|
-
} catch {}
|
|
3499
|
-
}
|
|
3500
|
-
});
|
|
3501
|
-
} else {
|
|
3502
|
-
Object.defineProperty(wrapper, key2, base);
|
|
3487
|
+
descriptor.set = function(value) {
|
|
3488
|
+
rawAttrs[key2] = value;
|
|
3489
|
+
};
|
|
3503
3490
|
}
|
|
3491
|
+
Object.defineProperty(wrapper, key2, descriptor);
|
|
3504
3492
|
};
|
|
3505
3493
|
for (const [key2, info] of Object.entries(schema)) {
|
|
3506
3494
|
const parentValue = rawAttrs[key2];
|
|
@@ -3521,8 +3509,6 @@ function __attrsFor(factory, rawAttrs) {
|
|
|
3521
3509
|
return new Proxy(wrapper, {
|
|
3522
3510
|
get(target, prop2) {
|
|
3523
3511
|
if (typeof prop2 === "string" && prop2 in schema) {
|
|
3524
|
-
if (hasLocalValue[prop2])
|
|
3525
|
-
return localValues[prop2];
|
|
3526
3512
|
return rawAttrs[prop2];
|
|
3527
3513
|
}
|
|
3528
3514
|
if (prop2 in target)
|
|
@@ -3531,15 +3517,11 @@ function __attrsFor(factory, rawAttrs) {
|
|
|
3531
3517
|
},
|
|
3532
3518
|
set(target, prop2, value) {
|
|
3533
3519
|
if (typeof prop2 === "string" && prop2 in schema) {
|
|
3534
|
-
localValues[prop2] = value;
|
|
3535
|
-
hasLocalValue[prop2] = true;
|
|
3536
3520
|
if (!Object.prototype.hasOwnProperty.call(wrapper, prop2)) {
|
|
3537
3521
|
defineExposedProp(prop2, schema[prop2]);
|
|
3538
3522
|
}
|
|
3539
3523
|
if (schema[prop2].bindable) {
|
|
3540
|
-
|
|
3541
|
-
rawAttrs[prop2] = value;
|
|
3542
|
-
} catch {}
|
|
3524
|
+
rawAttrs[prop2] = value;
|
|
3543
3525
|
}
|
|
3544
3526
|
return true;
|
|
3545
3527
|
}
|
|
@@ -3551,18 +3533,10 @@ function __attrsFor(factory, rawAttrs) {
|
|
|
3551
3533
|
}
|
|
3552
3534
|
try {
|
|
3553
3535
|
rawAttrs[prop2] = value;
|
|
3554
|
-
} catch {
|
|
3555
|
-
if (typeof prop2 === "string") {
|
|
3556
|
-
localValues[prop2] = value;
|
|
3557
|
-
hasLocalValue[prop2] = true;
|
|
3558
|
-
}
|
|
3559
|
-
}
|
|
3536
|
+
} catch {}
|
|
3560
3537
|
return true;
|
|
3561
3538
|
},
|
|
3562
3539
|
has(target, prop2) {
|
|
3563
|
-
if (typeof prop2 === "string" && prop2 in schema) {
|
|
3564
|
-
return prop2 in target || hasLocalValue[prop2];
|
|
3565
|
-
}
|
|
3566
3540
|
return prop2 in target || prop2 in rawAttrs;
|
|
3567
3541
|
},
|
|
3568
3542
|
ownKeys() {
|
|
@@ -3889,6 +3863,79 @@ function findMatchingBracket(source2, openIndex, openChar = "(", closeChar = ")"
|
|
|
3889
3863
|
return -1;
|
|
3890
3864
|
}
|
|
3891
3865
|
|
|
3866
|
+
// src/transform/reserved-keywords.ts
|
|
3867
|
+
var RESERVED_KEYWORDS = new Set([
|
|
3868
|
+
"break",
|
|
3869
|
+
"case",
|
|
3870
|
+
"catch",
|
|
3871
|
+
"continue",
|
|
3872
|
+
"debugger",
|
|
3873
|
+
"default",
|
|
3874
|
+
"delete",
|
|
3875
|
+
"do",
|
|
3876
|
+
"else",
|
|
3877
|
+
"finally",
|
|
3878
|
+
"for",
|
|
3879
|
+
"function",
|
|
3880
|
+
"if",
|
|
3881
|
+
"in",
|
|
3882
|
+
"instanceof",
|
|
3883
|
+
"new",
|
|
3884
|
+
"return",
|
|
3885
|
+
"switch",
|
|
3886
|
+
"this",
|
|
3887
|
+
"throw",
|
|
3888
|
+
"try",
|
|
3889
|
+
"typeof",
|
|
3890
|
+
"var",
|
|
3891
|
+
"void",
|
|
3892
|
+
"while",
|
|
3893
|
+
"with",
|
|
3894
|
+
"class",
|
|
3895
|
+
"const",
|
|
3896
|
+
"enum",
|
|
3897
|
+
"export",
|
|
3898
|
+
"extends",
|
|
3899
|
+
"import",
|
|
3900
|
+
"super",
|
|
3901
|
+
"implements",
|
|
3902
|
+
"interface",
|
|
3903
|
+
"let",
|
|
3904
|
+
"package",
|
|
3905
|
+
"private",
|
|
3906
|
+
"protected",
|
|
3907
|
+
"public",
|
|
3908
|
+
"static",
|
|
3909
|
+
"yield",
|
|
3910
|
+
"await",
|
|
3911
|
+
"async",
|
|
3912
|
+
"true",
|
|
3913
|
+
"false",
|
|
3914
|
+
"null",
|
|
3915
|
+
"undefined",
|
|
3916
|
+
"arguments",
|
|
3917
|
+
"eval"
|
|
3918
|
+
]);
|
|
3919
|
+
function getInternalVarName(propName, forcePrefix = true) {
|
|
3920
|
+
return `___${propName}`;
|
|
3921
|
+
}
|
|
3922
|
+
function generatePropDestructure(propName, defaultValue) {
|
|
3923
|
+
const internalName = getInternalVarName(propName);
|
|
3924
|
+
return `${propName}: ${internalName} = ${defaultValue}`;
|
|
3925
|
+
}
|
|
3926
|
+
function generatePropDestructureNoDefault(propName) {
|
|
3927
|
+
const internalName = getInternalVarName(propName);
|
|
3928
|
+
return `${propName}: ${internalName}`;
|
|
3929
|
+
}
|
|
3930
|
+
function generateGetter(propName) {
|
|
3931
|
+
const internalName = getInternalVarName(propName);
|
|
3932
|
+
return `get ${propName}() { return ${internalName} }`;
|
|
3933
|
+
}
|
|
3934
|
+
function generateSetter(propName) {
|
|
3935
|
+
const internalName = getInternalVarName(propName);
|
|
3936
|
+
return `set ${propName}(v) { ${internalName} = v }`;
|
|
3937
|
+
}
|
|
3938
|
+
|
|
3892
3939
|
// src/transform/schema.ts
|
|
3893
3940
|
function parseOriginSchemaFromSource(source2, exportName) {
|
|
3894
3941
|
const sourceResult = parseSourceOrigin(source2, exportName);
|
|
@@ -4227,14 +4274,14 @@ function generatePropsDestructuring(attrs, factoryName) {
|
|
|
4227
4274
|
for (const attr2 of attrs) {
|
|
4228
4275
|
if (attr2.bindable) {
|
|
4229
4276
|
if (attr2.hasDefault) {
|
|
4230
|
-
parts.push(
|
|
4277
|
+
parts.push(generatePropDestructure(attr2.key, `$bindable(${attr2.defaultValue})`));
|
|
4231
4278
|
} else {
|
|
4232
|
-
parts.push(
|
|
4279
|
+
parts.push(generatePropDestructure(attr2.key, "$bindable()"));
|
|
4233
4280
|
}
|
|
4234
4281
|
} else if (attr2.hasDefault) {
|
|
4235
|
-
parts.push(
|
|
4282
|
+
parts.push(generatePropDestructure(attr2.key, attr2.defaultValue));
|
|
4236
4283
|
} else {
|
|
4237
|
-
parts.push(attr2.key);
|
|
4284
|
+
parts.push(generatePropDestructureNoDefault(attr2.key));
|
|
4238
4285
|
}
|
|
4239
4286
|
}
|
|
4240
4287
|
return `let { ${parts.join(", ")} }: $attrs.Of<typeof ${factoryName}> = $props()`;
|
|
@@ -4242,9 +4289,9 @@ function generatePropsDestructuring(attrs, factoryName) {
|
|
|
4242
4289
|
function generateFactoryCallFromAttrs(factoryName, attrs) {
|
|
4243
4290
|
const parts = [];
|
|
4244
4291
|
for (const attr2 of attrs) {
|
|
4245
|
-
parts.push(
|
|
4292
|
+
parts.push(generateGetter(attr2.key));
|
|
4246
4293
|
if (attr2.bindable) {
|
|
4247
|
-
parts.push(
|
|
4294
|
+
parts.push(generateSetter(attr2.key));
|
|
4248
4295
|
}
|
|
4249
4296
|
}
|
|
4250
4297
|
return `${factoryName}({ ${parts.join(", ")} })`;
|
|
@@ -6563,7 +6610,10 @@ function transformAttrsOriginCallsSync(s, source2, neededImports) {
|
|
|
6563
6610
|
let firstFactoryExpr = null;
|
|
6564
6611
|
let propsTypeDeclaration = null;
|
|
6565
6612
|
let isFirstDeclaration = true;
|
|
6566
|
-
const
|
|
6613
|
+
const originInstances = new Map;
|
|
6614
|
+
const existingPropsMatch = source2.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
6615
|
+
const hasExistingProps = !!existingPropsMatch;
|
|
6616
|
+
const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source2) && !hasExistingProps;
|
|
6567
6617
|
for (const decl of declarations) {
|
|
6568
6618
|
const { varName, startIndex, macroOpenParen } = decl;
|
|
6569
6619
|
const closeParenIndex = findMatchingBracket(source2, macroOpenParen);
|
|
@@ -6584,20 +6634,47 @@ function transformAttrsOriginCallsSync(s, source2, neededImports) {
|
|
|
6584
6634
|
expansionLines.push(`type $$Props = $attrs.Of<${typeExpr}>`);
|
|
6585
6635
|
propsTypeDeclaration = "";
|
|
6586
6636
|
}
|
|
6587
|
-
|
|
6637
|
+
if (hasExistingProps) {
|
|
6638
|
+
const existingContent = existingPropsMatch[1];
|
|
6639
|
+
const hasRestSpread = /\.\.\.\s*(\w+)\s*$/.test(existingContent);
|
|
6640
|
+
if (!hasRestSpread && isFirstDeclaration) {
|
|
6641
|
+
const restVarName = "___originAttrs";
|
|
6642
|
+
const newContent = existingContent.trimEnd() ? `${existingContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
6643
|
+
const braceEnd = source2.indexOf("}", existingPropsMatch.index + 5);
|
|
6644
|
+
if (braceEnd !== -1) {
|
|
6645
|
+
s.overwrite(existingPropsMatch.index + 5, braceEnd, ` ${newContent} `);
|
|
6646
|
+
}
|
|
6647
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVarName}))`);
|
|
6648
|
+
} else {
|
|
6649
|
+
const restMatch = existingContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
6650
|
+
const restVar = restMatch ? restMatch[1] : "___originAttrs";
|
|
6651
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVar}))`);
|
|
6652
|
+
}
|
|
6653
|
+
} else {
|
|
6654
|
+
expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
|
|
6655
|
+
}
|
|
6588
6656
|
s.overwrite(startIndex, endIndex, expansionLines.join(`
|
|
6589
6657
|
`));
|
|
6658
|
+
originInstances.set(varName, {
|
|
6659
|
+
varName,
|
|
6660
|
+
factoryExpr,
|
|
6661
|
+
startPos: startIndex,
|
|
6662
|
+
endPos: endIndex
|
|
6663
|
+
});
|
|
6590
6664
|
neededImports.add("__attrsFor");
|
|
6591
6665
|
changed = true;
|
|
6592
6666
|
isFirstDeclaration = false;
|
|
6593
6667
|
}
|
|
6594
|
-
return { changed, propsTypeDeclaration };
|
|
6668
|
+
return { changed, propsTypeDeclaration, originInstances };
|
|
6595
6669
|
}
|
|
6596
6670
|
async function transformAttrsOriginCalls(s, source2, neededImports, schemaResolver) {
|
|
6597
6671
|
const declarations = findVariableDeclarationsWithMacro(source2, "$attrs.origin");
|
|
6598
6672
|
let changed = false;
|
|
6599
6673
|
let propsTypeDeclaration = null;
|
|
6600
|
-
const
|
|
6674
|
+
const originInstances = new Map;
|
|
6675
|
+
const existingPropsMatch = source2.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
6676
|
+
const hasExistingProps = !!existingPropsMatch;
|
|
6677
|
+
const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source2) && !hasExistingProps;
|
|
6601
6678
|
const matches = [];
|
|
6602
6679
|
for (const decl of declarations) {
|
|
6603
6680
|
const { varName, startIndex, macroOpenParen } = decl;
|
|
@@ -6617,6 +6694,7 @@ async function transformAttrsOriginCalls(s, source2, neededImports, schemaResolv
|
|
|
6617
6694
|
}
|
|
6618
6695
|
let firstFactoryExpr = null;
|
|
6619
6696
|
let isFirstDeclaration = true;
|
|
6697
|
+
let restVarInjected = false;
|
|
6620
6698
|
for (const { varName, factoryExpr, startIndex, endIndex, isGeneric } of matches) {
|
|
6621
6699
|
const expansionLines = [];
|
|
6622
6700
|
let usesFallback = true;
|
|
@@ -6644,15 +6722,40 @@ async function transformAttrsOriginCalls(s, source2, neededImports, schemaResolv
|
|
|
6644
6722
|
}
|
|
6645
6723
|
if (usesFallback) {
|
|
6646
6724
|
const normalizedFactoryCall = normalizeFactoryCall(factoryExpr);
|
|
6647
|
-
|
|
6725
|
+
if (hasExistingProps) {
|
|
6726
|
+
const existingContent = existingPropsMatch[1];
|
|
6727
|
+
const hasRestSpread = /\.\.\.\s*(\w+)\s*$/.test(existingContent);
|
|
6728
|
+
if (!hasRestSpread && !restVarInjected) {
|
|
6729
|
+
const restVarName = "___originAttrs";
|
|
6730
|
+
const newContent = existingContent.trimEnd() ? `${existingContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
6731
|
+
const braceEnd = source2.indexOf("}", existingPropsMatch.index + 5);
|
|
6732
|
+
if (braceEnd !== -1) {
|
|
6733
|
+
s.overwrite(existingPropsMatch.index + 5, braceEnd, ` ${newContent} `);
|
|
6734
|
+
restVarInjected = true;
|
|
6735
|
+
}
|
|
6736
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVarName}))`);
|
|
6737
|
+
} else {
|
|
6738
|
+
const restMatch = existingContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
6739
|
+
const restVar = restMatch ? restMatch[1] : "___originAttrs";
|
|
6740
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVar}))`);
|
|
6741
|
+
}
|
|
6742
|
+
} else {
|
|
6743
|
+
expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
|
|
6744
|
+
}
|
|
6648
6745
|
neededImports.add("__attrsFor");
|
|
6649
6746
|
}
|
|
6650
6747
|
s.overwrite(startIndex, endIndex, expansionLines.join(`
|
|
6651
6748
|
`));
|
|
6749
|
+
originInstances.set(varName, {
|
|
6750
|
+
varName,
|
|
6751
|
+
factoryExpr,
|
|
6752
|
+
startPos: startIndex,
|
|
6753
|
+
endPos: endIndex
|
|
6754
|
+
});
|
|
6652
6755
|
changed = true;
|
|
6653
6756
|
isFirstDeclaration = false;
|
|
6654
6757
|
}
|
|
6655
|
-
return { changed, propsTypeDeclaration };
|
|
6758
|
+
return { changed, propsTypeDeclaration, originInstances };
|
|
6656
6759
|
}
|
|
6657
6760
|
|
|
6658
6761
|
// src/transform/element-types.ts
|
|
@@ -6661,7 +6764,34 @@ function getElementTypeImport(element2) {
|
|
|
6661
6764
|
}
|
|
6662
6765
|
|
|
6663
6766
|
// src/transform/attrs-for-transform.ts
|
|
6664
|
-
function
|
|
6767
|
+
function generateReactiveAttrsWrapper(attrs) {
|
|
6768
|
+
const parts = [];
|
|
6769
|
+
for (const attr2 of attrs) {
|
|
6770
|
+
parts.push(generateGetter(attr2.key));
|
|
6771
|
+
if (attr2.bindable) {
|
|
6772
|
+
parts.push(generateSetter(attr2.key));
|
|
6773
|
+
}
|
|
6774
|
+
}
|
|
6775
|
+
return `{ ${parts.join(", ")} }`;
|
|
6776
|
+
}
|
|
6777
|
+
function generateAttrsForMerge(attrs) {
|
|
6778
|
+
const parts = [];
|
|
6779
|
+
for (const attr2 of attrs) {
|
|
6780
|
+
if (attr2.bindable) {
|
|
6781
|
+
if (attr2.hasDefault) {
|
|
6782
|
+
parts.push(generatePropDestructure(attr2.key, `$bindable(${attr2.defaultValue})`));
|
|
6783
|
+
} else {
|
|
6784
|
+
parts.push(generatePropDestructure(attr2.key, "$bindable()"));
|
|
6785
|
+
}
|
|
6786
|
+
} else if (attr2.hasDefault) {
|
|
6787
|
+
parts.push(generatePropDestructure(attr2.key, attr2.defaultValue));
|
|
6788
|
+
} else {
|
|
6789
|
+
parts.push(generatePropDestructureNoDefault(attr2.key));
|
|
6790
|
+
}
|
|
6791
|
+
}
|
|
6792
|
+
return parts.join(", ");
|
|
6793
|
+
}
|
|
6794
|
+
function transformAttrsForCallsSync(s, source2, neededImports) {
|
|
6665
6795
|
let changed = false;
|
|
6666
6796
|
const existingPropsMatch = source2.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
6667
6797
|
let restVarName = null;
|
|
@@ -6727,6 +6857,396 @@ function transformAttrsForCalls(s, source2, neededImports) {
|
|
|
6727
6857
|
}
|
|
6728
6858
|
return changed;
|
|
6729
6859
|
}
|
|
6860
|
+
async function transformAttrsForCalls(s, source2, neededImports, schemaResolver) {
|
|
6861
|
+
let changed = false;
|
|
6862
|
+
const existingPropsMatch = source2.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
6863
|
+
let restVarName = null;
|
|
6864
|
+
let existingPropsStart = -1;
|
|
6865
|
+
let existingDestructureContent = "";
|
|
6866
|
+
if (existingPropsMatch) {
|
|
6867
|
+
existingPropsStart = existingPropsMatch.index;
|
|
6868
|
+
existingDestructureContent = existingPropsMatch[1];
|
|
6869
|
+
const restMatch = existingDestructureContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
6870
|
+
if (restMatch) {
|
|
6871
|
+
restVarName = restMatch[1];
|
|
6872
|
+
}
|
|
6873
|
+
}
|
|
6874
|
+
const declarations = findVariableDeclarationsWithMacro(source2, "$attrs.for");
|
|
6875
|
+
for (const decl of declarations) {
|
|
6876
|
+
const { varName, startIndex, macroOpenParen } = decl;
|
|
6877
|
+
const closeParenIndex = findMatchingBracket(source2, macroOpenParen);
|
|
6878
|
+
if (closeParenIndex === -1)
|
|
6879
|
+
continue;
|
|
6880
|
+
const arg = source2.slice(macroOpenParen + 1, closeParenIndex).trim();
|
|
6881
|
+
const endIndex = closeParenIndex + 1;
|
|
6882
|
+
const stringMatch = arg.match(/^['"](\w+)['"]$/);
|
|
6883
|
+
if (stringMatch) {
|
|
6884
|
+
const element2 = stringMatch[1];
|
|
6885
|
+
const typeImport = getElementTypeImport(element2);
|
|
6886
|
+
if (existingPropsMatch && existingPropsStart !== -1) {
|
|
6887
|
+
if (!restVarName) {
|
|
6888
|
+
restVarName = "___restAttrs";
|
|
6889
|
+
const newDestructure = existingDestructureContent.trimEnd() ? `${existingDestructureContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
6890
|
+
const braceEnd = source2.indexOf("}", existingPropsStart + 5);
|
|
6891
|
+
if (braceEnd !== -1) {
|
|
6892
|
+
s.overwrite(existingPropsStart + 5, braceEnd, ` ${newDestructure} `);
|
|
6893
|
+
}
|
|
6894
|
+
}
|
|
6895
|
+
s.overwrite(startIndex, endIndex, `let ${varName} = ${restVarName} as ${typeImport}`);
|
|
6896
|
+
} else {
|
|
6897
|
+
const expansion = `let { ...${varName} } = $props<${typeImport}>()`;
|
|
6898
|
+
s.overwrite(startIndex, endIndex, expansion);
|
|
6899
|
+
}
|
|
6900
|
+
} else {
|
|
6901
|
+
const factoryName = arg;
|
|
6902
|
+
let transformed = false;
|
|
6903
|
+
if (schemaResolver) {
|
|
6904
|
+
const importPath = findImportPath(source2, factoryName);
|
|
6905
|
+
if (importPath) {
|
|
6906
|
+
const schema = await schemaResolver.resolve(importPath, factoryName);
|
|
6907
|
+
if (schema && schema.attrs.length > 0) {
|
|
6908
|
+
if (existingPropsMatch && existingPropsStart !== -1) {
|
|
6909
|
+
const additionalAttrs = generateAttrsForMerge(schema.attrs);
|
|
6910
|
+
const restMatch = existingDestructureContent.match(/,?\s*\.\.\.\s*(\w+)\s*$/);
|
|
6911
|
+
let baseContent = existingDestructureContent;
|
|
6912
|
+
let restPart = "";
|
|
6913
|
+
if (restMatch) {
|
|
6914
|
+
restPart = restMatch[0];
|
|
6915
|
+
baseContent = existingDestructureContent.slice(0, restMatch.index);
|
|
6916
|
+
}
|
|
6917
|
+
const mergedDestructure = baseContent.trimEnd() ? `${baseContent.trimEnd()}, ${additionalAttrs}${restPart}` : `${additionalAttrs}${restPart}`;
|
|
6918
|
+
const braceEnd = source2.indexOf("}", existingPropsStart + 5);
|
|
6919
|
+
if (braceEnd !== -1) {
|
|
6920
|
+
s.overwrite(existingPropsStart + 5, braceEnd, ` ${mergedDestructure} `);
|
|
6921
|
+
}
|
|
6922
|
+
const wrapper = generateReactiveAttrsWrapper(schema.attrs);
|
|
6923
|
+
s.overwrite(startIndex, endIndex, `let ${varName} = __attrsFor(${factoryName}, ${wrapper})`);
|
|
6924
|
+
transformed = true;
|
|
6925
|
+
} else {
|
|
6926
|
+
const propsDestructure = generatePropsDestructuring(schema.attrs, factoryName);
|
|
6927
|
+
const wrapper = generateReactiveAttrsWrapper(schema.attrs);
|
|
6928
|
+
const expansion = [
|
|
6929
|
+
propsDestructure,
|
|
6930
|
+
`let ${varName} = __attrsFor(${factoryName}, ${wrapper})`
|
|
6931
|
+
].join(`
|
|
6932
|
+
`);
|
|
6933
|
+
s.overwrite(startIndex, endIndex, expansion);
|
|
6934
|
+
transformed = true;
|
|
6935
|
+
}
|
|
6936
|
+
}
|
|
6937
|
+
}
|
|
6938
|
+
}
|
|
6939
|
+
if (!transformed) {
|
|
6940
|
+
if (existingPropsMatch && existingPropsStart !== -1) {
|
|
6941
|
+
if (!restVarName) {
|
|
6942
|
+
restVarName = "___restAttrs";
|
|
6943
|
+
const newDestructure = existingDestructureContent.trimEnd() ? `${existingDestructureContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
6944
|
+
const braceEnd = source2.indexOf("}", existingPropsStart + 5);
|
|
6945
|
+
if (braceEnd !== -1) {
|
|
6946
|
+
s.overwrite(existingPropsStart + 5, braceEnd, ` ${newDestructure} `);
|
|
6947
|
+
}
|
|
6948
|
+
}
|
|
6949
|
+
s.overwrite(startIndex, endIndex, `let ${varName} = __attrsFor(${factoryName}, ${restVarName})`);
|
|
6950
|
+
} else {
|
|
6951
|
+
const expansion = [
|
|
6952
|
+
`let ___attrs: $attrs.Of<typeof ${factoryName}> = $props()`,
|
|
6953
|
+
`let ${varName} = __attrsFor(${factoryName}, ___attrs)`
|
|
6954
|
+
].join(`
|
|
6955
|
+
`);
|
|
6956
|
+
s.overwrite(startIndex, endIndex, expansion);
|
|
6957
|
+
}
|
|
6958
|
+
}
|
|
6959
|
+
neededImports.add("__attrsFor");
|
|
6960
|
+
}
|
|
6961
|
+
changed = true;
|
|
6962
|
+
}
|
|
6963
|
+
return changed;
|
|
6964
|
+
}
|
|
6965
|
+
|
|
6966
|
+
// src/transform/origin-destructure-transform.ts
|
|
6967
|
+
function transformOriginDestructuring(s, source2, originInstances) {
|
|
6968
|
+
let changed = false;
|
|
6969
|
+
const destructures = findOriginDestructures(source2, originInstances);
|
|
6970
|
+
for (const d of destructures.reverse()) {
|
|
6971
|
+
const replacement = generateDestructureReplacement(d, originInstances);
|
|
6972
|
+
if (replacement) {
|
|
6973
|
+
s.overwrite(d.startPos, d.endPos, replacement);
|
|
6974
|
+
changed = true;
|
|
6975
|
+
}
|
|
6976
|
+
}
|
|
6977
|
+
return changed;
|
|
6978
|
+
}
|
|
6979
|
+
function findOriginDestructures(source2, originInstances) {
|
|
6980
|
+
const results = [];
|
|
6981
|
+
const destructurePattern = /\b(let|const|var)\s*\{/g;
|
|
6982
|
+
let match;
|
|
6983
|
+
while ((match = destructurePattern.exec(source2)) !== null) {
|
|
6984
|
+
if (isInsideStringOrComment(source2, match.index))
|
|
6985
|
+
continue;
|
|
6986
|
+
const declKeyword = match[1];
|
|
6987
|
+
const braceStart = match.index + match[0].length - 1;
|
|
6988
|
+
const braceEnd = findMatchingBracket(source2, braceStart, "{", "}");
|
|
6989
|
+
if (braceEnd === -1)
|
|
6990
|
+
continue;
|
|
6991
|
+
let i = braceEnd + 1;
|
|
6992
|
+
while (i < source2.length && /\s/.test(source2[i]))
|
|
6993
|
+
i++;
|
|
6994
|
+
if (source2[i] !== "=")
|
|
6995
|
+
continue;
|
|
6996
|
+
i++;
|
|
6997
|
+
while (i < source2.length && /\s/.test(source2[i]))
|
|
6998
|
+
i++;
|
|
6999
|
+
const exprStart = i;
|
|
7000
|
+
let sourceVar = "";
|
|
7001
|
+
while (i < source2.length && /[\w$]/.test(source2[i])) {
|
|
7002
|
+
sourceVar += source2[i];
|
|
7003
|
+
i++;
|
|
7004
|
+
}
|
|
7005
|
+
if (!sourceVar)
|
|
7006
|
+
continue;
|
|
7007
|
+
if (!originInstances.has(sourceVar))
|
|
7008
|
+
continue;
|
|
7009
|
+
let isPropsAccess = false;
|
|
7010
|
+
const afterVarPos = i;
|
|
7011
|
+
while (i < source2.length && /\s/.test(source2[i]))
|
|
7012
|
+
i++;
|
|
7013
|
+
if (source2[i] === ".") {
|
|
7014
|
+
i++;
|
|
7015
|
+
while (i < source2.length && /\s/.test(source2[i]))
|
|
7016
|
+
i++;
|
|
7017
|
+
const propName = readIdentifier(source2, i);
|
|
7018
|
+
if (propName === "props") {
|
|
7019
|
+
isPropsAccess = true;
|
|
7020
|
+
i += propName.length;
|
|
7021
|
+
} else {
|
|
7022
|
+
continue;
|
|
7023
|
+
}
|
|
7024
|
+
}
|
|
7025
|
+
let endPos = i;
|
|
7026
|
+
while (endPos < source2.length && /\s/.test(source2[endPos]) && source2[endPos] !== `
|
|
7027
|
+
`)
|
|
7028
|
+
endPos++;
|
|
7029
|
+
if (source2[endPos] === ";")
|
|
7030
|
+
endPos++;
|
|
7031
|
+
if (source2[endPos] === `
|
|
7032
|
+
`)
|
|
7033
|
+
endPos++;
|
|
7034
|
+
const patternContent = source2.slice(braceStart + 1, braceEnd);
|
|
7035
|
+
const parsed = parseDestructurePattern(patternContent, isPropsAccess);
|
|
7036
|
+
results.push({
|
|
7037
|
+
startPos: match.index,
|
|
7038
|
+
endPos,
|
|
7039
|
+
sourceVar,
|
|
7040
|
+
isPropsAccess,
|
|
7041
|
+
methods: parsed.methods,
|
|
7042
|
+
props: parsed.props,
|
|
7043
|
+
nestedPropsPattern: parsed.nestedProps
|
|
7044
|
+
});
|
|
7045
|
+
}
|
|
7046
|
+
return results;
|
|
7047
|
+
}
|
|
7048
|
+
function parseDestructurePattern(content, isPropsAccess) {
|
|
7049
|
+
const methods = [];
|
|
7050
|
+
const props = [];
|
|
7051
|
+
let nestedProps = null;
|
|
7052
|
+
const parts = splitByTopLevelCommas2(content);
|
|
7053
|
+
for (const part of parts) {
|
|
7054
|
+
const trimmed = part.trim();
|
|
7055
|
+
if (!trimmed)
|
|
7056
|
+
continue;
|
|
7057
|
+
const propsMatch = trimmed.match(/^props\s*:\s*\{/);
|
|
7058
|
+
if (propsMatch) {
|
|
7059
|
+
const braceStart = trimmed.indexOf("{");
|
|
7060
|
+
const braceEnd = findMatchingBracket(trimmed, braceStart, "{", "}");
|
|
7061
|
+
if (braceEnd !== -1) {
|
|
7062
|
+
const nestedContent = trimmed.slice(braceStart + 1, braceEnd);
|
|
7063
|
+
const nestedParts = splitByTopLevelCommas2(nestedContent);
|
|
7064
|
+
const nestedPropsList = [];
|
|
7065
|
+
for (const np of nestedParts) {
|
|
7066
|
+
const parsed = parseDestructuredProp(np.trim());
|
|
7067
|
+
if (parsed)
|
|
7068
|
+
nestedPropsList.push(parsed);
|
|
7069
|
+
}
|
|
7070
|
+
nestedProps = {
|
|
7071
|
+
startPos: 0,
|
|
7072
|
+
endPos: 0,
|
|
7073
|
+
props: nestedPropsList
|
|
7074
|
+
};
|
|
7075
|
+
}
|
|
7076
|
+
continue;
|
|
7077
|
+
}
|
|
7078
|
+
if (isPropsAccess) {
|
|
7079
|
+
const parsed = parseDestructuredProp(trimmed);
|
|
7080
|
+
if (parsed)
|
|
7081
|
+
props.push(parsed);
|
|
7082
|
+
} else {
|
|
7083
|
+
const parsed = parseDestructuredItem(trimmed);
|
|
7084
|
+
if (parsed)
|
|
7085
|
+
methods.push(parsed);
|
|
7086
|
+
}
|
|
7087
|
+
}
|
|
7088
|
+
return { methods, props, nestedProps };
|
|
7089
|
+
}
|
|
7090
|
+
function parseDestructuredItem(part) {
|
|
7091
|
+
const trimmed = part.trim();
|
|
7092
|
+
if (!trimmed)
|
|
7093
|
+
return null;
|
|
7094
|
+
const colonIdx = findTopLevelColon(trimmed);
|
|
7095
|
+
let key2;
|
|
7096
|
+
let alias = null;
|
|
7097
|
+
let rest = trimmed;
|
|
7098
|
+
if (colonIdx !== -1) {
|
|
7099
|
+
key2 = trimmed.slice(0, colonIdx).trim();
|
|
7100
|
+
rest = trimmed.slice(colonIdx + 1).trim();
|
|
7101
|
+
const eqIdx2 = findTopLevelEquals(rest);
|
|
7102
|
+
if (eqIdx2 !== -1) {
|
|
7103
|
+
alias = rest.slice(0, eqIdx2).trim();
|
|
7104
|
+
return {
|
|
7105
|
+
key: key2,
|
|
7106
|
+
alias,
|
|
7107
|
+
defaultValue: rest.slice(eqIdx2 + 1).trim()
|
|
7108
|
+
};
|
|
7109
|
+
} else {
|
|
7110
|
+
alias = rest;
|
|
7111
|
+
return { key: key2, alias, defaultValue: null };
|
|
7112
|
+
}
|
|
7113
|
+
}
|
|
7114
|
+
const eqIdx = findTopLevelEquals(trimmed);
|
|
7115
|
+
if (eqIdx !== -1) {
|
|
7116
|
+
key2 = trimmed.slice(0, eqIdx).trim();
|
|
7117
|
+
return {
|
|
7118
|
+
key: key2,
|
|
7119
|
+
alias: null,
|
|
7120
|
+
defaultValue: trimmed.slice(eqIdx + 1).trim()
|
|
7121
|
+
};
|
|
7122
|
+
}
|
|
7123
|
+
return { key: trimmed, alias: null, defaultValue: null };
|
|
7124
|
+
}
|
|
7125
|
+
function parseDestructuredProp(part) {
|
|
7126
|
+
const item = parseDestructuredItem(part);
|
|
7127
|
+
if (!item)
|
|
7128
|
+
return null;
|
|
7129
|
+
let isBindable2 = false;
|
|
7130
|
+
let bindableDefault = null;
|
|
7131
|
+
if (item.defaultValue) {
|
|
7132
|
+
const bindableMatch = item.defaultValue.match(/^\$bindable\s*\(/);
|
|
7133
|
+
if (bindableMatch) {
|
|
7134
|
+
isBindable2 = true;
|
|
7135
|
+
const parenStart = item.defaultValue.indexOf("(");
|
|
7136
|
+
const parenEnd = findMatchingBracket(item.defaultValue, parenStart, "(", ")");
|
|
7137
|
+
if (parenEnd !== -1) {
|
|
7138
|
+
bindableDefault = item.defaultValue.slice(parenStart + 1, parenEnd).trim() || null;
|
|
7139
|
+
}
|
|
7140
|
+
}
|
|
7141
|
+
}
|
|
7142
|
+
return {
|
|
7143
|
+
...item,
|
|
7144
|
+
isBindable: isBindable2,
|
|
7145
|
+
bindableDefault
|
|
7146
|
+
};
|
|
7147
|
+
}
|
|
7148
|
+
function generateDestructureReplacement(d, originInstances) {
|
|
7149
|
+
const lines = [];
|
|
7150
|
+
const instance = originInstances.get(d.sourceVar);
|
|
7151
|
+
if (!instance)
|
|
7152
|
+
return null;
|
|
7153
|
+
for (const m of d.methods) {
|
|
7154
|
+
const varName = m.alias || m.key;
|
|
7155
|
+
if (m.defaultValue) {
|
|
7156
|
+
lines.push(`let ${varName} = ${d.sourceVar}.${m.key}?.bind(${d.sourceVar}) ?? ${m.defaultValue}`);
|
|
7157
|
+
} else {
|
|
7158
|
+
lines.push(`let ${varName} = ${d.sourceVar}.${m.key}.bind(${d.sourceVar})`);
|
|
7159
|
+
}
|
|
7160
|
+
}
|
|
7161
|
+
if (d.nestedPropsPattern) {
|
|
7162
|
+
for (const p of d.nestedPropsPattern.props) {
|
|
7163
|
+
lines.push(...generatePropAccessors(p, d.sourceVar));
|
|
7164
|
+
}
|
|
7165
|
+
}
|
|
7166
|
+
if (d.isPropsAccess) {
|
|
7167
|
+
for (const p of d.props) {
|
|
7168
|
+
lines.push(...generatePropAccessors(p, d.sourceVar));
|
|
7169
|
+
}
|
|
7170
|
+
}
|
|
7171
|
+
return lines.join(`
|
|
7172
|
+
`) + `
|
|
7173
|
+
`;
|
|
7174
|
+
}
|
|
7175
|
+
function generatePropAccessors(p, sourceVar) {
|
|
7176
|
+
const lines = [];
|
|
7177
|
+
const varName = p.alias || p.key;
|
|
7178
|
+
if (p.isBindable) {
|
|
7179
|
+
const defaultVal = p.bindableDefault || "undefined";
|
|
7180
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key} ?? ${defaultVal})`);
|
|
7181
|
+
} else if (p.defaultValue) {
|
|
7182
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key} ?? ${p.defaultValue})`);
|
|
7183
|
+
} else {
|
|
7184
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key})`);
|
|
7185
|
+
}
|
|
7186
|
+
return lines;
|
|
7187
|
+
}
|
|
7188
|
+
function splitByTopLevelCommas2(content) {
|
|
7189
|
+
const parts = [];
|
|
7190
|
+
let current = "";
|
|
7191
|
+
let depth = 0;
|
|
7192
|
+
for (let i = 0;i < content.length; i++) {
|
|
7193
|
+
const char = content[i];
|
|
7194
|
+
if (char === "{" || char === "(" || char === "[" || char === "<") {
|
|
7195
|
+
depth++;
|
|
7196
|
+
current += char;
|
|
7197
|
+
} else if (char === "}" || char === ")" || char === "]" || char === ">") {
|
|
7198
|
+
depth--;
|
|
7199
|
+
current += char;
|
|
7200
|
+
} else if (char === "," && depth === 0) {
|
|
7201
|
+
parts.push(current);
|
|
7202
|
+
current = "";
|
|
7203
|
+
} else {
|
|
7204
|
+
current += char;
|
|
7205
|
+
}
|
|
7206
|
+
}
|
|
7207
|
+
if (current.trim()) {
|
|
7208
|
+
parts.push(current);
|
|
7209
|
+
}
|
|
7210
|
+
return parts;
|
|
7211
|
+
}
|
|
7212
|
+
function findTopLevelColon(str) {
|
|
7213
|
+
let depth = 0;
|
|
7214
|
+
for (let i = 0;i < str.length; i++) {
|
|
7215
|
+
const char = str[i];
|
|
7216
|
+
if (char === "{" || char === "(" || char === "[" || char === "<")
|
|
7217
|
+
depth++;
|
|
7218
|
+
else if (char === "}" || char === ")" || char === "]" || char === ">")
|
|
7219
|
+
depth--;
|
|
7220
|
+
else if (char === ":" && depth === 0)
|
|
7221
|
+
return i;
|
|
7222
|
+
}
|
|
7223
|
+
return -1;
|
|
7224
|
+
}
|
|
7225
|
+
function findTopLevelEquals(str) {
|
|
7226
|
+
let depth = 0;
|
|
7227
|
+
for (let i = 0;i < str.length; i++) {
|
|
7228
|
+
const char = str[i];
|
|
7229
|
+
if (char === "{" || char === "(" || char === "[" || char === "<")
|
|
7230
|
+
depth++;
|
|
7231
|
+
else if (char === "}" || char === ")" || char === "]" || char === ">")
|
|
7232
|
+
depth--;
|
|
7233
|
+
else if (char === "=" && depth === 0) {
|
|
7234
|
+
if (str[i + 1] !== "=" && str[i + 1] !== ">") {
|
|
7235
|
+
return i;
|
|
7236
|
+
}
|
|
7237
|
+
}
|
|
7238
|
+
}
|
|
7239
|
+
return -1;
|
|
7240
|
+
}
|
|
7241
|
+
function readIdentifier(str, start) {
|
|
7242
|
+
let result = "";
|
|
7243
|
+
let i = start;
|
|
7244
|
+
while (i < str.length && /[\w$]/.test(str[i])) {
|
|
7245
|
+
result += str[i];
|
|
7246
|
+
i++;
|
|
7247
|
+
}
|
|
7248
|
+
return result;
|
|
7249
|
+
}
|
|
6730
7250
|
|
|
6731
7251
|
// src/transform/core.ts
|
|
6732
7252
|
function transformScript(source2, options = {}) {
|
|
@@ -6750,9 +7270,12 @@ function transformScript(source2, options = {}) {
|
|
|
6750
7270
|
const result = transformAttrsOriginCallsSync(s, source2, neededImports);
|
|
6751
7271
|
changed = result.changed || changed;
|
|
6752
7272
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
7273
|
+
if (result.originInstances.size > 0) {
|
|
7274
|
+
changed = transformOriginDestructuring(s, source2, result.originInstances) || changed;
|
|
7275
|
+
}
|
|
6753
7276
|
}
|
|
6754
7277
|
if (isComponent) {
|
|
6755
|
-
changed =
|
|
7278
|
+
changed = transformAttrsForCallsSync(s, source2, neededImports) || changed;
|
|
6756
7279
|
}
|
|
6757
7280
|
const needsPropsInjection = propsTypeDeclaration && propsTypeDeclaration.length > 0;
|
|
6758
7281
|
if (isComponent && (neededImports.size > 0 || needsPropsInjection)) {
|
|
@@ -6821,9 +7344,12 @@ async function transformScriptAsync(source2, options) {
|
|
|
6821
7344
|
const result = await transformAttrsOriginCalls(s, source2, neededImports, schemaResolver);
|
|
6822
7345
|
changed = result.changed || changed;
|
|
6823
7346
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
7347
|
+
if (result.originInstances.size > 0) {
|
|
7348
|
+
changed = transformOriginDestructuring(s, source2, result.originInstances) || changed;
|
|
7349
|
+
}
|
|
6824
7350
|
}
|
|
6825
7351
|
if (isComponent) {
|
|
6826
|
-
changed = transformAttrsForCalls(s, source2, neededImports) || changed;
|
|
7352
|
+
changed = await transformAttrsForCalls(s, source2, neededImports, schemaResolver) || changed;
|
|
6827
7353
|
}
|
|
6828
7354
|
const needsPropsInjectionAsync = propsTypeDeclaration && propsTypeDeclaration.length > 0;
|
|
6829
7355
|
if (isComponent && (neededImports.size > 0 || needsPropsInjectionAsync)) {
|
|
@@ -6891,9 +7417,12 @@ async function transformScriptContent(source2, options) {
|
|
|
6891
7417
|
const result = await transformAttrsOriginCalls(s, source2, neededImports, schemaResolver);
|
|
6892
7418
|
changed = result.changed || changed;
|
|
6893
7419
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
7420
|
+
if (result.originInstances.size > 0) {
|
|
7421
|
+
changed = transformOriginDestructuring(s, source2, result.originInstances) || changed;
|
|
7422
|
+
}
|
|
6894
7423
|
}
|
|
6895
7424
|
if (isComponent) {
|
|
6896
|
-
changed = transformAttrsForCalls(s, source2, neededImports) || changed;
|
|
7425
|
+
changed = await transformAttrsForCalls(s, source2, neededImports, schemaResolver) || changed;
|
|
6897
7426
|
}
|
|
6898
7427
|
if (neededImports.size > 0) {
|
|
6899
7428
|
const importStatement = generateRuntimeImport([...neededImports]);
|