Skip to content

Commit a59bc99

Browse files
authored
chore: tidy up (#12336)
* chore: tidy up * changeset * reverse wrap_snippet argument order, for more compact output * more tidy-up
1 parent daac5fc commit a59bc99

File tree

10 files changed

+47
-52
lines changed

10 files changed

+47
-52
lines changed

.changeset/eleven-cows-judge.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+
fix: reverse parent/child order in invalid HTML warning

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ function serialize_inline_component(node, component_name, context, anchor = cont
883883
b.init(
884884
'children',
885885
context.state.options.dev
886-
? b.call('$.wrap_snippet', slot_fn, b.id(context.state.analysis.name))
886+
? b.call('$.wrap_snippet', b.id(context.state.analysis.name), slot_fn)
887887
: slot_fn
888888
)
889889
);
@@ -2752,7 +2752,7 @@ export const template_visitors = {
27522752
let snippet = b.arrow(args, body);
27532753

27542754
if (context.state.options.dev) {
2755-
snippet = b.call('$.wrap_snippet', snippet, b.id(context.state.analysis.name));
2755+
snippet = b.call('$.wrap_snippet', b.id(context.state.analysis.name), snippet);
27562756
}
27572757

27582758
const declaration = b.const(node.expression, snippet);

packages/svelte/src/internal/client/dom/blocks/await.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @import { Effect, Source, TemplateNode } from '#client' */
12
import { is_promise, noop } from '../../../shared/utils.js';
23
import {
34
current_component_context,
@@ -20,10 +21,10 @@ const CATCH = 2;
2021

2122
/**
2223
* @template V
23-
* @param {Comment} anchor
24+
* @param {TemplateNode} anchor
2425
* @param {(() => Promise<V>)} get_input
2526
* @param {null | ((anchor: Node) => void)} pending_fn
26-
* @param {null | ((anchor: Node, value: import('#client').Source<V>) => void)} then_fn
27+
* @param {null | ((anchor: Node, value: Source<V>) => void)} then_fn
2728
* @param {null | ((anchor: Node, error: unknown) => void)} catch_fn
2829
* @returns {void}
2930
*/
@@ -37,13 +38,13 @@ export function await_block(anchor, get_input, pending_fn, then_fn, catch_fn) {
3738
/** @type {V | Promise<V>} */
3839
var input;
3940

40-
/** @type {import('#client').Effect | null} */
41+
/** @type {Effect | null} */
4142
var pending_effect;
4243

43-
/** @type {import('#client').Effect | null} */
44+
/** @type {Effect | null} */
4445
var then_effect;
4546

46-
/** @type {import('#client').Effect | null} */
47+
/** @type {Effect | null} */
4748
var catch_effect;
4849

4950
var input_source = runes
Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
/** @import { TemplateNode } from '#client' */
12
import { hydrating, set_hydrate_nodes } from '../hydration.js';
2-
import { render_effect } from '../../reactivity/effects.js';
3+
import { render_effect, teardown } from '../../reactivity/effects.js';
34

45
/**
56
* @param {HTMLDivElement | SVGGElement} element
@@ -8,29 +9,24 @@ import { render_effect } from '../../reactivity/effects.js';
89
*/
910
export function css_props(element, get_styles) {
1011
if (hydrating) {
11-
set_hydrate_nodes(
12-
/** @type {import('#client').TemplateNode[]} */ ([...element.childNodes]).slice(0, -1)
13-
);
12+
set_hydrate_nodes(/** @type {TemplateNode[]} */ ([...element.childNodes]).slice(0, -1));
1413
}
1514

1615
render_effect(() => {
17-
render_effect(() => {
18-
var styles = get_styles();
16+
var styles = get_styles();
1917

20-
for (var key in styles) {
21-
var value = styles[key];
18+
for (var key in styles) {
19+
var value = styles[key];
2220

23-
if (value) {
24-
element.style.setProperty(key, value);
25-
} else {
26-
element.style.removeProperty(key);
27-
}
21+
if (value) {
22+
element.style.setProperty(key, value);
23+
} else {
24+
element.style.removeProperty(key);
2825
}
29-
});
26+
}
27+
});
3028

31-
return () => {
32-
// TODO use `teardown` instead of creating a nested effect, post-https://github.com/sveltejs/svelte/pull/11936
33-
element.remove();
34-
};
29+
teardown(() => {
30+
element.remove();
3531
});
3632
}

packages/svelte/src/internal/client/dom/blocks/html.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @import { Effect, TemplateNode } from '#client' */
12
import { block, branch, destroy_effect } from '../../reactivity/effects.js';
23
import { get_start, hydrate_nodes, hydrating } from '../hydration.js';
34
import { create_fragment_from_html } from '../reconciler.js';
@@ -13,7 +14,7 @@ import { assign_nodes } from '../template.js';
1314
export function html(anchor, get_value, svg, mathml) {
1415
var value = '';
1516

16-
/** @type {import('#client').Effect | null} */
17+
/** @type {Effect | null} */
1718
var effect;
1819

1920
block(() => {
@@ -46,8 +47,8 @@ export function html(anchor, get_value, svg, mathml) {
4647
}
4748

4849
assign_nodes(
49-
/** @type {import('#client').TemplateNode} */ (node.firstChild),
50-
/** @type {import('#client').TemplateNode} */ (node.lastChild)
50+
/** @type {TemplateNode} */ (node.firstChild),
51+
/** @type {TemplateNode} */ (node.lastChild)
5152
);
5253

5354
if (svg || mathml) {
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @import { Effect, TemplateNode } from '#client' */
12
import { add_snippet_symbol } from '../../../shared/validate.js';
23
import { EFFECT_TRANSPARENT } from '../../constants.js';
34
import { branch, block, destroy_effect } from '../../reactivity/effects.js';
@@ -7,8 +8,8 @@ import {
78
} from '../../runtime.js';
89

910
/**
10-
* @template {(node: import('#client').TemplateNode, ...args: any[]) => import('#client').Dom} SnippetFn
11-
* @param {import('#client').TemplateNode} anchor
11+
* @template {(node: TemplateNode, ...args: any[]) => void} SnippetFn
12+
* @param {TemplateNode} anchor
1213
* @param {() => SnippetFn | null | undefined} get_snippet
1314
* @param {(() => any)[]} args
1415
* @returns {void}
@@ -17,7 +18,7 @@ export function snippet(anchor, get_snippet, ...args) {
1718
/** @type {SnippetFn | null | undefined} */
1819
var snippet;
1920

20-
/** @type {import('#client').Effect | null} */
21+
/** @type {Effect | null} */
2122
var snippet_effect;
2223

2324
block(() => {
@@ -37,20 +38,18 @@ export function snippet(anchor, get_snippet, ...args) {
3738
/**
3839
* In development, wrap the snippet function so that it passes validation, and so that the
3940
* correct component context is set for ownership checks
40-
* @param {(node: import('#client').TemplateNode, ...args: any[]) => import('#client').Dom} fn
4141
* @param {any} component
42+
* @param {(node: TemplateNode, ...args: any[]) => void} fn
4243
*/
43-
export function wrap_snippet(fn, component) {
44-
return add_snippet_symbol(
45-
(/** @type {import('#client').TemplateNode} */ node, /** @type {any[]} */ ...args) => {
46-
var previous_component_function = dev_current_component_function;
47-
set_dev_current_component_function(component);
44+
export function wrap_snippet(component, fn) {
45+
return add_snippet_symbol((/** @type {TemplateNode} */ node, /** @type {any[]} */ ...args) => {
46+
var previous_component_function = dev_current_component_function;
47+
set_dev_current_component_function(component);
4848

49-
try {
50-
return fn(node, ...args);
51-
} finally {
52-
set_dev_current_component_function(previous_component_function);
53-
}
49+
try {
50+
return fn(node, ...args);
51+
} finally {
52+
set_dev_current_component_function(previous_component_function);
5453
}
55-
);
54+
});
5655
}

packages/svelte/src/internal/client/dom/blocks/svelte-element.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio
6969
pause_effect(effect, () => {
7070
effect = null;
7171
current_tag = null;
72-
element?.remove();
7372
});
7473
} else if (next_tag === current_tag) {
7574
// same tag as is currently rendered — abort outro
@@ -83,7 +82,6 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio
8382

8483
if (next_tag && next_tag !== current_tag) {
8584
effect = branch(() => {
86-
const prev_element = element;
8785
element = hydrating
8886
? /** @type {Element} */ (element)
8987
: ns
@@ -103,10 +101,6 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio
103101
};
104102
}
105103

106-
if (prev_element && !hydrating) {
107-
prev_element.remove();
108-
}
109-
110104
if (render_fn) {
111105
// If hydrating, use the existing ssr comment as the anchor so that the
112106
// inner open and close methods can pick up the existing nodes correctly

packages/svelte/src/internal/server/dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function stringify(element) {
3838
*/
3939
function print_error(payload, parent, child) {
4040
var message =
41-
`${stringify(child)} cannot contain ${stringify(parent)}\n\n` +
41+
`${stringify(parent)} cannot contain ${stringify(child)}\n\n` +
4242
'This can cause content to shift around as the browser repairs the HTML, and will likely result in a `hydration_mismatch` warning.';
4343

4444
if ((seen ??= new Set()).has(message)) return;

packages/svelte/tests/runtime-legacy/samples/inline-style-directive-shorthand-declaration-only/_config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export default test({
1515
assert.equal(window.getComputedStyle(p2).color, 'red');
1616

1717
const btn = target.querySelector('button');
18-
console.log(btn);
1918
btn?.click();
2019
flushSync();
2120

packages/svelte/tests/runtime-runes/samples/invalid-html-ssr/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default test({
3232
if (variant === 'hydrate') {
3333
assert.equal(
3434
log[0],
35-
'`<h1>` (Component.svelte:1:0) cannot contain `<p>` (main.svelte:5:0)\n\n' +
35+
'`<p>` (main.svelte:5:0) cannot contain `<h1>` (Component.svelte:1:0)\n\n' +
3636
'This can cause content to shift around as the browser repairs the HTML, and will likely result in a `hydration_mismatch` warning.'
3737
);
3838
}

0 commit comments

Comments
 (0)