Skip to content

Commit 3a3b7e8

Browse files
authored
chore: tidy up types for DOM operations (#17298)
* chore: tidy up types for DOM operations * more
1 parent ba9de6a commit 3a3b7e8

File tree

10 files changed

+32
-33
lines changed

10 files changed

+32
-33
lines changed

packages/svelte/src/internal/client/dom/blocks/css-props.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
/** @import { TemplateNode } from '#client' */
2-
import { render_effect, teardown } from '../../reactivity/effects.js';
1+
import { render_effect } from '../../reactivity/effects.js';
32
import { hydrating, set_hydrate_node } from '../hydration.js';
43
import { get_first_child } from '../operations.js';
54

@@ -10,7 +9,7 @@ import { get_first_child } from '../operations.js';
109
*/
1110
export function css_props(element, get_styles) {
1211
if (hydrating) {
13-
set_hydrate_node(/** @type {TemplateNode} */ (get_first_child(element)));
12+
set_hydrate_node(get_first_child(element));
1413
}
1514

1615
render_effect(() => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
131131
var parent_node = /** @type {Element} */ (node);
132132

133133
anchor = hydrating
134-
? set_hydrate_node(/** @type {Comment | Text} */ (get_first_child(parent_node)))
134+
? set_hydrate_node(get_first_child(parent_node))
135135
: parent_node.appendChild(create_text());
136136
}
137137

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ export function html(node, get_value, svg = false, mathml = false, skip_warning
6565
// We're deliberately not trying to repair mismatches between server and client,
6666
// as it's costly and error-prone (and it's an edge case to have a mismatch anyway)
6767
var hash = /** @type {Comment} */ (hydrate_node).data;
68+
69+
/** @type {TemplateNode | null} */
6870
var next = hydrate_next();
6971
var last = next;
7072

@@ -73,7 +75,7 @@ export function html(node, get_value, svg = false, mathml = false, skip_warning
7375
(next.nodeType !== COMMENT_NODE || /** @type {Comment} */ (next).data !== '')
7476
) {
7577
last = next;
76-
next = /** @type {TemplateNode} */ (get_next_sibling(next));
78+
next = get_next_sibling(next);
7779
}
7880

7981
if (next === null) {
@@ -110,7 +112,7 @@ export function html(node, get_value, svg = false, mathml = false, skip_warning
110112

111113
if (svg || mathml) {
112114
while (get_first_child(node)) {
113-
anchor.before(/** @type {Node} */ (get_first_child(node)));
115+
anchor.before(/** @type {TemplateNode} */ (get_first_child(node)));
114116
}
115117
} else {
116118
anchor.before(node);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio
9595

9696
// If hydrating, use the existing ssr comment as the anchor so that the
9797
// inner open and close methods can pick up the existing nodes correctly
98-
var child_anchor = /** @type {TemplateNode} */ (
99-
hydrating ? get_first_child(element) : element.appendChild(create_text())
100-
);
98+
var child_anchor = hydrating
99+
? get_first_child(element)
100+
: element.appendChild(create_text());
101101

102102
if (hydrating) {
103103
if (child_anchor === null) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ export function head(hash, render_fn) {
2121
if (hydrating) {
2222
previous_hydrate_node = hydrate_node;
2323

24-
var head_anchor = /** @type {TemplateNode} */ (get_first_child(document.head));
24+
var head_anchor = get_first_child(document.head);
2525

2626
// There might be multiple head blocks in our app, and they could have been
2727
// rendered in an arbitrary order — find one corresponding to this component
2828
while (
2929
head_anchor !== null &&
3030
(head_anchor.nodeType !== COMMENT_NODE || /** @type {Comment} */ (head_anchor).data !== hash)
3131
) {
32-
head_anchor = /** @type {TemplateNode} */ (get_next_sibling(head_anchor));
32+
head_anchor = get_next_sibling(head_anchor);
3333
}
3434

3535
// If we can't find an opening hydration marker, skip hydration (this can happen

packages/svelte/src/internal/client/dom/hydration.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function set_hydrating(value) {
3030
*/
3131
export let hydrate_node;
3232

33-
/** @param {TemplateNode} node */
33+
/** @param {TemplateNode | null} node */
3434
export function set_hydrate_node(node) {
3535
if (node === null) {
3636
w.hydration_mismatch();
@@ -41,7 +41,7 @@ export function set_hydrate_node(node) {
4141
}
4242

4343
export function hydrate_next() {
44-
return set_hydrate_node(/** @type {TemplateNode} */ (get_next_sibling(hydrate_node)));
44+
return set_hydrate_node(get_next_sibling(hydrate_node));
4545
}
4646

4747
/** @param {TemplateNode} node */

packages/svelte/src/internal/client/dom/operations.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,36 +83,34 @@ export function create_text(value = '') {
8383
/**
8484
* @template {Node} N
8585
* @param {N} node
86-
* @returns {Node | null}
8786
*/
8887
/*@__NO_SIDE_EFFECTS__*/
8988
export function get_first_child(node) {
90-
return first_child_getter.call(node);
89+
return /** @type {TemplateNode | null} */ (first_child_getter.call(node));
9190
}
9291

9392
/**
9493
* @template {Node} N
9594
* @param {N} node
96-
* @returns {Node | null}
9795
*/
9896
/*@__NO_SIDE_EFFECTS__*/
9997
export function get_next_sibling(node) {
100-
return next_sibling_getter.call(node);
98+
return /** @type {TemplateNode | null} */ (next_sibling_getter.call(node));
10199
}
102100

103101
/**
104102
* Don't mark this as side-effect-free, hydration needs to walk all nodes
105103
* @template {Node} N
106104
* @param {N} node
107105
* @param {boolean} is_text
108-
* @returns {Node | null}
106+
* @returns {TemplateNode | null}
109107
*/
110108
export function child(node, is_text) {
111109
if (!hydrating) {
112110
return get_first_child(node);
113111
}
114112

115-
var child = /** @type {TemplateNode} */ (get_first_child(hydrate_node));
113+
var child = get_first_child(hydrate_node);
116114

117115
// Child can be null if we have an element with a single child, like `<p>{text}</p>`, where `text` is empty
118116
if (child === null) {
@@ -130,14 +128,13 @@ export function child(node, is_text) {
130128

131129
/**
132130
* Don't mark this as side-effect-free, hydration needs to walk all nodes
133-
* @param {DocumentFragment | TemplateNode | TemplateNode[]} fragment
131+
* @param {TemplateNode} node
134132
* @param {boolean} [is_text]
135-
* @returns {Node | null}
133+
* @returns {TemplateNode | null}
136134
*/
137-
export function first_child(fragment, is_text = false) {
135+
export function first_child(node, is_text = false) {
138136
if (!hydrating) {
139-
// when not hydrating, `fragment` is a `DocumentFragment` (the result of calling `open_frag`)
140-
var first = /** @type {DocumentFragment} */ (get_first_child(/** @type {Node} */ (fragment)));
137+
var first = get_first_child(node);
141138

142139
// TODO prevent user comments with the empty string when preserveComments is true
143140
if (first instanceof Comment && first.data === '') return get_next_sibling(first);
@@ -163,7 +160,7 @@ export function first_child(fragment, is_text = false) {
163160
* @param {TemplateNode} node
164161
* @param {number} count
165162
* @param {boolean} is_text
166-
* @returns {Node | null}
163+
* @returns {TemplateNode | null}
167164
*/
168165
export function sibling(node, count = 1, is_text = false) {
169166
let next_sibling = hydrating ? hydrate_node : node;
@@ -195,7 +192,7 @@ export function sibling(node, count = 1, is_text = false) {
195192
}
196193

197194
set_hydrate_node(next_sibling);
198-
return /** @type {TemplateNode} */ (next_sibling);
195+
return next_sibling;
199196
}
200197

201198
/**

packages/svelte/src/internal/client/dom/template.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function from_html(content, flags) {
6060

6161
if (node === undefined) {
6262
node = create_fragment_from_html(has_start ? content : '<!>' + content);
63-
if (!is_fragment) node = /** @type {Node} */ (get_first_child(node));
63+
if (!is_fragment) node = /** @type {TemplateNode} */ (get_first_child(node));
6464
}
6565

6666
var clone = /** @type {TemplateNode} */ (
@@ -113,7 +113,7 @@ function from_namespace(content, flags, ns = 'svg') {
113113
if (is_fragment) {
114114
node = document.createDocumentFragment();
115115
while (get_first_child(root)) {
116-
node.appendChild(/** @type {Node} */ (get_first_child(root)));
116+
node.appendChild(/** @type {TemplateNode} */ (get_first_child(root)));
117117
}
118118
} else {
119119
node = /** @type {Element} */ (get_first_child(root));
@@ -227,7 +227,7 @@ export function from_tree(structure, flags) {
227227
: undefined;
228228

229229
node = fragment_from_tree(structure, ns);
230-
if (!is_fragment) node = /** @type {Node} */ (get_first_child(node));
230+
if (!is_fragment) node = /** @type {TemplateNode} */ (get_first_child(node));
231231
}
232232

233233
var clone = /** @type {TemplateNode} */ (

packages/svelte/src/internal/client/reactivity/effects.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ export function destroy_effect(effect, remove_dom = true) {
548548
export function remove_effect_dom(node, end) {
549549
while (node !== null) {
550550
/** @type {TemplateNode | null} */
551-
var next = node === end ? null : /** @type {TemplateNode} */ (get_next_sibling(node));
551+
var next = node === end ? null : get_next_sibling(node);
552552

553553
node.remove();
554554
node = next;
@@ -715,7 +715,7 @@ export function move_effect(effect, fragment) {
715715

716716
while (node !== null) {
717717
/** @type {TemplateNode | null} */
718-
var next = node === end ? null : /** @type {TemplateNode} */ (get_next_sibling(node));
718+
var next = node === end ? null : get_next_sibling(node);
719719

720720
fragment.append(node);
721721
node = next;

packages/svelte/src/internal/client/render.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,13 @@ export function hydrate(component, options) {
9999
const previous_hydrate_node = hydrate_node;
100100

101101
try {
102-
var anchor = /** @type {TemplateNode} */ (get_first_child(target));
102+
var anchor = get_first_child(target);
103+
103104
while (
104105
anchor &&
105106
(anchor.nodeType !== COMMENT_NODE || /** @type {Comment} */ (anchor).data !== HYDRATION_START)
106107
) {
107-
anchor = /** @type {TemplateNode} */ (get_next_sibling(anchor));
108+
anchor = get_next_sibling(anchor);
108109
}
109110

110111
if (!anchor) {

0 commit comments

Comments
 (0)