Skip to content

Support for <svelte:element> #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/parser/converts/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ export function* convertChildren(
continue
}
if (child.type === "Element") {
yield convertHTMLElement(child, parent, ctx)
if (child.name.includes(":")) {
yield convertSpecialElement(child, parent, ctx)
} else {
yield convertHTMLElement(child, parent, ctx)
}
continue
}
if (child.type === "InlineComponent") {
Expand Down Expand Up @@ -277,6 +281,7 @@ function convertHTMLElement(
function convertSpecialElement(
node:
| SvAST.InlineComponent
| SvAST.Element
| SvAST.Window
| SvAST.Body
| SvAST.Head
Expand Down Expand Up @@ -330,20 +335,18 @@ function convertSpecialElement(
ctx.scriptLet.closeScope()
}

if (
node.type === "InlineComponent" &&
node.expression &&
node.name === "svelte:component"
) {
const thisExpression =
(node.type === "InlineComponent" &&
node.name === "svelte:component" &&
node.expression) ||
(node.type === "Element" && node.name === "svelte:element" && node.tag)
if (thisExpression) {
const eqIndex = ctx.code.lastIndexOf(
"=",
getWithLoc(node.expression).start,
getWithLoc(thisExpression).start,
)
const startIndex = ctx.code.lastIndexOf("this", eqIndex)
const closeIndex = ctx.code.indexOf(
"}",
getWithLoc(node.expression).end,
)
const closeIndex = ctx.code.indexOf("}", getWithLoc(thisExpression).end)
const endIndex = indexOf(
ctx.code,
(c) => c === ">" || !c.trim(),
Expand All @@ -366,7 +369,7 @@ function convertSpecialElement(
start: startIndex,
end: eqIndex,
})
ctx.scriptLet.addExpression(node.expression, thisAttr, null, (es) => {
ctx.scriptLet.addExpression(thisExpression, thisAttr, null, (es) => {
thisAttr.expression = es
})
element.startTag.attributes.push(thisAttr)
Expand Down
12 changes: 11 additions & 1 deletion src/parser/svelte-ast-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,22 @@ export interface KeyBlock extends BaseNode {
children: TemplateNode[]
}

export interface Element extends BaseNode {
export interface BaseElement extends BaseNode {
type: "Element"
name: string
children: TemplateNode[]
attributes: AttributeOrDirective[]
}

export interface BasicElement extends BaseElement {
tag?: undefined
}
export interface SvelteComponent extends BaseElement {
name: "svelte:element"
tag: ESTree.Expression
}
export type Element = BasicElement | SvelteComponent

export interface BaseInlineComponent extends BaseNode {
type: "InlineComponent"
name: string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<svelte:element this={expression}/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"ruleId": "no-undef",
"code": "expression",
"line": 1,
"column": 23
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
{
"type": "Program",
"body": [
{
"type": "SvelteElement",
"kind": "special",
"name": {
"type": "SvelteName",
"name": "svelte:element",
"range": [
1,
15
],
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 15
}
}
},
"startTag": {
"type": "SvelteStartTag",
"attributes": [
{
"type": "SvelteSpecialDirective",
"kind": "this",
"key": {
"type": "SvelteSpecialDirectiveKey",
"range": [
16,
20
],
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 20
}
}
},
"expression": {
"type": "Identifier",
"name": "expression",
"range": [
22,
32
],
"loc": {
"start": {
"line": 1,
"column": 22
},
"end": {
"line": 1,
"column": 32
}
}
},
"range": [
16,
34
],
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 34
}
}
}
],
"selfClosing": true,
"range": [
0,
35
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 35
}
}
},
"children": [],
"endTag": null,
"range": [
0,
35
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 35
}
}
}
],
"sourceType": "module",
"comments": [],
"tokens": [
{
"type": "Punctuator",
"value": "<",
"range": [
0,
1
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
}
}
},
{
"type": "HTMLIdentifier",
"value": "svelte:element",
"range": [
1,
15
],
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 15
}
}
},
{
"type": "HTMLIdentifier",
"value": "this",
"range": [
16,
20
],
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 20
}
}
},
{
"type": "Punctuator",
"value": "=",
"range": [
20,
21
],
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 21
}
}
},
{
"type": "Punctuator",
"value": "{",
"range": [
21,
22
],
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 22
}
}
},
{
"type": "Identifier",
"value": "expression",
"range": [
22,
32
],
"loc": {
"start": {
"line": 1,
"column": 22
},
"end": {
"line": 1,
"column": 32
}
}
},
{
"type": "Punctuator",
"value": "}",
"range": [
32,
33
],
"loc": {
"start": {
"line": 1,
"column": 32
},
"end": {
"line": 1,
"column": 33
}
}
},
{
"type": "Punctuator",
"value": "/",
"range": [
33,
34
],
"loc": {
"start": {
"line": 1,
"column": 33
},
"end": {
"line": 1,
"column": 34
}
}
},
{
"type": "Punctuator",
"value": ">",
"range": [
34,
35
],
"loc": {
"start": {
"line": 1,
"column": 34
},
"end": {
"line": 1,
"column": 35
}
}
}
],
"range": [
0,
35
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 35
}
}
}
Loading