Skip to content

Commit cd98a53

Browse files
authored
(feat) add support for svelte:element (#1437)
#1435
1 parent d745f5d commit cd98a53

File tree

16 files changed

+99
-18
lines changed

16 files changed

+99
-18
lines changed

packages/language-server/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
"fast-glob": "^3.2.7",
5454
"lodash": "^4.17.21",
5555
"prettier": "2.5.1",
56-
"prettier-plugin-svelte": "~2.6.0",
56+
"prettier-plugin-svelte": "~2.7.0",
5757
"source-map": "^0.7.3",
58-
"svelte": "^3.46.1",
58+
"svelte": "^3.47.0",
5959
"svelte-preprocess": "~4.10.1",
6060
"svelte2tsx": "~0.5.0",
6161
"typescript": "*",

packages/language-server/src/plugins/html/dataProvider.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ const svelteTags: ITagData[] = [
8585
}
8686
]
8787
},
88+
{
89+
name: 'svelte:element',
90+
description:
91+
'Renders a DOM element dynamically, using the string as the this property. When the property changes, the element is destroyed and recreated.\n\nIf this is falsy, no element is rendered.',
92+
attributes: [
93+
{
94+
name: 'this',
95+
description:
96+
'DOM element to render.\n\nWhen this property changes, the element is destroyed and recreated.\nIf this is falsy, no element is rendered.'
97+
}
98+
]
99+
},
88100
{
89101
name: 'svelte:window',
90102
description:

packages/language-server/test/plugins/svelte/features/getDiagnostics.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,11 @@ describe('SveltePlugin#getDiagnostics', () => {
471471

472472
assert.deepStrictEqual(diagnostics, [
473473
{
474-
range: { start: { line: 1, character: 15 }, end: { line: 1, character: 27 } },
475-
message:
476-
"Component has unused export property 'name'. If it is for external reference only, please consider using `export const name`",
474+
range: { start: { line: 1, character: 4 }, end: { line: 1, character: 26 } },
475+
message: '$: has no effect in a module script',
477476
severity: 2,
478477
source: 'svelte',
479-
code: 'unused-export-let'
478+
code: 'module-script-reactive-declaration'
480479
}
481480
]);
482481
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<script context="module" lang="ts">
2-
export let name: string;
2+
$: console.log('foo');
33
</script>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"range": { "start": { "line": 10, "character": 0 }, "end": { "line": 10, "character": 0 } },
4+
"severity": 1,
5+
"source": "js",
6+
"message": "<svelte:element> must have a 'this' attribute",
7+
"code": -1
8+
}
9+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"range": { "start": { "line": 10, "character": 0 }, "end": { "line": 10, "character": 0 } },
4+
"severity": 1,
5+
"source": "js",
6+
"message": "<svelte:element> must have a 'this' attribute",
7+
"code": -1
8+
}
9+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
let tag = 'div';
3+
</script>
4+
5+
<!-- valid -->
6+
<svelte:element this={tag} />
7+
<svelte:element this={tag}>{tag}</svelte:element>
8+
<svelte:element this={tag} on:click={() => tag} />
9+
10+
<!-- error -->
11+
<svelte:element />

packages/svelte2tsx/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"source-map": "^0.6.1",
3535
"source-map-support": "^0.5.16",
3636
"sourcemap-codec": "^1.4.8",
37-
"svelte": "~3.46.1",
37+
"svelte": "~3.47.0",
3838
"tiny-glob": "^0.2.6",
3939
"tslib": "^1.10.0",
4040
"typescript": "^4.6.2"

packages/svelte2tsx/src/htmlxtojsx/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ export function convertHtmlxToJsx(
111111
);
112112
break;
113113
case 'Element':
114+
if (node.name === 'svelte:element') {
115+
handleSvelteTag(htmlx, str, node);
116+
}
114117
templateScopeManager.componentOrSlotTemplateOrElementEnter(node);
115118
handleElement(
116119
htmlx,

packages/svelte2tsx/src/htmlxtojsx/nodes/svelte-tag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { BaseNode } from '../../interfaces';
33

44
/**
55
* `<svelte:window>...</svelte:window>` ----> `<sveltewindow>...</sveltewindow>`
6-
* (same for :head, :body, :options, :fragment)
6+
* (same for :head, :body, :options, :fragment, :element)
77
*/
88
export function handleSvelteTag(htmlx: string, str: MagicString, node: BaseNode): void {
99
const colon = htmlx.indexOf(':', node.start);

0 commit comments

Comments
 (0)