Skip to content

Commit bdcba5c

Browse files
authored
Support for <svelte:element> (#156)
1 parent 00d9f76 commit bdcba5c

24 files changed

+8090
-13
lines changed

src/parser/converts/element.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ export function* convertChildren(
8282
continue
8383
}
8484
if (child.type === "Element") {
85-
yield convertHTMLElement(child, parent, ctx)
85+
if (child.name.includes(":")) {
86+
yield convertSpecialElement(child, parent, ctx)
87+
} else {
88+
yield convertHTMLElement(child, parent, ctx)
89+
}
8690
continue
8791
}
8892
if (child.type === "InlineComponent") {
@@ -277,6 +281,7 @@ function convertHTMLElement(
277281
function convertSpecialElement(
278282
node:
279283
| SvAST.InlineComponent
284+
| SvAST.Element
280285
| SvAST.Window
281286
| SvAST.Body
282287
| SvAST.Head
@@ -330,20 +335,18 @@ function convertSpecialElement(
330335
ctx.scriptLet.closeScope()
331336
}
332337

333-
if (
334-
node.type === "InlineComponent" &&
335-
node.expression &&
336-
node.name === "svelte:component"
337-
) {
338+
const thisExpression =
339+
(node.type === "InlineComponent" &&
340+
node.name === "svelte:component" &&
341+
node.expression) ||
342+
(node.type === "Element" && node.name === "svelte:element" && node.tag)
343+
if (thisExpression) {
338344
const eqIndex = ctx.code.lastIndexOf(
339345
"=",
340-
getWithLoc(node.expression).start,
346+
getWithLoc(thisExpression).start,
341347
)
342348
const startIndex = ctx.code.lastIndexOf("this", eqIndex)
343-
const closeIndex = ctx.code.indexOf(
344-
"}",
345-
getWithLoc(node.expression).end,
346-
)
349+
const closeIndex = ctx.code.indexOf("}", getWithLoc(thisExpression).end)
347350
const endIndex = indexOf(
348351
ctx.code,
349352
(c) => c === ">" || !c.trim(),
@@ -366,7 +369,7 @@ function convertSpecialElement(
366369
start: startIndex,
367370
end: eqIndex,
368371
})
369-
ctx.scriptLet.addExpression(node.expression, thisAttr, null, (es) => {
372+
ctx.scriptLet.addExpression(thisExpression, thisAttr, null, (es) => {
370373
thisAttr.expression = es
371374
})
372375
element.startTag.attributes.push(thisAttr)

src/parser/svelte-ast-types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,22 @@ export interface KeyBlock extends BaseNode {
106106
children: TemplateNode[]
107107
}
108108

109-
export interface Element extends BaseNode {
109+
export interface BaseElement extends BaseNode {
110110
type: "Element"
111111
name: string
112112
children: TemplateNode[]
113113
attributes: AttributeOrDirective[]
114114
}
115+
116+
export interface BasicElement extends BaseElement {
117+
tag?: undefined
118+
}
119+
export interface SvelteComponent extends BaseElement {
120+
name: "svelte:element"
121+
tag: ESTree.Expression
122+
}
123+
export type Element = BasicElement | SvelteComponent
124+
115125
export interface BaseInlineComponent extends BaseNode {
116126
type: "InlineComponent"
117127
name: string

0 commit comments

Comments
 (0)