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/index.js
CHANGED
|
@@ -3863,6 +3863,79 @@ function findMatchingBracket(source2, openIndex, openChar = "(", closeChar = ")"
|
|
|
3863
3863
|
return -1;
|
|
3864
3864
|
}
|
|
3865
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
|
+
|
|
3866
3939
|
// src/transform/schema.ts
|
|
3867
3940
|
function parseOriginSchemaFromSource(source2, exportName) {
|
|
3868
3941
|
const sourceResult = parseSourceOrigin(source2, exportName);
|
|
@@ -4089,6 +4162,7 @@ function parseAttrsContent(content) {
|
|
|
4089
4162
|
continue;
|
|
4090
4163
|
const key2 = trimmed.slice(0, colonIndex).trim();
|
|
4091
4164
|
let value = trimmed.slice(colonIndex + 1).trim();
|
|
4165
|
+
value = stripTrailingComments(value);
|
|
4092
4166
|
const asIndex = findTopLevelAs(value);
|
|
4093
4167
|
if (asIndex !== -1) {
|
|
4094
4168
|
value = value.slice(0, asIndex).trim();
|
|
@@ -4196,19 +4270,69 @@ function stripComments(str) {
|
|
|
4196
4270
|
}
|
|
4197
4271
|
return result;
|
|
4198
4272
|
}
|
|
4273
|
+
function stripTrailingComments(str) {
|
|
4274
|
+
let result = str.trim();
|
|
4275
|
+
let i = 0;
|
|
4276
|
+
while (i < result.length) {
|
|
4277
|
+
const char = result[i];
|
|
4278
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
4279
|
+
const quote = char;
|
|
4280
|
+
i++;
|
|
4281
|
+
while (i < result.length && result[i] !== quote) {
|
|
4282
|
+
if (result[i] === "\\")
|
|
4283
|
+
i++;
|
|
4284
|
+
i++;
|
|
4285
|
+
}
|
|
4286
|
+
i++;
|
|
4287
|
+
continue;
|
|
4288
|
+
}
|
|
4289
|
+
if (char === "{" || char === "(" || char === "[") {
|
|
4290
|
+
let depth = 1;
|
|
4291
|
+
i++;
|
|
4292
|
+
while (i < result.length && depth > 0) {
|
|
4293
|
+
const c = result[i];
|
|
4294
|
+
if (c === "{" || c === "(" || c === "[")
|
|
4295
|
+
depth++;
|
|
4296
|
+
else if (c === "}" || c === ")" || c === "]")
|
|
4297
|
+
depth--;
|
|
4298
|
+
else if (c === '"' || c === "'" || c === "`") {
|
|
4299
|
+
const quote = c;
|
|
4300
|
+
i++;
|
|
4301
|
+
while (i < result.length && result[i] !== quote) {
|
|
4302
|
+
if (result[i] === "\\")
|
|
4303
|
+
i++;
|
|
4304
|
+
i++;
|
|
4305
|
+
}
|
|
4306
|
+
}
|
|
4307
|
+
i++;
|
|
4308
|
+
}
|
|
4309
|
+
continue;
|
|
4310
|
+
}
|
|
4311
|
+
if (char === "/" && result[i + 1] === "*") {
|
|
4312
|
+
result = result.slice(0, i).trim();
|
|
4313
|
+
break;
|
|
4314
|
+
}
|
|
4315
|
+
if (char === "/" && result[i + 1] === "/") {
|
|
4316
|
+
result = result.slice(0, i).trim();
|
|
4317
|
+
break;
|
|
4318
|
+
}
|
|
4319
|
+
i++;
|
|
4320
|
+
}
|
|
4321
|
+
return result;
|
|
4322
|
+
}
|
|
4199
4323
|
function generatePropsDestructuring(attrs, factoryName) {
|
|
4200
4324
|
const parts = [];
|
|
4201
4325
|
for (const attr2 of attrs) {
|
|
4202
4326
|
if (attr2.bindable) {
|
|
4203
4327
|
if (attr2.hasDefault) {
|
|
4204
|
-
parts.push(
|
|
4328
|
+
parts.push(generatePropDestructure(attr2.key, `$bindable(${attr2.defaultValue})`));
|
|
4205
4329
|
} else {
|
|
4206
|
-
parts.push(
|
|
4330
|
+
parts.push(generatePropDestructure(attr2.key, "$bindable()"));
|
|
4207
4331
|
}
|
|
4208
4332
|
} else if (attr2.hasDefault) {
|
|
4209
|
-
parts.push(
|
|
4333
|
+
parts.push(generatePropDestructure(attr2.key, attr2.defaultValue));
|
|
4210
4334
|
} else {
|
|
4211
|
-
parts.push(attr2.key);
|
|
4335
|
+
parts.push(generatePropDestructureNoDefault(attr2.key));
|
|
4212
4336
|
}
|
|
4213
4337
|
}
|
|
4214
4338
|
return `let { ${parts.join(", ")} }: $attrs.Of<typeof ${factoryName}> = $props()`;
|
|
@@ -4216,9 +4340,9 @@ function generatePropsDestructuring(attrs, factoryName) {
|
|
|
4216
4340
|
function generateFactoryCallFromAttrs(factoryName, attrs) {
|
|
4217
4341
|
const parts = [];
|
|
4218
4342
|
for (const attr2 of attrs) {
|
|
4219
|
-
parts.push(
|
|
4343
|
+
parts.push(generateGetter(attr2.key));
|
|
4220
4344
|
if (attr2.bindable) {
|
|
4221
|
-
parts.push(
|
|
4345
|
+
parts.push(generateSetter(attr2.key));
|
|
4222
4346
|
}
|
|
4223
4347
|
}
|
|
4224
4348
|
return `${factoryName}({ ${parts.join(", ")} })`;
|
|
@@ -5786,11 +5910,93 @@ function findPropsInjectionPosition(source2) {
|
|
|
5786
5910
|
}
|
|
5787
5911
|
|
|
5788
5912
|
// src/transform/attrs-schema.ts
|
|
5913
|
+
function stripLeadingComments(str) {
|
|
5914
|
+
let result = str.trim();
|
|
5915
|
+
while (true) {
|
|
5916
|
+
if (result.startsWith("/*")) {
|
|
5917
|
+
const endComment = result.indexOf("*/");
|
|
5918
|
+
if (endComment !== -1) {
|
|
5919
|
+
result = result.slice(endComment + 2).trim();
|
|
5920
|
+
continue;
|
|
5921
|
+
}
|
|
5922
|
+
break;
|
|
5923
|
+
}
|
|
5924
|
+
if (result.startsWith("//")) {
|
|
5925
|
+
const endLine = result.indexOf(`
|
|
5926
|
+
`);
|
|
5927
|
+
if (endLine !== -1) {
|
|
5928
|
+
result = result.slice(endLine + 1).trim();
|
|
5929
|
+
continue;
|
|
5930
|
+
}
|
|
5931
|
+
result = "";
|
|
5932
|
+
break;
|
|
5933
|
+
}
|
|
5934
|
+
break;
|
|
5935
|
+
}
|
|
5936
|
+
return result;
|
|
5937
|
+
}
|
|
5938
|
+
function stripTrailingComments2(str) {
|
|
5939
|
+
let result = str.trim();
|
|
5940
|
+
let i = 0;
|
|
5941
|
+
let lastValidEnd = result.length;
|
|
5942
|
+
while (i < result.length) {
|
|
5943
|
+
const char = result[i];
|
|
5944
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
5945
|
+
const quote = char;
|
|
5946
|
+
i++;
|
|
5947
|
+
while (i < result.length && result[i] !== quote) {
|
|
5948
|
+
if (result[i] === "\\")
|
|
5949
|
+
i++;
|
|
5950
|
+
i++;
|
|
5951
|
+
}
|
|
5952
|
+
i++;
|
|
5953
|
+
lastValidEnd = i;
|
|
5954
|
+
continue;
|
|
5955
|
+
}
|
|
5956
|
+
if (char === "{" || char === "(" || char === "[") {
|
|
5957
|
+
let depth = 1;
|
|
5958
|
+
i++;
|
|
5959
|
+
while (i < result.length && depth > 0) {
|
|
5960
|
+
const c = result[i];
|
|
5961
|
+
if (c === "{" || c === "(" || c === "[")
|
|
5962
|
+
depth++;
|
|
5963
|
+
else if (c === "}" || c === ")" || c === "]")
|
|
5964
|
+
depth--;
|
|
5965
|
+
else if (c === '"' || c === "'" || c === "`") {
|
|
5966
|
+
const quote = c;
|
|
5967
|
+
i++;
|
|
5968
|
+
while (i < result.length && result[i] !== quote) {
|
|
5969
|
+
if (result[i] === "\\")
|
|
5970
|
+
i++;
|
|
5971
|
+
i++;
|
|
5972
|
+
}
|
|
5973
|
+
}
|
|
5974
|
+
i++;
|
|
5975
|
+
}
|
|
5976
|
+
lastValidEnd = i;
|
|
5977
|
+
continue;
|
|
5978
|
+
}
|
|
5979
|
+
if (char === "/" && result[i + 1] === "*") {
|
|
5980
|
+
result = result.slice(0, i).trim();
|
|
5981
|
+
break;
|
|
5982
|
+
}
|
|
5983
|
+
if (char === "/" && result[i + 1] === "/") {
|
|
5984
|
+
result = result.slice(0, i).trim();
|
|
5985
|
+
break;
|
|
5986
|
+
}
|
|
5987
|
+
i++;
|
|
5988
|
+
lastValidEnd = i;
|
|
5989
|
+
}
|
|
5990
|
+
return result;
|
|
5991
|
+
}
|
|
5789
5992
|
function generateAttrSchemaFromSource(attrsSource) {
|
|
5790
5993
|
const entries = [];
|
|
5791
5994
|
const attrs = splitAttrsSource(attrsSource);
|
|
5792
5995
|
for (const attr2 of attrs) {
|
|
5793
|
-
|
|
5996
|
+
let trimmed = attr2.trim();
|
|
5997
|
+
if (!trimmed)
|
|
5998
|
+
continue;
|
|
5999
|
+
trimmed = stripLeadingComments(trimmed);
|
|
5794
6000
|
if (!trimmed)
|
|
5795
6001
|
continue;
|
|
5796
6002
|
const colonIndex = trimmed.indexOf(":");
|
|
@@ -5798,6 +6004,7 @@ function generateAttrSchemaFromSource(attrsSource) {
|
|
|
5798
6004
|
continue;
|
|
5799
6005
|
const key2 = trimmed.slice(0, colonIndex).trim();
|
|
5800
6006
|
let value = trimmed.slice(colonIndex + 1).trim();
|
|
6007
|
+
value = stripTrailingComments2(value);
|
|
5801
6008
|
const asIndex = findTopLevelAs(value);
|
|
5802
6009
|
if (asIndex !== -1) {
|
|
5803
6010
|
value = value.slice(0, asIndex).trim();
|
|
@@ -5821,24 +6028,7 @@ function parseAttrsSource(attrsSource) {
|
|
|
5821
6028
|
let trimmed = attr2.trim();
|
|
5822
6029
|
if (!trimmed)
|
|
5823
6030
|
continue;
|
|
5824
|
-
|
|
5825
|
-
const endComment = trimmed.indexOf("*/");
|
|
5826
|
-
if (endComment !== -1) {
|
|
5827
|
-
trimmed = trimmed.slice(endComment + 2).trim();
|
|
5828
|
-
} else {
|
|
5829
|
-
break;
|
|
5830
|
-
}
|
|
5831
|
-
}
|
|
5832
|
-
while (trimmed.startsWith("//")) {
|
|
5833
|
-
const endLine = trimmed.indexOf(`
|
|
5834
|
-
`);
|
|
5835
|
-
if (endLine !== -1) {
|
|
5836
|
-
trimmed = trimmed.slice(endLine + 1).trim();
|
|
5837
|
-
} else {
|
|
5838
|
-
trimmed = "";
|
|
5839
|
-
break;
|
|
5840
|
-
}
|
|
5841
|
-
}
|
|
6031
|
+
trimmed = stripLeadingComments(trimmed);
|
|
5842
6032
|
if (!trimmed)
|
|
5843
6033
|
continue;
|
|
5844
6034
|
const colonIndex = trimmed.indexOf(":");
|
|
@@ -5846,6 +6036,7 @@ function parseAttrsSource(attrsSource) {
|
|
|
5846
6036
|
continue;
|
|
5847
6037
|
const key2 = trimmed.slice(0, colonIndex).trim();
|
|
5848
6038
|
let value = trimmed.slice(colonIndex + 1).trim();
|
|
6039
|
+
value = stripTrailingComments2(value);
|
|
5849
6040
|
const asIndex = findTopLevelAs(value);
|
|
5850
6041
|
if (asIndex !== -1) {
|
|
5851
6042
|
value = value.slice(0, asIndex).trim();
|
|
@@ -6537,7 +6728,10 @@ function transformAttrsOriginCallsSync(s, source2, neededImports) {
|
|
|
6537
6728
|
let firstFactoryExpr = null;
|
|
6538
6729
|
let propsTypeDeclaration = null;
|
|
6539
6730
|
let isFirstDeclaration = true;
|
|
6540
|
-
const
|
|
6731
|
+
const originInstances = new Map;
|
|
6732
|
+
const existingPropsMatch = source2.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
6733
|
+
const hasExistingProps = !!existingPropsMatch;
|
|
6734
|
+
const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source2) && !hasExistingProps;
|
|
6541
6735
|
for (const decl of declarations) {
|
|
6542
6736
|
const { varName, startIndex, macroOpenParen } = decl;
|
|
6543
6737
|
const closeParenIndex = findMatchingBracket(source2, macroOpenParen);
|
|
@@ -6558,20 +6752,47 @@ function transformAttrsOriginCallsSync(s, source2, neededImports) {
|
|
|
6558
6752
|
expansionLines.push(`type $$Props = $attrs.Of<${typeExpr}>`);
|
|
6559
6753
|
propsTypeDeclaration = "";
|
|
6560
6754
|
}
|
|
6561
|
-
|
|
6755
|
+
if (hasExistingProps) {
|
|
6756
|
+
const existingContent = existingPropsMatch[1];
|
|
6757
|
+
const hasRestSpread = /\.\.\.\s*(\w+)\s*$/.test(existingContent);
|
|
6758
|
+
if (!hasRestSpread && isFirstDeclaration) {
|
|
6759
|
+
const restVarName = "___originAttrs";
|
|
6760
|
+
const newContent = existingContent.trimEnd() ? `${existingContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
6761
|
+
const braceEnd = source2.indexOf("}", existingPropsMatch.index + 5);
|
|
6762
|
+
if (braceEnd !== -1) {
|
|
6763
|
+
s.overwrite(existingPropsMatch.index + 5, braceEnd, ` ${newContent} `);
|
|
6764
|
+
}
|
|
6765
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVarName}))`);
|
|
6766
|
+
} else {
|
|
6767
|
+
const restMatch = existingContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
6768
|
+
const restVar = restMatch ? restMatch[1] : "___originAttrs";
|
|
6769
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVar}))`);
|
|
6770
|
+
}
|
|
6771
|
+
} else {
|
|
6772
|
+
expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
|
|
6773
|
+
}
|
|
6562
6774
|
s.overwrite(startIndex, endIndex, expansionLines.join(`
|
|
6563
6775
|
`));
|
|
6776
|
+
originInstances.set(varName, {
|
|
6777
|
+
varName,
|
|
6778
|
+
factoryExpr,
|
|
6779
|
+
startPos: startIndex,
|
|
6780
|
+
endPos: endIndex
|
|
6781
|
+
});
|
|
6564
6782
|
neededImports.add("__attrsFor");
|
|
6565
6783
|
changed = true;
|
|
6566
6784
|
isFirstDeclaration = false;
|
|
6567
6785
|
}
|
|
6568
|
-
return { changed, propsTypeDeclaration };
|
|
6786
|
+
return { changed, propsTypeDeclaration, originInstances };
|
|
6569
6787
|
}
|
|
6570
6788
|
async function transformAttrsOriginCalls(s, source2, neededImports, schemaResolver) {
|
|
6571
6789
|
const declarations = findVariableDeclarationsWithMacro(source2, "$attrs.origin");
|
|
6572
6790
|
let changed = false;
|
|
6573
6791
|
let propsTypeDeclaration = null;
|
|
6574
|
-
const
|
|
6792
|
+
const originInstances = new Map;
|
|
6793
|
+
const existingPropsMatch = source2.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
6794
|
+
const hasExistingProps = !!existingPropsMatch;
|
|
6795
|
+
const needsPropsInjection = declarations.length > 0 && !hasExistingPropsDeclaration(source2) && !hasExistingProps;
|
|
6575
6796
|
const matches = [];
|
|
6576
6797
|
for (const decl of declarations) {
|
|
6577
6798
|
const { varName, startIndex, macroOpenParen } = decl;
|
|
@@ -6591,6 +6812,7 @@ async function transformAttrsOriginCalls(s, source2, neededImports, schemaResolv
|
|
|
6591
6812
|
}
|
|
6592
6813
|
let firstFactoryExpr = null;
|
|
6593
6814
|
let isFirstDeclaration = true;
|
|
6815
|
+
let restVarInjected = false;
|
|
6594
6816
|
for (const { varName, factoryExpr, startIndex, endIndex, isGeneric } of matches) {
|
|
6595
6817
|
const expansionLines = [];
|
|
6596
6818
|
let usesFallback = true;
|
|
@@ -6618,15 +6840,40 @@ async function transformAttrsOriginCalls(s, source2, neededImports, schemaResolv
|
|
|
6618
6840
|
}
|
|
6619
6841
|
if (usesFallback) {
|
|
6620
6842
|
const normalizedFactoryCall = normalizeFactoryCall(factoryExpr);
|
|
6621
|
-
|
|
6843
|
+
if (hasExistingProps) {
|
|
6844
|
+
const existingContent = existingPropsMatch[1];
|
|
6845
|
+
const hasRestSpread = /\.\.\.\s*(\w+)\s*$/.test(existingContent);
|
|
6846
|
+
if (!hasRestSpread && !restVarInjected) {
|
|
6847
|
+
const restVarName = "___originAttrs";
|
|
6848
|
+
const newContent = existingContent.trimEnd() ? `${existingContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
6849
|
+
const braceEnd = source2.indexOf("}", existingPropsMatch.index + 5);
|
|
6850
|
+
if (braceEnd !== -1) {
|
|
6851
|
+
s.overwrite(existingPropsMatch.index + 5, braceEnd, ` ${newContent} `);
|
|
6852
|
+
restVarInjected = true;
|
|
6853
|
+
}
|
|
6854
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVarName}))`);
|
|
6855
|
+
} else {
|
|
6856
|
+
const restMatch = existingContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
6857
|
+
const restVar = restMatch ? restMatch[1] : "___originAttrs";
|
|
6858
|
+
expansionLines.push(`let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ${restVar}))`);
|
|
6859
|
+
}
|
|
6860
|
+
} else {
|
|
6861
|
+
expansionLines.push(`let ___attrs: $$Props = $props()`, `let ${varName} = ${normalizedFactoryCall}(__attrsFor(${normalizedFactoryCall}, ___attrs))`);
|
|
6862
|
+
}
|
|
6622
6863
|
neededImports.add("__attrsFor");
|
|
6623
6864
|
}
|
|
6624
6865
|
s.overwrite(startIndex, endIndex, expansionLines.join(`
|
|
6625
6866
|
`));
|
|
6867
|
+
originInstances.set(varName, {
|
|
6868
|
+
varName,
|
|
6869
|
+
factoryExpr,
|
|
6870
|
+
startPos: startIndex,
|
|
6871
|
+
endPos: endIndex
|
|
6872
|
+
});
|
|
6626
6873
|
changed = true;
|
|
6627
6874
|
isFirstDeclaration = false;
|
|
6628
6875
|
}
|
|
6629
|
-
return { changed, propsTypeDeclaration };
|
|
6876
|
+
return { changed, propsTypeDeclaration, originInstances };
|
|
6630
6877
|
}
|
|
6631
6878
|
|
|
6632
6879
|
// src/transform/element-types.ts
|
|
@@ -6638,9 +6885,9 @@ function getElementTypeImport(element2) {
|
|
|
6638
6885
|
function generateReactiveAttrsWrapper(attrs) {
|
|
6639
6886
|
const parts = [];
|
|
6640
6887
|
for (const attr2 of attrs) {
|
|
6641
|
-
parts.push(
|
|
6888
|
+
parts.push(generateGetter(attr2.key));
|
|
6642
6889
|
if (attr2.bindable) {
|
|
6643
|
-
parts.push(
|
|
6890
|
+
parts.push(generateSetter(attr2.key));
|
|
6644
6891
|
}
|
|
6645
6892
|
}
|
|
6646
6893
|
return `{ ${parts.join(", ")} }`;
|
|
@@ -6650,14 +6897,14 @@ function generateAttrsForMerge(attrs) {
|
|
|
6650
6897
|
for (const attr2 of attrs) {
|
|
6651
6898
|
if (attr2.bindable) {
|
|
6652
6899
|
if (attr2.hasDefault) {
|
|
6653
|
-
parts.push(
|
|
6900
|
+
parts.push(generatePropDestructure(attr2.key, `$bindable(${attr2.defaultValue})`));
|
|
6654
6901
|
} else {
|
|
6655
|
-
parts.push(
|
|
6902
|
+
parts.push(generatePropDestructure(attr2.key, "$bindable()"));
|
|
6656
6903
|
}
|
|
6657
6904
|
} else if (attr2.hasDefault) {
|
|
6658
|
-
parts.push(
|
|
6905
|
+
parts.push(generatePropDestructure(attr2.key, attr2.defaultValue));
|
|
6659
6906
|
} else {
|
|
6660
|
-
parts.push(attr2.key);
|
|
6907
|
+
parts.push(generatePropDestructureNoDefault(attr2.key));
|
|
6661
6908
|
}
|
|
6662
6909
|
}
|
|
6663
6910
|
return parts.join(", ");
|
|
@@ -6834,6 +7081,291 @@ async function transformAttrsForCalls(s, source2, neededImports, schemaResolver)
|
|
|
6834
7081
|
return changed;
|
|
6835
7082
|
}
|
|
6836
7083
|
|
|
7084
|
+
// src/transform/origin-destructure-transform.ts
|
|
7085
|
+
function transformOriginDestructuring(s, source2, originInstances) {
|
|
7086
|
+
let changed = false;
|
|
7087
|
+
const destructures = findOriginDestructures(source2, originInstances);
|
|
7088
|
+
for (const d of destructures.reverse()) {
|
|
7089
|
+
const replacement = generateDestructureReplacement(d, originInstances);
|
|
7090
|
+
if (replacement) {
|
|
7091
|
+
s.overwrite(d.startPos, d.endPos, replacement);
|
|
7092
|
+
changed = true;
|
|
7093
|
+
}
|
|
7094
|
+
}
|
|
7095
|
+
return changed;
|
|
7096
|
+
}
|
|
7097
|
+
function findOriginDestructures(source2, originInstances) {
|
|
7098
|
+
const results = [];
|
|
7099
|
+
const destructurePattern = /\b(let|const|var)\s*\{/g;
|
|
7100
|
+
let match;
|
|
7101
|
+
while ((match = destructurePattern.exec(source2)) !== null) {
|
|
7102
|
+
if (isInsideStringOrComment(source2, match.index))
|
|
7103
|
+
continue;
|
|
7104
|
+
const declKeyword = match[1];
|
|
7105
|
+
const braceStart = match.index + match[0].length - 1;
|
|
7106
|
+
const braceEnd = findMatchingBracket(source2, braceStart, "{", "}");
|
|
7107
|
+
if (braceEnd === -1)
|
|
7108
|
+
continue;
|
|
7109
|
+
let i = braceEnd + 1;
|
|
7110
|
+
while (i < source2.length && /\s/.test(source2[i]))
|
|
7111
|
+
i++;
|
|
7112
|
+
if (source2[i] !== "=")
|
|
7113
|
+
continue;
|
|
7114
|
+
i++;
|
|
7115
|
+
while (i < source2.length && /\s/.test(source2[i]))
|
|
7116
|
+
i++;
|
|
7117
|
+
const exprStart = i;
|
|
7118
|
+
let sourceVar = "";
|
|
7119
|
+
while (i < source2.length && /[\w$]/.test(source2[i])) {
|
|
7120
|
+
sourceVar += source2[i];
|
|
7121
|
+
i++;
|
|
7122
|
+
}
|
|
7123
|
+
if (!sourceVar)
|
|
7124
|
+
continue;
|
|
7125
|
+
if (!originInstances.has(sourceVar))
|
|
7126
|
+
continue;
|
|
7127
|
+
let isPropsAccess = false;
|
|
7128
|
+
const afterVarPos = i;
|
|
7129
|
+
while (i < source2.length && /\s/.test(source2[i]))
|
|
7130
|
+
i++;
|
|
7131
|
+
if (source2[i] === ".") {
|
|
7132
|
+
i++;
|
|
7133
|
+
while (i < source2.length && /\s/.test(source2[i]))
|
|
7134
|
+
i++;
|
|
7135
|
+
const propName = readIdentifier(source2, i);
|
|
7136
|
+
if (propName === "props") {
|
|
7137
|
+
isPropsAccess = true;
|
|
7138
|
+
i += propName.length;
|
|
7139
|
+
} else {
|
|
7140
|
+
continue;
|
|
7141
|
+
}
|
|
7142
|
+
}
|
|
7143
|
+
let endPos = i;
|
|
7144
|
+
while (endPos < source2.length && /\s/.test(source2[endPos]) && source2[endPos] !== `
|
|
7145
|
+
`)
|
|
7146
|
+
endPos++;
|
|
7147
|
+
if (source2[endPos] === ";")
|
|
7148
|
+
endPos++;
|
|
7149
|
+
if (source2[endPos] === `
|
|
7150
|
+
`)
|
|
7151
|
+
endPos++;
|
|
7152
|
+
const patternContent = source2.slice(braceStart + 1, braceEnd);
|
|
7153
|
+
const parsed = parseDestructurePattern(patternContent, isPropsAccess);
|
|
7154
|
+
results.push({
|
|
7155
|
+
startPos: match.index,
|
|
7156
|
+
endPos,
|
|
7157
|
+
sourceVar,
|
|
7158
|
+
isPropsAccess,
|
|
7159
|
+
methods: parsed.methods,
|
|
7160
|
+
props: parsed.props,
|
|
7161
|
+
nestedPropsPattern: parsed.nestedProps
|
|
7162
|
+
});
|
|
7163
|
+
}
|
|
7164
|
+
return results;
|
|
7165
|
+
}
|
|
7166
|
+
function parseDestructurePattern(content, isPropsAccess) {
|
|
7167
|
+
const methods = [];
|
|
7168
|
+
const props = [];
|
|
7169
|
+
let nestedProps = null;
|
|
7170
|
+
const parts = splitByTopLevelCommas2(content);
|
|
7171
|
+
for (const part of parts) {
|
|
7172
|
+
const trimmed = part.trim();
|
|
7173
|
+
if (!trimmed)
|
|
7174
|
+
continue;
|
|
7175
|
+
const propsMatch = trimmed.match(/^props\s*:\s*\{/);
|
|
7176
|
+
if (propsMatch) {
|
|
7177
|
+
const braceStart = trimmed.indexOf("{");
|
|
7178
|
+
const braceEnd = findMatchingBracket(trimmed, braceStart, "{", "}");
|
|
7179
|
+
if (braceEnd !== -1) {
|
|
7180
|
+
const nestedContent = trimmed.slice(braceStart + 1, braceEnd);
|
|
7181
|
+
const nestedParts = splitByTopLevelCommas2(nestedContent);
|
|
7182
|
+
const nestedPropsList = [];
|
|
7183
|
+
for (const np of nestedParts) {
|
|
7184
|
+
const parsed = parseDestructuredProp(np.trim());
|
|
7185
|
+
if (parsed)
|
|
7186
|
+
nestedPropsList.push(parsed);
|
|
7187
|
+
}
|
|
7188
|
+
nestedProps = {
|
|
7189
|
+
startPos: 0,
|
|
7190
|
+
endPos: 0,
|
|
7191
|
+
props: nestedPropsList
|
|
7192
|
+
};
|
|
7193
|
+
}
|
|
7194
|
+
continue;
|
|
7195
|
+
}
|
|
7196
|
+
if (isPropsAccess) {
|
|
7197
|
+
const parsed = parseDestructuredProp(trimmed);
|
|
7198
|
+
if (parsed)
|
|
7199
|
+
props.push(parsed);
|
|
7200
|
+
} else {
|
|
7201
|
+
const parsed = parseDestructuredItem(trimmed);
|
|
7202
|
+
if (parsed)
|
|
7203
|
+
methods.push(parsed);
|
|
7204
|
+
}
|
|
7205
|
+
}
|
|
7206
|
+
return { methods, props, nestedProps };
|
|
7207
|
+
}
|
|
7208
|
+
function parseDestructuredItem(part) {
|
|
7209
|
+
const trimmed = part.trim();
|
|
7210
|
+
if (!trimmed)
|
|
7211
|
+
return null;
|
|
7212
|
+
const colonIdx = findTopLevelColon(trimmed);
|
|
7213
|
+
let key2;
|
|
7214
|
+
let alias = null;
|
|
7215
|
+
let rest = trimmed;
|
|
7216
|
+
if (colonIdx !== -1) {
|
|
7217
|
+
key2 = trimmed.slice(0, colonIdx).trim();
|
|
7218
|
+
rest = trimmed.slice(colonIdx + 1).trim();
|
|
7219
|
+
const eqIdx2 = findTopLevelEquals(rest);
|
|
7220
|
+
if (eqIdx2 !== -1) {
|
|
7221
|
+
alias = rest.slice(0, eqIdx2).trim();
|
|
7222
|
+
return {
|
|
7223
|
+
key: key2,
|
|
7224
|
+
alias,
|
|
7225
|
+
defaultValue: rest.slice(eqIdx2 + 1).trim()
|
|
7226
|
+
};
|
|
7227
|
+
} else {
|
|
7228
|
+
alias = rest;
|
|
7229
|
+
return { key: key2, alias, defaultValue: null };
|
|
7230
|
+
}
|
|
7231
|
+
}
|
|
7232
|
+
const eqIdx = findTopLevelEquals(trimmed);
|
|
7233
|
+
if (eqIdx !== -1) {
|
|
7234
|
+
key2 = trimmed.slice(0, eqIdx).trim();
|
|
7235
|
+
return {
|
|
7236
|
+
key: key2,
|
|
7237
|
+
alias: null,
|
|
7238
|
+
defaultValue: trimmed.slice(eqIdx + 1).trim()
|
|
7239
|
+
};
|
|
7240
|
+
}
|
|
7241
|
+
return { key: trimmed, alias: null, defaultValue: null };
|
|
7242
|
+
}
|
|
7243
|
+
function parseDestructuredProp(part) {
|
|
7244
|
+
const item = parseDestructuredItem(part);
|
|
7245
|
+
if (!item)
|
|
7246
|
+
return null;
|
|
7247
|
+
let isBindable2 = false;
|
|
7248
|
+
let bindableDefault = null;
|
|
7249
|
+
if (item.defaultValue) {
|
|
7250
|
+
const bindableMatch = item.defaultValue.match(/^\$bindable\s*\(/);
|
|
7251
|
+
if (bindableMatch) {
|
|
7252
|
+
isBindable2 = true;
|
|
7253
|
+
const parenStart = item.defaultValue.indexOf("(");
|
|
7254
|
+
const parenEnd = findMatchingBracket(item.defaultValue, parenStart, "(", ")");
|
|
7255
|
+
if (parenEnd !== -1) {
|
|
7256
|
+
bindableDefault = item.defaultValue.slice(parenStart + 1, parenEnd).trim() || null;
|
|
7257
|
+
}
|
|
7258
|
+
}
|
|
7259
|
+
}
|
|
7260
|
+
return {
|
|
7261
|
+
...item,
|
|
7262
|
+
isBindable: isBindable2,
|
|
7263
|
+
bindableDefault
|
|
7264
|
+
};
|
|
7265
|
+
}
|
|
7266
|
+
function generateDestructureReplacement(d, originInstances) {
|
|
7267
|
+
const lines = [];
|
|
7268
|
+
const instance = originInstances.get(d.sourceVar);
|
|
7269
|
+
if (!instance)
|
|
7270
|
+
return null;
|
|
7271
|
+
for (const m of d.methods) {
|
|
7272
|
+
const varName = m.alias || m.key;
|
|
7273
|
+
if (m.defaultValue) {
|
|
7274
|
+
lines.push(`let ${varName} = ${d.sourceVar}.${m.key}?.bind(${d.sourceVar}) ?? ${m.defaultValue}`);
|
|
7275
|
+
} else {
|
|
7276
|
+
lines.push(`let ${varName} = ${d.sourceVar}.${m.key}.bind(${d.sourceVar})`);
|
|
7277
|
+
}
|
|
7278
|
+
}
|
|
7279
|
+
if (d.nestedPropsPattern) {
|
|
7280
|
+
for (const p of d.nestedPropsPattern.props) {
|
|
7281
|
+
lines.push(...generatePropAccessors(p, d.sourceVar));
|
|
7282
|
+
}
|
|
7283
|
+
}
|
|
7284
|
+
if (d.isPropsAccess) {
|
|
7285
|
+
for (const p of d.props) {
|
|
7286
|
+
lines.push(...generatePropAccessors(p, d.sourceVar));
|
|
7287
|
+
}
|
|
7288
|
+
}
|
|
7289
|
+
return lines.join(`
|
|
7290
|
+
`) + `
|
|
7291
|
+
`;
|
|
7292
|
+
}
|
|
7293
|
+
function generatePropAccessors(p, sourceVar) {
|
|
7294
|
+
const lines = [];
|
|
7295
|
+
const varName = p.alias || p.key;
|
|
7296
|
+
if (p.isBindable) {
|
|
7297
|
+
const defaultVal = p.bindableDefault || "undefined";
|
|
7298
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key} ?? ${defaultVal})`);
|
|
7299
|
+
} else if (p.defaultValue) {
|
|
7300
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key} ?? ${p.defaultValue})`);
|
|
7301
|
+
} else {
|
|
7302
|
+
lines.push(`let ${varName} = $derived(${sourceVar}.props.${p.key})`);
|
|
7303
|
+
}
|
|
7304
|
+
return lines;
|
|
7305
|
+
}
|
|
7306
|
+
function splitByTopLevelCommas2(content) {
|
|
7307
|
+
const parts = [];
|
|
7308
|
+
let current = "";
|
|
7309
|
+
let depth = 0;
|
|
7310
|
+
for (let i = 0;i < content.length; i++) {
|
|
7311
|
+
const char = content[i];
|
|
7312
|
+
if (char === "{" || char === "(" || char === "[" || char === "<") {
|
|
7313
|
+
depth++;
|
|
7314
|
+
current += char;
|
|
7315
|
+
} else if (char === "}" || char === ")" || char === "]" || char === ">") {
|
|
7316
|
+
depth--;
|
|
7317
|
+
current += char;
|
|
7318
|
+
} else if (char === "," && depth === 0) {
|
|
7319
|
+
parts.push(current);
|
|
7320
|
+
current = "";
|
|
7321
|
+
} else {
|
|
7322
|
+
current += char;
|
|
7323
|
+
}
|
|
7324
|
+
}
|
|
7325
|
+
if (current.trim()) {
|
|
7326
|
+
parts.push(current);
|
|
7327
|
+
}
|
|
7328
|
+
return parts;
|
|
7329
|
+
}
|
|
7330
|
+
function findTopLevelColon(str) {
|
|
7331
|
+
let depth = 0;
|
|
7332
|
+
for (let i = 0;i < str.length; i++) {
|
|
7333
|
+
const char = str[i];
|
|
7334
|
+
if (char === "{" || char === "(" || char === "[" || char === "<")
|
|
7335
|
+
depth++;
|
|
7336
|
+
else if (char === "}" || char === ")" || char === "]" || char === ">")
|
|
7337
|
+
depth--;
|
|
7338
|
+
else if (char === ":" && depth === 0)
|
|
7339
|
+
return i;
|
|
7340
|
+
}
|
|
7341
|
+
return -1;
|
|
7342
|
+
}
|
|
7343
|
+
function findTopLevelEquals(str) {
|
|
7344
|
+
let depth = 0;
|
|
7345
|
+
for (let i = 0;i < str.length; i++) {
|
|
7346
|
+
const char = str[i];
|
|
7347
|
+
if (char === "{" || char === "(" || char === "[" || char === "<")
|
|
7348
|
+
depth++;
|
|
7349
|
+
else if (char === "}" || char === ")" || char === "]" || char === ">")
|
|
7350
|
+
depth--;
|
|
7351
|
+
else if (char === "=" && depth === 0) {
|
|
7352
|
+
if (str[i + 1] !== "=" && str[i + 1] !== ">") {
|
|
7353
|
+
return i;
|
|
7354
|
+
}
|
|
7355
|
+
}
|
|
7356
|
+
}
|
|
7357
|
+
return -1;
|
|
7358
|
+
}
|
|
7359
|
+
function readIdentifier(str, start) {
|
|
7360
|
+
let result = "";
|
|
7361
|
+
let i = start;
|
|
7362
|
+
while (i < str.length && /[\w$]/.test(str[i])) {
|
|
7363
|
+
result += str[i];
|
|
7364
|
+
i++;
|
|
7365
|
+
}
|
|
7366
|
+
return result;
|
|
7367
|
+
}
|
|
7368
|
+
|
|
6837
7369
|
// src/transform/core.ts
|
|
6838
7370
|
function transformScript(source2, options = {}) {
|
|
6839
7371
|
if (options.schemaResolver) {
|
|
@@ -6856,6 +7388,9 @@ function transformScript(source2, options = {}) {
|
|
|
6856
7388
|
const result = transformAttrsOriginCallsSync(s, source2, neededImports);
|
|
6857
7389
|
changed = result.changed || changed;
|
|
6858
7390
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
7391
|
+
if (result.originInstances.size > 0) {
|
|
7392
|
+
changed = transformOriginDestructuring(s, source2, result.originInstances) || changed;
|
|
7393
|
+
}
|
|
6859
7394
|
}
|
|
6860
7395
|
if (isComponent) {
|
|
6861
7396
|
changed = transformAttrsForCallsSync(s, source2, neededImports) || changed;
|
|
@@ -6927,6 +7462,9 @@ async function transformScriptAsync(source2, options) {
|
|
|
6927
7462
|
const result = await transformAttrsOriginCalls(s, source2, neededImports, schemaResolver);
|
|
6928
7463
|
changed = result.changed || changed;
|
|
6929
7464
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
7465
|
+
if (result.originInstances.size > 0) {
|
|
7466
|
+
changed = transformOriginDestructuring(s, source2, result.originInstances) || changed;
|
|
7467
|
+
}
|
|
6930
7468
|
}
|
|
6931
7469
|
if (isComponent) {
|
|
6932
7470
|
changed = await transformAttrsForCalls(s, source2, neededImports, schemaResolver) || changed;
|
|
@@ -6997,6 +7535,9 @@ async function transformScriptContent(source2, options) {
|
|
|
6997
7535
|
const result = await transformAttrsOriginCalls(s, source2, neededImports, schemaResolver);
|
|
6998
7536
|
changed = result.changed || changed;
|
|
6999
7537
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
7538
|
+
if (result.originInstances.size > 0) {
|
|
7539
|
+
changed = transformOriginDestructuring(s, source2, result.originInstances) || changed;
|
|
7540
|
+
}
|
|
7000
7541
|
}
|
|
7001
7542
|
if (isComponent) {
|
|
7002
7543
|
changed = await transformAttrsForCalls(s, source2, neededImports, schemaResolver) || changed;
|