Skip to content

Commit cbcd761

Browse files
paoloricciutioscard0mRich-Harris
authored
feat: deprecate svelte:component (#12694)
* feat: deprecate `svelte:component` Co-authored-by: Oscar Dominguez <dominguez.celada@gmail.com> * move logic into the visitor * tweak docs --------- Co-authored-by: Oscar Dominguez <dominguez.celada@gmail.com> Co-authored-by: Rich Harris <rich.harris@vercel.com>
1 parent 9a67ab1 commit cbcd761

File tree

7 files changed

+60
-0
lines changed

7 files changed

+60
-0
lines changed

.changeset/big-apes-smell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
feat: deprecate `svelte:component`

packages/svelte/messages/compile-warnings/template.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ This code will work when the component is rendered on the client (which is why t
5454

5555
> Using `<slot>` to render parent content is deprecated. Use `{@render ...}` tags instead
5656
57+
## svelte_component_deprecated
58+
59+
> `<svelte:component>` is deprecated in runes mode — components are dynamic by default
60+
61+
In previous versions of Svelte, the component constructor was fixed when the component was rendered. In other words, if you wanted `<X>` to re-render when `X` changed, you would either have to use `<svelte:component this={X}>` or put the component inside a `{#key X}...{/key}` block.
62+
63+
In Svelte 5 this is no longer true — if `X` changes, `<X>` re-renders. For more complex expressions like `condition ? Y : Z` you can use a derived value instead.
64+
5765
## svelte_element_invalid_this
5866

5967
> `this` should be an `{expression}`. Using a string attribute value will cause an error in future versions of Svelte
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
/** @import { SvelteComponent } from '#compiler' */
22
/** @import { Context } from '../types' */
3+
import * as w from '../../../warnings.js';
34
import { visit_component } from './shared/component.js';
45

56
/**
67
* @param {SvelteComponent} node
78
* @param {Context} context
89
*/
910
export function SvelteComponent(node, context) {
11+
if (context.state.analysis.runes) {
12+
w.svelte_component_deprecated(node);
13+
}
14+
1015
visit_component(node, context);
1116
}

packages/svelte/src/compiler/warnings.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export const codes = [
117117
"event_directive_deprecated",
118118
"node_invalid_placement_ssr",
119119
"slot_element_deprecated",
120+
"svelte_component_deprecated",
120121
"svelte_element_invalid_this"
121122
];
122123

@@ -767,6 +768,14 @@ export function slot_element_deprecated(node) {
767768
w(node, "slot_element_deprecated", "Using `<slot>` to render parent content is deprecated. Use `{@render ...}` tags instead");
768769
}
769770

771+
/**
772+
* `<svelte:component>` is deprecated in runes mode — components are dynamic by default
773+
* @param {null | NodeLike} node
774+
*/
775+
export function svelte_component_deprecated(node) {
776+
w(node, "svelte_component_deprecated", "`<svelte:component>` is deprecated in runes mode — components are dynamic by default");
777+
}
778+
770779
/**
771780
* `this` should be an `{expression}`. Using a string attribute value will cause an error in future versions of Svelte
772781
* @param {null | NodeLike} node

packages/svelte/tests/validator/samples/attribute-quoted/warnings.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111
"line": 13
1212
}
1313
},
14+
{
15+
"code": "svelte_component_deprecated",
16+
"end": {
17+
"column": 45,
18+
"line": 15
19+
},
20+
"message": "`<svelte:component>` is deprecated in runes mode — components are dynamic by default",
21+
"start": {
22+
"column": 0,
23+
"line": 15
24+
}
25+
},
1426
{
1527
"code": "attribute_quoted",
1628
"message": "Quoted attributes on components and custom elements will be stringified in a future version of Svelte. If this isn't what you want, remove the quotes",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<svelte:options runes />
2+
3+
<script>
4+
import A from './A.svelte';
5+
</script>
6+
7+
<svelte:component this={A} />
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "svelte_component_deprecated",
4+
"message": "`<svelte:component>` is deprecated in runes mode — components are dynamic by default",
5+
"start": {
6+
"line": 7,
7+
"column": 0
8+
},
9+
"end": {
10+
"line": 7,
11+
"column": 29
12+
}
13+
}
14+
]

0 commit comments

Comments
 (0)