Skip to content

Commit 4db4ee5

Browse files
authored
chore: rid of nodeType magic numbers (#16208)
* chore: rid of nodeType magic numbers * lint --------- Co-authored-by: 7nik <[email protected]>
1 parent 669a223 commit 4db4ee5

File tree

14 files changed

+53
-35
lines changed

14 files changed

+53
-35
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ export const STATE_SYMBOL = Symbol('$state');
2626
export const LEGACY_PROPS = Symbol('legacy props');
2727
export const LOADING_ATTR_SYMBOL = Symbol('');
2828
export const PROXY_PATH_SYMBOL = Symbol('proxy path');
29+
30+
export const ELEMENT_NODE = 1;
31+
export const TEXT_NODE = 3;
32+
export const COMMENT_NODE = 8;
33+
export const DOCUMENT_FRAGMENT_NODE = 11;

packages/svelte/src/internal/client/dev/elements.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/** @import { SourceLocation } from '#client' */
2+
import { COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, ELEMENT_NODE } from '#client/constants';
23
import { HYDRATION_END, HYDRATION_START, HYDRATION_START_ELSE } from '../../../constants.js';
34
import { hydrating } from '../dom/hydration.js';
45

@@ -12,7 +13,7 @@ export function add_locations(fn, filename, locations) {
1213
return (/** @type {any[]} */ ...args) => {
1314
const dom = fn(...args);
1415

15-
var node = hydrating ? dom : dom.nodeType === 11 ? dom.firstChild : dom;
16+
var node = hydrating ? dom : dom.nodeType === DOCUMENT_FRAGMENT_NODE ? dom.firstChild : dom;
1617
assign_locations(node, filename, locations);
1718

1819
return dom;
@@ -45,13 +46,13 @@ function assign_locations(node, filename, locations) {
4546
var depth = 0;
4647

4748
while (node && i < locations.length) {
48-
if (hydrating && node.nodeType === 8) {
49+
if (hydrating && node.nodeType === COMMENT_NODE) {
4950
var comment = /** @type {Comment} */ (node);
5051
if (comment.data === HYDRATION_START || comment.data === HYDRATION_START_ELSE) depth += 1;
5152
else if (comment.data[0] === HYDRATION_END) depth -= 1;
5253
}
5354

54-
if (depth === 0 && node.nodeType === 1) {
55+
if (depth === 0 && node.nodeType === ELEMENT_NODE) {
5556
assign_location(/** @type {Element} */ (node), filename, locations[i++]);
5657
}
5758

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
} from '../../reactivity/effects.js';
3535
import { source, mutable_source, internal_set } from '../../reactivity/sources.js';
3636
import { array_from, is_array } from '../../../shared/utils.js';
37-
import { INERT } from '#client/constants';
37+
import { COMMENT_NODE, INERT } from '#client/constants';
3838
import { queue_micro_task } from '../task.js';
3939
import { active_effect, get } from '../../runtime.js';
4040
import { DEV } from 'esm-env';
@@ -183,7 +183,7 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
183183

184184
for (var i = 0; i < length; i++) {
185185
if (
186-
hydrate_node.nodeType === 8 &&
186+
hydrate_node.nodeType === COMMENT_NODE &&
187187
/** @type {Comment} */ (hydrate_node).data === HYDRATION_END
188188
) {
189189
// The server rendered fewer items than expected,

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { DEV } from 'esm-env';
1010
import { dev_current_component_function } from '../../context.js';
1111
import { get_first_child, get_next_sibling } from '../operations.js';
1212
import { active_effect } from '../../runtime.js';
13+
import { COMMENT_NODE } from '#client/constants';
1314

1415
/**
1516
* @param {Element} element
@@ -67,7 +68,10 @@ export function html(node, get_value, svg = false, mathml = false, skip_warning
6768
var next = hydrate_next();
6869
var last = next;
6970

70-
while (next !== null && (next.nodeType !== 8 || /** @type {Comment} */ (next).data !== '')) {
71+
while (
72+
next !== null &&
73+
(next.nodeType !== COMMENT_NODE || /** @type {Comment} */ (next).data !== '')
74+
) {
7175
last = next;
7276
next = /** @type {TemplateNode} */ (get_next_sibling(next));
7377
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @import { Snippet } from 'svelte' */
22
/** @import { Effect, TemplateNode } from '#client' */
33
/** @import { Getters } from '#shared' */
4-
import { EFFECT_TRANSPARENT } from '#client/constants';
4+
import { EFFECT_TRANSPARENT, ELEMENT_NODE } from '#client/constants';
55
import { branch, block, destroy_effect, teardown } from '../../reactivity/effects.js';
66
import {
77
dev_current_component_function,
@@ -102,7 +102,7 @@ export function createRawSnippet(fn) {
102102
var fragment = create_fragment_from_html(html);
103103
element = /** @type {Element} */ (get_first_child(fragment));
104104

105-
if (DEV && (get_next_sibling(element) !== null || element.nodeType !== 1)) {
105+
if (DEV && (get_next_sibling(element) !== null || element.nodeType !== ELEMENT_NODE)) {
106106
w.invalid_raw_snippet_render();
107107
}
108108

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { current_each_item, set_current_each_item } from './each.js';
2020
import { active_effect } from '../../runtime.js';
2121
import { component_context } from '../../context.js';
2222
import { DEV } from 'esm-env';
23-
import { EFFECT_TRANSPARENT } from '#client/constants';
23+
import { EFFECT_TRANSPARENT, ELEMENT_NODE } from '#client/constants';
2424
import { assign_nodes } from '../template.js';
2525
import { is_raw_text_element } from '../../../../utils.js';
2626

@@ -51,7 +51,7 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio
5151
/** @type {null | Element} */
5252
var element = null;
5353

54-
if (hydrating && hydrate_node.nodeType === 1) {
54+
if (hydrating && hydrate_node.nodeType === ELEMENT_NODE) {
5555
element = /** @type {Element} */ (hydrate_node);
5656
hydrate_next();
5757
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { hydrate_node, hydrating, set_hydrate_node, set_hydrating } from '../hydration.js';
33
import { create_text, get_first_child, get_next_sibling } from '../operations.js';
44
import { block } from '../../reactivity/effects.js';
5-
import { HEAD_EFFECT } from '#client/constants';
5+
import { COMMENT_NODE, HEAD_EFFECT } from '#client/constants';
66
import { HYDRATION_START } from '../../../../constants.js';
77

88
/**
@@ -37,7 +37,8 @@ export function head(render_fn) {
3737

3838
while (
3939
head_anchor !== null &&
40-
(head_anchor.nodeType !== 8 || /** @type {Comment} */ (head_anchor).data !== HYDRATION_START)
40+
(head_anchor.nodeType !== COMMENT_NODE ||
41+
/** @type {Comment} */ (head_anchor).data !== HYDRATION_START)
4142
) {
4243
head_anchor = /** @type {TemplateNode} */ (get_next_sibling(head_anchor));
4344
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** @import { TemplateNode } from '#client' */
22

3+
import { COMMENT_NODE } from '#client/constants';
34
import {
45
HYDRATION_END,
56
HYDRATION_ERROR,
@@ -87,7 +88,7 @@ export function remove_nodes() {
8788
var node = hydrate_node;
8889

8990
while (true) {
90-
if (node.nodeType === 8) {
91+
if (node.nodeType === COMMENT_NODE) {
9192
var data = /** @type {Comment} */ (node).data;
9293

9394
if (data === HYDRATION_END) {
@@ -109,7 +110,7 @@ export function remove_nodes() {
109110
* @param {TemplateNode} node
110111
*/
111112
export function read_hydration_instruction(node) {
112-
if (!node || node.nodeType !== 8) {
113+
if (!node || node.nodeType !== COMMENT_NODE) {
113114
w.hydration_mismatch();
114115
throw HYDRATION_ERROR;
115116
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { hydrate_node, hydrating, set_hydrate_node } from './hydration.js';
33
import { DEV } from 'esm-env';
44
import { init_array_prototype_warnings } from '../dev/equality.js';
55
import { get_descriptor, is_extensible } from '../../shared/utils.js';
6+
import { TEXT_NODE } from '#client/constants';
67

78
// export these for reference in the compiled code, making global name deduplication unnecessary
89
/** @type {Window} */
@@ -113,7 +114,7 @@ export function child(node, is_text) {
113114
// Child can be null if we have an element with a single child, like `<p>{text}</p>`, where `text` is empty
114115
if (child === null) {
115116
child = hydrate_node.appendChild(create_text());
116-
} else if (is_text && child.nodeType !== 3) {
117+
} else if (is_text && child.nodeType !== TEXT_NODE) {
117118
var text = create_text();
118119
child?.before(text);
119120
set_hydrate_node(text);
@@ -143,7 +144,7 @@ export function first_child(fragment, is_text) {
143144

144145
// if an {expression} is empty during SSR, there might be no
145146
// text node to hydrate — we must therefore create one
146-
if (is_text && hydrate_node?.nodeType !== 3) {
147+
if (is_text && hydrate_node?.nodeType !== TEXT_NODE) {
147148
var text = create_text();
148149

149150
hydrate_node?.before(text);
@@ -174,11 +175,9 @@ export function sibling(node, count = 1, is_text = false) {
174175
return next_sibling;
175176
}
176177

177-
var type = next_sibling?.nodeType;
178-
179178
// if a sibling {expression} is empty during SSR, there might be no
180179
// text node to hydrate — we must therefore create one
181-
if (is_text && type !== 3) {
180+
if (is_text && next_sibling?.nodeType !== TEXT_NODE) {
182181
var text = create_text();
183182
// If the next sibling is `null` and we're handling text then it's because
184183
// the SSR content was empty for the text, so we need to generate a new text

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
TEMPLATE_USE_MATHML,
2121
TEMPLATE_USE_SVG
2222
} from '../../../constants.js';
23+
import { COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, TEXT_NODE } from '#client/constants';
2324

2425
/**
2526
* @param {TemplateNode} start
@@ -264,7 +265,7 @@ function run_scripts(node) {
264265
// scripts were SSR'd, in which case they will run
265266
if (hydrating) return node;
266267

267-
const is_fragment = node.nodeType === 11;
268+
const is_fragment = node.nodeType === DOCUMENT_FRAGMENT_NODE;
268269
const scripts =
269270
/** @type {HTMLElement} */ (node).tagName === 'SCRIPT'
270271
? [/** @type {HTMLScriptElement} */ (node)]
@@ -305,7 +306,7 @@ export function text(value = '') {
305306

306307
var node = hydrate_node;
307308

308-
if (node.nodeType !== 3) {
309+
if (node.nodeType !== TEXT_NODE) {
309310
// if an {expression} is empty during SSR, we need to insert an empty text node
310311
node.before((node = create_text()));
311312
set_hydrate_node(node);
@@ -360,7 +361,7 @@ export function props_id() {
360361
if (
361362
hydrating &&
362363
hydrate_node &&
363-
hydrate_node.nodeType === 8 &&
364+
hydrate_node.nodeType === COMMENT_NODE &&
364365
hydrate_node.textContent?.startsWith(`#`)
365366
) {
366367
const id = hydrate_node.textContent.substring(1);

0 commit comments

Comments
 (0)