svelte-origin 1.0.0-next.21 → 1.0.0-next.23

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.
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 base = {
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
- Object.defineProperty(wrapper, key2, {
3492
- ...base,
3493
- set(value) {
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
- try {
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() {
@@ -3899,6 +3873,73 @@ function parseOriginSchemaFromSource(source2, exportName) {
3899
3873
  return compiledResult;
3900
3874
  return null;
3901
3875
  }
3876
+ function parseAttrsSchemaFromSource(source2, exportName) {
3877
+ const sourceResult = parseSourceAttrs(source2, exportName);
3878
+ if (sourceResult)
3879
+ return sourceResult;
3880
+ const compiledResult = parseCompiledAttrs(source2, exportName);
3881
+ if (compiledResult)
3882
+ return compiledResult;
3883
+ return null;
3884
+ }
3885
+ function parseSourceAttrs(source2, exportName) {
3886
+ const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*\\$attrs\\s*\\(`, "s");
3887
+ const match = exportPattern.exec(source2);
3888
+ if (!match)
3889
+ return null;
3890
+ const attrsStart = match.index + match[0].length - 1;
3891
+ const attrsEnd = findMatchingBracket(source2, attrsStart);
3892
+ if (attrsEnd === -1)
3893
+ return null;
3894
+ let attrsContent = source2.slice(attrsStart + 1, attrsEnd).trim();
3895
+ let parentNames = [];
3896
+ if (attrsContent.startsWith("[")) {
3897
+ const closeBracket = findMatchingBracket(attrsContent, 0, "[", "]");
3898
+ if (closeBracket !== -1) {
3899
+ const parentsContent = attrsContent.slice(1, closeBracket);
3900
+ parentNames = parentsContent.split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
3901
+ let i = closeBracket + 1;
3902
+ while (i < attrsContent.length && /[\s,]/.test(attrsContent[i]))
3903
+ i++;
3904
+ attrsContent = attrsContent.slice(i);
3905
+ }
3906
+ }
3907
+ if (!attrsContent.startsWith("{")) {
3908
+ return { attrs: [], parentNames };
3909
+ }
3910
+ const objEnd = findMatchingBracket(attrsContent, 0, "{", "}");
3911
+ if (objEnd === -1)
3912
+ return { attrs: [], parentNames };
3913
+ const objContent = attrsContent.slice(1, objEnd);
3914
+ const attrs = parseAttrsContent(objContent);
3915
+ return { attrs, parentNames };
3916
+ }
3917
+ function parseCompiledAttrs(source2, exportName) {
3918
+ const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*__createAttrs\\s*\\(\\s*\\{`, "s");
3919
+ const match = exportPattern.exec(source2);
3920
+ if (!match)
3921
+ return null;
3922
+ const braceStart = match.index + match[0].length - 1;
3923
+ const braceEnd = findMatchingBracket(source2, braceStart, "{", "}");
3924
+ if (braceEnd === -1)
3925
+ return null;
3926
+ const configContent = source2.slice(braceStart + 1, braceEnd);
3927
+ const schemaMatch = configContent.match(/__attrSchema\s*:\s*\{/);
3928
+ if (!schemaMatch)
3929
+ return { attrs: [], parentNames: [] };
3930
+ const schemaStart = schemaMatch.index + schemaMatch[0].length - 1;
3931
+ const schemaEnd = findMatchingBracket(configContent, schemaStart, "{", "}");
3932
+ if (schemaEnd === -1)
3933
+ return { attrs: [], parentNames: [] };
3934
+ const schemaContent = configContent.slice(schemaStart + 1, schemaEnd);
3935
+ const attrs = parseCompiledAttrSchema(schemaContent);
3936
+ const parentsMatch = configContent.match(/__parents\s*:\s*\[([^\]]*)\]/);
3937
+ let parentNames = [];
3938
+ if (parentsMatch && parentsMatch[1].trim()) {
3939
+ parentNames = parentsMatch[1].split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
3940
+ }
3941
+ return { attrs, parentNames };
3942
+ }
3902
3943
  function parseSourceOrigin(source2, exportName) {
3903
3944
  const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*\\$origin\\s*\\(`, "s");
3904
3945
  const match = exportPattern.exec(source2);
@@ -3924,6 +3965,13 @@ function parseSourceOrigin(source2, exportName) {
3924
3965
  }
3925
3966
  const attrsMatch = bodyContent.match(/(\w+)\s*:\s*\$attrs\s*\(\s*\{/);
3926
3967
  if (!attrsMatch) {
3968
+ const externalRefMatch = bodyContent.match(/(?:props|attrs)\s*:\s*([A-Z]\w*)\b/);
3969
+ if (externalRefMatch) {
3970
+ const propsRef = externalRefMatch[1];
3971
+ if (!["Object", "Array", "String", "Number", "Boolean"].includes(propsRef)) {
3972
+ return { attrs: [], parentNames, propsRef };
3973
+ }
3974
+ }
3927
3975
  return { attrs: [], parentNames };
3928
3976
  }
3929
3977
  const attrsStart = attrsMatch.index;
@@ -3960,7 +4008,9 @@ function parseCompiledOrigin(source2, exportName) {
3960
4008
  if (parentsMatch && parentsMatch[1].trim()) {
3961
4009
  parentNames = parentsMatch[1].split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
3962
4010
  }
3963
- return { attrs, parentNames };
4011
+ const propsRefMatch = configContent.match(/__propsRef\s*:\s*(\w+)/);
4012
+ const propsRef = propsRefMatch ? propsRefMatch[1] : undefined;
4013
+ return { attrs, parentNames, propsRef };
3964
4014
  }
3965
4015
  function parseCompiledAttrSchema(content) {
3966
4016
  const result = [];
@@ -6160,6 +6210,7 @@ function transformOriginDefinition(content, svelteImports) {
6160
6210
  let attrsContent = "";
6161
6211
  let contentWithoutAttrs = bodyContent;
6162
6212
  let attrPropertyName = "props";
6213
+ let propsRef = null;
6163
6214
  if (attrsMatch) {
6164
6215
  attrPropertyName = attrsMatch[1];
6165
6216
  const attrsStart = attrsMatch.index;
@@ -6190,13 +6241,31 @@ function transformOriginDefinition(content, svelteImports) {
6190
6241
  }
6191
6242
  contentWithoutAttrs = bodyContent.slice(0, attrsStart) + bodyContent.slice(cutEnd);
6192
6243
  }
6244
+ } else {
6245
+ const externalRefMatch = bodyContent.match(/(\w+)\s*:\s*(\w+)\s*,/);
6246
+ if (externalRefMatch) {
6247
+ const propName = externalRefMatch[1];
6248
+ const refName = externalRefMatch[2];
6249
+ if ((propName === "props" || propName === "attrs") && /^[A-Z]/.test(refName) && !["$state", "$derived", "$effect", "$bindable"].includes(refName)) {
6250
+ attrPropertyName = propName;
6251
+ propsRef = refName;
6252
+ const refStart = externalRefMatch.index;
6253
+ const refEnd = refStart + externalRefMatch[0].length;
6254
+ contentWithoutAttrs = bodyContent.slice(0, refStart) + bodyContent.slice(refEnd);
6255
+ }
6256
+ }
6193
6257
  }
6194
6258
  const attrDetails = parseAttrsSource(attrsContent);
6195
6259
  const { body: createFnBody, attachments } = transformOriginBody(contentWithoutAttrs.trim(), parents, attrDetails, attrPropertyName, svelteImports);
6196
6260
  const parentsCode = parents.length > 0 ? `[${parents.join(", ")}]` : "undefined";
6197
6261
  let configCode = `{
6198
6262
  __attrSchema: ${attrSchemaCode},
6199
- __parents: ${parentsCode},
6263
+ __parents: ${parentsCode},`;
6264
+ if (propsRef) {
6265
+ configCode += `
6266
+ __propsRef: ${propsRef},`;
6267
+ }
6268
+ configCode += `
6200
6269
  __create: (__inputAttrs${parents.length > 0 ? ", __super" : ""}) => {
6201
6270
  ${createFnBody}
6202
6271
  }`;
@@ -6566,7 +6635,34 @@ function getElementTypeImport(element2) {
6566
6635
  }
6567
6636
 
6568
6637
  // src/transform/attrs-for-transform.ts
6569
- function transformAttrsForCalls(s, source2, neededImports) {
6638
+ function generateReactiveAttrsWrapper(attrs) {
6639
+ const parts = [];
6640
+ for (const attr2 of attrs) {
6641
+ parts.push(`get ${attr2.key}() { return ${attr2.key} }`);
6642
+ if (attr2.bindable) {
6643
+ parts.push(`set ${attr2.key}(v) { ${attr2.key} = v }`);
6644
+ }
6645
+ }
6646
+ return `{ ${parts.join(", ")} }`;
6647
+ }
6648
+ function generateAttrsForMerge(attrs) {
6649
+ const parts = [];
6650
+ for (const attr2 of attrs) {
6651
+ if (attr2.bindable) {
6652
+ if (attr2.hasDefault) {
6653
+ parts.push(`${attr2.key} = $bindable(${attr2.defaultValue})`);
6654
+ } else {
6655
+ parts.push(`${attr2.key} = $bindable()`);
6656
+ }
6657
+ } else if (attr2.hasDefault) {
6658
+ parts.push(`${attr2.key} = ${attr2.defaultValue}`);
6659
+ } else {
6660
+ parts.push(attr2.key);
6661
+ }
6662
+ }
6663
+ return parts.join(", ");
6664
+ }
6665
+ function transformAttrsForCallsSync(s, source2, neededImports) {
6570
6666
  let changed = false;
6571
6667
  const existingPropsMatch = source2.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
6572
6668
  let restVarName = null;
@@ -6632,6 +6728,111 @@ function transformAttrsForCalls(s, source2, neededImports) {
6632
6728
  }
6633
6729
  return changed;
6634
6730
  }
6731
+ async function transformAttrsForCalls(s, source2, neededImports, schemaResolver) {
6732
+ let changed = false;
6733
+ const existingPropsMatch = source2.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
6734
+ let restVarName = null;
6735
+ let existingPropsStart = -1;
6736
+ let existingDestructureContent = "";
6737
+ if (existingPropsMatch) {
6738
+ existingPropsStart = existingPropsMatch.index;
6739
+ existingDestructureContent = existingPropsMatch[1];
6740
+ const restMatch = existingDestructureContent.match(/\.\.\.\s*(\w+)\s*$/);
6741
+ if (restMatch) {
6742
+ restVarName = restMatch[1];
6743
+ }
6744
+ }
6745
+ const declarations = findVariableDeclarationsWithMacro(source2, "$attrs.for");
6746
+ for (const decl of declarations) {
6747
+ const { varName, startIndex, macroOpenParen } = decl;
6748
+ const closeParenIndex = findMatchingBracket(source2, macroOpenParen);
6749
+ if (closeParenIndex === -1)
6750
+ continue;
6751
+ const arg = source2.slice(macroOpenParen + 1, closeParenIndex).trim();
6752
+ const endIndex = closeParenIndex + 1;
6753
+ const stringMatch = arg.match(/^['"](\w+)['"]$/);
6754
+ if (stringMatch) {
6755
+ const element2 = stringMatch[1];
6756
+ const typeImport = getElementTypeImport(element2);
6757
+ if (existingPropsMatch && existingPropsStart !== -1) {
6758
+ if (!restVarName) {
6759
+ restVarName = "___restAttrs";
6760
+ const newDestructure = existingDestructureContent.trimEnd() ? `${existingDestructureContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
6761
+ const braceEnd = source2.indexOf("}", existingPropsStart + 5);
6762
+ if (braceEnd !== -1) {
6763
+ s.overwrite(existingPropsStart + 5, braceEnd, ` ${newDestructure} `);
6764
+ }
6765
+ }
6766
+ s.overwrite(startIndex, endIndex, `let ${varName} = ${restVarName} as ${typeImport}`);
6767
+ } else {
6768
+ const expansion = `let { ...${varName} } = $props<${typeImport}>()`;
6769
+ s.overwrite(startIndex, endIndex, expansion);
6770
+ }
6771
+ } else {
6772
+ const factoryName = arg;
6773
+ let transformed = false;
6774
+ if (schemaResolver) {
6775
+ const importPath = findImportPath(source2, factoryName);
6776
+ if (importPath) {
6777
+ const schema = await schemaResolver.resolve(importPath, factoryName);
6778
+ if (schema && schema.attrs.length > 0) {
6779
+ if (existingPropsMatch && existingPropsStart !== -1) {
6780
+ const additionalAttrs = generateAttrsForMerge(schema.attrs);
6781
+ const restMatch = existingDestructureContent.match(/,?\s*\.\.\.\s*(\w+)\s*$/);
6782
+ let baseContent = existingDestructureContent;
6783
+ let restPart = "";
6784
+ if (restMatch) {
6785
+ restPart = restMatch[0];
6786
+ baseContent = existingDestructureContent.slice(0, restMatch.index);
6787
+ }
6788
+ const mergedDestructure = baseContent.trimEnd() ? `${baseContent.trimEnd()}, ${additionalAttrs}${restPart}` : `${additionalAttrs}${restPart}`;
6789
+ const braceEnd = source2.indexOf("}", existingPropsStart + 5);
6790
+ if (braceEnd !== -1) {
6791
+ s.overwrite(existingPropsStart + 5, braceEnd, ` ${mergedDestructure} `);
6792
+ }
6793
+ const wrapper = generateReactiveAttrsWrapper(schema.attrs);
6794
+ s.overwrite(startIndex, endIndex, `let ${varName} = __attrsFor(${factoryName}, ${wrapper})`);
6795
+ transformed = true;
6796
+ } else {
6797
+ const propsDestructure = generatePropsDestructuring(schema.attrs, factoryName);
6798
+ const wrapper = generateReactiveAttrsWrapper(schema.attrs);
6799
+ const expansion = [
6800
+ propsDestructure,
6801
+ `let ${varName} = __attrsFor(${factoryName}, ${wrapper})`
6802
+ ].join(`
6803
+ `);
6804
+ s.overwrite(startIndex, endIndex, expansion);
6805
+ transformed = true;
6806
+ }
6807
+ }
6808
+ }
6809
+ }
6810
+ if (!transformed) {
6811
+ if (existingPropsMatch && existingPropsStart !== -1) {
6812
+ if (!restVarName) {
6813
+ restVarName = "___restAttrs";
6814
+ const newDestructure = existingDestructureContent.trimEnd() ? `${existingDestructureContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
6815
+ const braceEnd = source2.indexOf("}", existingPropsStart + 5);
6816
+ if (braceEnd !== -1) {
6817
+ s.overwrite(existingPropsStart + 5, braceEnd, ` ${newDestructure} `);
6818
+ }
6819
+ }
6820
+ s.overwrite(startIndex, endIndex, `let ${varName} = __attrsFor(${factoryName}, ${restVarName})`);
6821
+ } else {
6822
+ const expansion = [
6823
+ `let ___attrs: $attrs.Of<typeof ${factoryName}> = $props()`,
6824
+ `let ${varName} = __attrsFor(${factoryName}, ___attrs)`
6825
+ ].join(`
6826
+ `);
6827
+ s.overwrite(startIndex, endIndex, expansion);
6828
+ }
6829
+ }
6830
+ neededImports.add("__attrsFor");
6831
+ }
6832
+ changed = true;
6833
+ }
6834
+ return changed;
6835
+ }
6635
6836
 
6636
6837
  // src/transform/core.ts
6637
6838
  function transformScript(source2, options = {}) {
@@ -6657,7 +6858,7 @@ function transformScript(source2, options = {}) {
6657
6858
  propsTypeDeclaration = result.propsTypeDeclaration;
6658
6859
  }
6659
6860
  if (isComponent) {
6660
- changed = transformAttrsForCalls(s, source2, neededImports) || changed;
6861
+ changed = transformAttrsForCallsSync(s, source2, neededImports) || changed;
6661
6862
  }
6662
6863
  const needsPropsInjection = propsTypeDeclaration && propsTypeDeclaration.length > 0;
6663
6864
  if (isComponent && (neededImports.size > 0 || needsPropsInjection)) {
@@ -6728,7 +6929,7 @@ async function transformScriptAsync(source2, options) {
6728
6929
  propsTypeDeclaration = result.propsTypeDeclaration;
6729
6930
  }
6730
6931
  if (isComponent) {
6731
- changed = transformAttrsForCalls(s, source2, neededImports) || changed;
6932
+ changed = await transformAttrsForCalls(s, source2, neededImports, schemaResolver) || changed;
6732
6933
  }
6733
6934
  const needsPropsInjectionAsync = propsTypeDeclaration && propsTypeDeclaration.length > 0;
6734
6935
  if (isComponent && (neededImports.size > 0 || needsPropsInjectionAsync)) {
@@ -6798,7 +6999,7 @@ async function transformScriptContent(source2, options) {
6798
6999
  propsTypeDeclaration = result.propsTypeDeclaration;
6799
7000
  }
6800
7001
  if (isComponent) {
6801
- changed = transformAttrsForCalls(s, source2, neededImports) || changed;
7002
+ changed = await transformAttrsForCalls(s, source2, neededImports, schemaResolver) || changed;
6802
7003
  }
6803
7004
  if (neededImports.size > 0) {
6804
7005
  const importStatement = generateRuntimeImport([...neededImports]);