sveld 0.24.6 → 0.24.8
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/README.md
CHANGED
|
@@ -38,7 +38,7 @@ The generated definition extends the official `SvelteComponentTyped` interface e
|
|
|
38
38
|
**Button.svelte.d.ts**
|
|
39
39
|
|
|
40
40
|
```ts
|
|
41
|
-
import
|
|
41
|
+
import { SvelteComponentTyped } from "svelte";
|
|
42
42
|
import type { SvelteHTMLElements } from "svelte/elements";
|
|
43
43
|
|
|
44
44
|
type $RestProps = SvelteHTMLElements["button"];
|
package/lib/ComponentParser.d.ts
CHANGED
package/lib/ComponentParser.js
CHANGED
|
@@ -783,10 +783,20 @@ class ComponentParser {
|
|
|
783
783
|
}
|
|
784
784
|
if (node.type === "Spread" && node?.expression.name === "$$restProps") {
|
|
785
785
|
if (this.rest_props === undefined && (parent?.type === "InlineComponent" || parent?.type === "Element")) {
|
|
786
|
-
|
|
786
|
+
const restProps = {
|
|
787
787
|
type: parent.type,
|
|
788
788
|
name: parent.name,
|
|
789
789
|
};
|
|
790
|
+
// Handle svelte:element - check if this attribute is hardcoded
|
|
791
|
+
if (parent.type === "Element" && parent.name === "svelte:element") {
|
|
792
|
+
// The 'this' value is stored in the 'tag' property of the Element node
|
|
793
|
+
// If tag is a string, it's hardcoded; if undefined/null, it's dynamic
|
|
794
|
+
if (typeof parent.tag === "string") {
|
|
795
|
+
restProps.thisValue = parent.tag;
|
|
796
|
+
}
|
|
797
|
+
// If tag is undefined or not a string, thisValue remains undefined (dynamic)
|
|
798
|
+
}
|
|
799
|
+
this.rest_props = restProps;
|
|
790
800
|
}
|
|
791
801
|
}
|
|
792
802
|
if (node.type === "VariableDeclaration") {
|
|
@@ -89,13 +89,29 @@ function genPropDef(def) {
|
|
|
89
89
|
let prop_def = EMPTY_STR;
|
|
90
90
|
const genericsName = def.generics ? `<${def.generics[0]}>` : "";
|
|
91
91
|
if (def.rest_props?.type === "Element") {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
92
|
+
let extend_tag_map;
|
|
93
|
+
// Handle svelte:element specially
|
|
94
|
+
if (def.rest_props.name === "svelte:element") {
|
|
95
|
+
// If thisValue is provided (hardcoded element tag), use that element type
|
|
96
|
+
// Otherwise, fallback to HTMLElement for dynamic this attribute
|
|
97
|
+
if (def.rest_props.thisValue) {
|
|
98
|
+
extend_tag_map = `SvelteHTMLElements["${def.rest_props.thisValue}"]`;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
// Dynamic this attribute - use generic HTMLElement
|
|
102
|
+
extend_tag_map = "HTMLAttributes<HTMLElement>";
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
// Regular element - use existing logic
|
|
107
|
+
extend_tag_map = def.rest_props.name
|
|
108
|
+
.split("|")
|
|
109
|
+
.map((name) => {
|
|
110
|
+
const element = name.trim();
|
|
111
|
+
return `SvelteHTMLElements["${element}"]`;
|
|
112
|
+
})
|
|
113
|
+
.join("&");
|
|
114
|
+
}
|
|
99
115
|
/**
|
|
100
116
|
* Components that extend HTML elements should allow for `data-*` attributes.
|
|
101
117
|
* @see https://github.com/sveltejs/language-tools/issues/1825
|
|
@@ -423,8 +439,12 @@ function writeTsDefinition(component) {
|
|
|
423
439
|
});
|
|
424
440
|
const generic = generics ? `<${generics[1]}>` : "";
|
|
425
441
|
const genericProps = generics ? `${props_name}<${generics[0]}>` : props_name;
|
|
442
|
+
// Determine imports needed for rest_props
|
|
443
|
+
const needsSvelteHTMLElements = rest_props?.type === "Element" &&
|
|
444
|
+
(rest_props.name !== "svelte:element" || (rest_props.name === "svelte:element" && rest_props.thisValue));
|
|
445
|
+
const needsHTMLAttributes = rest_props?.type === "Element" && rest_props.name === "svelte:element" && !rest_props.thisValue;
|
|
426
446
|
return `
|
|
427
|
-
import
|
|
447
|
+
import { SvelteComponentTyped } from "svelte";${needsSvelteHTMLElements ? `import type { SvelteHTMLElements } from "svelte/elements";\n` : ""}${needsHTMLAttributes ? `import type { HTMLAttributes } from "svelte/elements";\n` : ""}
|
|
428
448
|
${genImports({ extends: _extends })}
|
|
429
449
|
${genModuleExports({ moduleExports })}
|
|
430
450
|
${getTypeDefs({ typedefs })}
|