chore: rid of nodeType magic numbers (#16208)

* chore: rid of nodeType magic numbers

* lint

---------

Co-authored-by: 7nik <kifiranet@gmail.com>
pull/16219/head
7nik 2 months ago committed by GitHub
parent 669a2233ee
commit 4db4ee5330
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -26,3 +26,8 @@ export const STATE_SYMBOL = Symbol('$state');
export const LEGACY_PROPS = Symbol('legacy props'); export const LEGACY_PROPS = Symbol('legacy props');
export const LOADING_ATTR_SYMBOL = Symbol(''); export const LOADING_ATTR_SYMBOL = Symbol('');
export const PROXY_PATH_SYMBOL = Symbol('proxy path'); export const PROXY_PATH_SYMBOL = Symbol('proxy path');
export const ELEMENT_NODE = 1;
export const TEXT_NODE = 3;
export const COMMENT_NODE = 8;
export const DOCUMENT_FRAGMENT_NODE = 11;

@ -1,4 +1,5 @@
/** @import { SourceLocation } from '#client' */ /** @import { SourceLocation } from '#client' */
import { COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, ELEMENT_NODE } from '#client/constants';
import { HYDRATION_END, HYDRATION_START, HYDRATION_START_ELSE } from '../../../constants.js'; import { HYDRATION_END, HYDRATION_START, HYDRATION_START_ELSE } from '../../../constants.js';
import { hydrating } from '../dom/hydration.js'; import { hydrating } from '../dom/hydration.js';
@ -12,7 +13,7 @@ export function add_locations(fn, filename, locations) {
return (/** @type {any[]} */ ...args) => { return (/** @type {any[]} */ ...args) => {
const dom = fn(...args); const dom = fn(...args);
var node = hydrating ? dom : dom.nodeType === 11 ? dom.firstChild : dom; var node = hydrating ? dom : dom.nodeType === DOCUMENT_FRAGMENT_NODE ? dom.firstChild : dom;
assign_locations(node, filename, locations); assign_locations(node, filename, locations);
return dom; return dom;
@ -45,13 +46,13 @@ function assign_locations(node, filename, locations) {
var depth = 0; var depth = 0;
while (node && i < locations.length) { while (node && i < locations.length) {
if (hydrating && node.nodeType === 8) { if (hydrating && node.nodeType === COMMENT_NODE) {
var comment = /** @type {Comment} */ (node); var comment = /** @type {Comment} */ (node);
if (comment.data === HYDRATION_START || comment.data === HYDRATION_START_ELSE) depth += 1; if (comment.data === HYDRATION_START || comment.data === HYDRATION_START_ELSE) depth += 1;
else if (comment.data[0] === HYDRATION_END) depth -= 1; else if (comment.data[0] === HYDRATION_END) depth -= 1;
} }
if (depth === 0 && node.nodeType === 1) { if (depth === 0 && node.nodeType === ELEMENT_NODE) {
assign_location(/** @type {Element} */ (node), filename, locations[i++]); assign_location(/** @type {Element} */ (node), filename, locations[i++]);
} }

@ -34,7 +34,7 @@ import {
} from '../../reactivity/effects.js'; } from '../../reactivity/effects.js';
import { source, mutable_source, internal_set } from '../../reactivity/sources.js'; import { source, mutable_source, internal_set } from '../../reactivity/sources.js';
import { array_from, is_array } from '../../../shared/utils.js'; import { array_from, is_array } from '../../../shared/utils.js';
import { INERT } from '#client/constants'; import { COMMENT_NODE, INERT } from '#client/constants';
import { queue_micro_task } from '../task.js'; import { queue_micro_task } from '../task.js';
import { active_effect, get } from '../../runtime.js'; import { active_effect, get } from '../../runtime.js';
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
@ -183,7 +183,7 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
if ( if (
hydrate_node.nodeType === 8 && hydrate_node.nodeType === COMMENT_NODE &&
/** @type {Comment} */ (hydrate_node).data === HYDRATION_END /** @type {Comment} */ (hydrate_node).data === HYDRATION_END
) { ) {
// The server rendered fewer items than expected, // The server rendered fewer items than expected,

@ -10,6 +10,7 @@ import { DEV } from 'esm-env';
import { dev_current_component_function } from '../../context.js'; import { dev_current_component_function } from '../../context.js';
import { get_first_child, get_next_sibling } from '../operations.js'; import { get_first_child, get_next_sibling } from '../operations.js';
import { active_effect } from '../../runtime.js'; import { active_effect } from '../../runtime.js';
import { COMMENT_NODE } from '#client/constants';
/** /**
* @param {Element} element * @param {Element} element
@ -67,7 +68,10 @@ export function html(node, get_value, svg = false, mathml = false, skip_warning
var next = hydrate_next(); var next = hydrate_next();
var last = next; var last = next;
while (next !== null && (next.nodeType !== 8 || /** @type {Comment} */ (next).data !== '')) { while (
next !== null &&
(next.nodeType !== COMMENT_NODE || /** @type {Comment} */ (next).data !== '')
) {
last = next; last = next;
next = /** @type {TemplateNode} */ (get_next_sibling(next)); next = /** @type {TemplateNode} */ (get_next_sibling(next));
} }

@ -1,7 +1,7 @@
/** @import { Snippet } from 'svelte' */ /** @import { Snippet } from 'svelte' */
/** @import { Effect, TemplateNode } from '#client' */ /** @import { Effect, TemplateNode } from '#client' */
/** @import { Getters } from '#shared' */ /** @import { Getters } from '#shared' */
import { EFFECT_TRANSPARENT } from '#client/constants'; import { EFFECT_TRANSPARENT, ELEMENT_NODE } from '#client/constants';
import { branch, block, destroy_effect, teardown } from '../../reactivity/effects.js'; import { branch, block, destroy_effect, teardown } from '../../reactivity/effects.js';
import { import {
dev_current_component_function, dev_current_component_function,
@ -102,7 +102,7 @@ export function createRawSnippet(fn) {
var fragment = create_fragment_from_html(html); var fragment = create_fragment_from_html(html);
element = /** @type {Element} */ (get_first_child(fragment)); element = /** @type {Element} */ (get_first_child(fragment));
if (DEV && (get_next_sibling(element) !== null || element.nodeType !== 1)) { if (DEV && (get_next_sibling(element) !== null || element.nodeType !== ELEMENT_NODE)) {
w.invalid_raw_snippet_render(); w.invalid_raw_snippet_render();
} }

@ -20,7 +20,7 @@ import { current_each_item, set_current_each_item } from './each.js';
import { active_effect } from '../../runtime.js'; import { active_effect } from '../../runtime.js';
import { component_context } from '../../context.js'; import { component_context } from '../../context.js';
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { EFFECT_TRANSPARENT } from '#client/constants'; import { EFFECT_TRANSPARENT, ELEMENT_NODE } from '#client/constants';
import { assign_nodes } from '../template.js'; import { assign_nodes } from '../template.js';
import { is_raw_text_element } from '../../../../utils.js'; import { is_raw_text_element } from '../../../../utils.js';
@ -51,7 +51,7 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio
/** @type {null | Element} */ /** @type {null | Element} */
var element = null; var element = null;
if (hydrating && hydrate_node.nodeType === 1) { if (hydrating && hydrate_node.nodeType === ELEMENT_NODE) {
element = /** @type {Element} */ (hydrate_node); element = /** @type {Element} */ (hydrate_node);
hydrate_next(); hydrate_next();
} }

@ -2,7 +2,7 @@
import { hydrate_node, hydrating, set_hydrate_node, set_hydrating } from '../hydration.js'; import { hydrate_node, hydrating, set_hydrate_node, set_hydrating } from '../hydration.js';
import { create_text, get_first_child, get_next_sibling } from '../operations.js'; import { create_text, get_first_child, get_next_sibling } from '../operations.js';
import { block } from '../../reactivity/effects.js'; import { block } from '../../reactivity/effects.js';
import { HEAD_EFFECT } from '#client/constants'; import { COMMENT_NODE, HEAD_EFFECT } from '#client/constants';
import { HYDRATION_START } from '../../../../constants.js'; import { HYDRATION_START } from '../../../../constants.js';
/** /**
@ -37,7 +37,8 @@ export function head(render_fn) {
while ( while (
head_anchor !== null && head_anchor !== null &&
(head_anchor.nodeType !== 8 || /** @type {Comment} */ (head_anchor).data !== HYDRATION_START) (head_anchor.nodeType !== COMMENT_NODE ||
/** @type {Comment} */ (head_anchor).data !== HYDRATION_START)
) { ) {
head_anchor = /** @type {TemplateNode} */ (get_next_sibling(head_anchor)); head_anchor = /** @type {TemplateNode} */ (get_next_sibling(head_anchor));
} }

@ -1,5 +1,6 @@
/** @import { TemplateNode } from '#client' */ /** @import { TemplateNode } from '#client' */
import { COMMENT_NODE } from '#client/constants';
import { import {
HYDRATION_END, HYDRATION_END,
HYDRATION_ERROR, HYDRATION_ERROR,
@ -87,7 +88,7 @@ export function remove_nodes() {
var node = hydrate_node; var node = hydrate_node;
while (true) { while (true) {
if (node.nodeType === 8) { if (node.nodeType === COMMENT_NODE) {
var data = /** @type {Comment} */ (node).data; var data = /** @type {Comment} */ (node).data;
if (data === HYDRATION_END) { if (data === HYDRATION_END) {
@ -109,7 +110,7 @@ export function remove_nodes() {
* @param {TemplateNode} node * @param {TemplateNode} node
*/ */
export function read_hydration_instruction(node) { export function read_hydration_instruction(node) {
if (!node || node.nodeType !== 8) { if (!node || node.nodeType !== COMMENT_NODE) {
w.hydration_mismatch(); w.hydration_mismatch();
throw HYDRATION_ERROR; throw HYDRATION_ERROR;
} }

@ -3,6 +3,7 @@ import { hydrate_node, hydrating, set_hydrate_node } from './hydration.js';
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { init_array_prototype_warnings } from '../dev/equality.js'; import { init_array_prototype_warnings } from '../dev/equality.js';
import { get_descriptor, is_extensible } from '../../shared/utils.js'; import { get_descriptor, is_extensible } from '../../shared/utils.js';
import { TEXT_NODE } from '#client/constants';
// export these for reference in the compiled code, making global name deduplication unnecessary // export these for reference in the compiled code, making global name deduplication unnecessary
/** @type {Window} */ /** @type {Window} */
@ -113,7 +114,7 @@ export function child(node, is_text) {
// Child can be null if we have an element with a single child, like `<p>{text}</p>`, where `text` is empty // Child can be null if we have an element with a single child, like `<p>{text}</p>`, where `text` is empty
if (child === null) { if (child === null) {
child = hydrate_node.appendChild(create_text()); child = hydrate_node.appendChild(create_text());
} else if (is_text && child.nodeType !== 3) { } else if (is_text && child.nodeType !== TEXT_NODE) {
var text = create_text(); var text = create_text();
child?.before(text); child?.before(text);
set_hydrate_node(text); set_hydrate_node(text);
@ -143,7 +144,7 @@ export function first_child(fragment, is_text) {
// if an {expression} is empty during SSR, there might be no // if an {expression} is empty during SSR, there might be no
// text node to hydrate — we must therefore create one // text node to hydrate — we must therefore create one
if (is_text && hydrate_node?.nodeType !== 3) { if (is_text && hydrate_node?.nodeType !== TEXT_NODE) {
var text = create_text(); var text = create_text();
hydrate_node?.before(text); hydrate_node?.before(text);
@ -174,11 +175,9 @@ export function sibling(node, count = 1, is_text = false) {
return next_sibling; return next_sibling;
} }
var type = next_sibling?.nodeType;
// if a sibling {expression} is empty during SSR, there might be no // if a sibling {expression} is empty during SSR, there might be no
// text node to hydrate — we must therefore create one // text node to hydrate — we must therefore create one
if (is_text && type !== 3) { if (is_text && next_sibling?.nodeType !== TEXT_NODE) {
var text = create_text(); var text = create_text();
// If the next sibling is `null` and we're handling text then it's because // If the next sibling is `null` and we're handling text then it's because
// the SSR content was empty for the text, so we need to generate a new text // the SSR content was empty for the text, so we need to generate a new text

@ -20,6 +20,7 @@ import {
TEMPLATE_USE_MATHML, TEMPLATE_USE_MATHML,
TEMPLATE_USE_SVG TEMPLATE_USE_SVG
} from '../../../constants.js'; } from '../../../constants.js';
import { COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, TEXT_NODE } from '#client/constants';
/** /**
* @param {TemplateNode} start * @param {TemplateNode} start
@ -264,7 +265,7 @@ function run_scripts(node) {
// scripts were SSR'd, in which case they will run // scripts were SSR'd, in which case they will run
if (hydrating) return node; if (hydrating) return node;
const is_fragment = node.nodeType === 11; const is_fragment = node.nodeType === DOCUMENT_FRAGMENT_NODE;
const scripts = const scripts =
/** @type {HTMLElement} */ (node).tagName === 'SCRIPT' /** @type {HTMLElement} */ (node).tagName === 'SCRIPT'
? [/** @type {HTMLScriptElement} */ (node)] ? [/** @type {HTMLScriptElement} */ (node)]
@ -305,7 +306,7 @@ export function text(value = '') {
var node = hydrate_node; var node = hydrate_node;
if (node.nodeType !== 3) { if (node.nodeType !== TEXT_NODE) {
// if an {expression} is empty during SSR, we need to insert an empty text node // if an {expression} is empty during SSR, we need to insert an empty text node
node.before((node = create_text())); node.before((node = create_text()));
set_hydrate_node(node); set_hydrate_node(node);
@ -360,7 +361,7 @@ export function props_id() {
if ( if (
hydrating && hydrating &&
hydrate_node && hydrate_node &&
hydrate_node.nodeType === 8 && hydrate_node.nodeType === COMMENT_NODE &&
hydrate_node.textContent?.startsWith(`#`) hydrate_node.textContent?.startsWith(`#`)
) { ) {
const id = hydrate_node.textContent.substring(1); const id = hydrate_node.textContent.substring(1);

@ -30,6 +30,7 @@ import * as w from './warnings.js';
import * as e from './errors.js'; import * as e from './errors.js';
import { assign_nodes } from './dom/template.js'; import { assign_nodes } from './dom/template.js';
import { is_passive_event } from '../../utils.js'; import { is_passive_event } from '../../utils.js';
import { COMMENT_NODE } from './constants.js';
/** /**
* This is normally true block effects should run their intro transitions * This is normally true block effects should run their intro transitions
@ -107,7 +108,7 @@ export function hydrate(component, options) {
var anchor = /** @type {TemplateNode} */ (get_first_child(target)); var anchor = /** @type {TemplateNode} */ (get_first_child(target));
while ( while (
anchor && anchor &&
(anchor.nodeType !== 8 || /** @type {Comment} */ (anchor).data !== HYDRATION_START) (anchor.nodeType !== COMMENT_NODE || /** @type {Comment} */ (anchor).data !== HYDRATION_START)
) { ) {
anchor = /** @type {TemplateNode} */ (get_next_sibling(anchor)); anchor = /** @type {TemplateNode} */ (get_next_sibling(anchor));
} }
@ -124,7 +125,7 @@ export function hydrate(component, options) {
if ( if (
hydrate_node === null || hydrate_node === null ||
hydrate_node.nodeType !== 8 || hydrate_node.nodeType !== COMMENT_NODE ||
/** @type {Comment} */ (hydrate_node).data !== HYDRATION_END /** @type {Comment} */ (hydrate_node).data !== HYDRATION_END
) { ) {
w.hydration_mismatch(); w.hydration_mismatch();

@ -1,3 +1,4 @@
import { COMMENT_NODE, ELEMENT_NODE, TEXT_NODE } from '#client/constants';
import { assert } from 'vitest'; import { assert } from 'vitest';
/** /**
@ -35,7 +36,7 @@ function clean_children(node, opts) {
}); });
for (let child of [...node.childNodes]) { for (let child of [...node.childNodes]) {
if (child.nodeType === 3) { if (child.nodeType === TEXT_NODE) {
let text = /** @type {Text} */ (child); let text = /** @type {Text} */ (child);
if ( if (
@ -49,7 +50,7 @@ function clean_children(node, opts) {
text.data = text.data.replace(/[^\S]+/g, ' '); text.data = text.data.replace(/[^\S]+/g, ' ');
if (previous && previous.nodeType === 3) { if (previous && previous.nodeType === TEXT_NODE) {
const prev = /** @type {Text} */ (previous); const prev = /** @type {Text} */ (previous);
prev.data += text.data; prev.data += text.data;
@ -62,22 +63,22 @@ function clean_children(node, opts) {
} }
} }
if (child.nodeType === 8 && !opts.preserveComments) { if (child.nodeType === COMMENT_NODE && !opts.preserveComments) {
// comment // comment
child.remove(); child.remove();
continue; continue;
} }
// add newlines for better readability and potentially recurse into children // add newlines for better readability and potentially recurse into children
if (child.nodeType === 1 || child.nodeType === 8) { if (child.nodeType === ELEMENT_NODE || child.nodeType === COMMENT_NODE) {
if (previous?.nodeType === 3) { if (previous?.nodeType === TEXT_NODE) {
const prev = /** @type {Text} */ (previous); const prev = /** @type {Text} */ (previous);
prev.data = prev.data.replace(/^[^\S]+$/, '\n'); prev.data = prev.data.replace(/^[^\S]+$/, '\n');
} else if (previous?.nodeType === 1 || previous?.nodeType === 8) { } else if (previous?.nodeType === ELEMENT_NODE || previous?.nodeType === COMMENT_NODE) {
node.insertBefore(document.createTextNode('\n'), child); node.insertBefore(document.createTextNode('\n'), child);
} }
if (child.nodeType === 1) { if (child.nodeType === ELEMENT_NODE) {
has_element_children = true; has_element_children = true;
clean_children(/** @type {Element} */ (child), opts); clean_children(/** @type {Element} */ (child), opts);
} }
@ -87,12 +88,12 @@ function clean_children(node, opts) {
} }
// collapse whitespace // collapse whitespace
if (node.firstChild && node.firstChild.nodeType === 3) { if (node.firstChild && node.firstChild.nodeType === TEXT_NODE) {
const text = /** @type {Text} */ (node.firstChild); const text = /** @type {Text} */ (node.firstChild);
text.data = text.data.trimStart(); text.data = text.data.trimStart();
} }
if (node.lastChild && node.lastChild.nodeType === 3) { if (node.lastChild && node.lastChild.nodeType === TEXT_NODE) {
const text = /** @type {Text} */ (node.lastChild); const text = /** @type {Text} */ (node.lastChild);
text.data = text.data.trimEnd(); text.data = text.data.trimEnd();
} }

@ -1,5 +1,8 @@
/** @import { assert } from 'vitest' */ /** @import { assert } from 'vitest' */
/** @import { CompileOptions, Warning } from '#compiler' */ /** @import { CompileOptions, Warning } from '#compiler' */
import { ELEMENT_NODE } from '#client/constants';
/** /**
* @param {any} a * @param {any} a
* @param {any} b * @param {any} b
@ -102,7 +105,7 @@ function normalize_children(node) {
} }
for (let child of [...node.childNodes]) { for (let child of [...node.childNodes]) {
if (child.nodeType === 1 /* Element */) { if (child.nodeType === ELEMENT_NODE) {
normalize_children(child); normalize_children(child);
} }
} }

@ -1,3 +1,4 @@
import { COMMENT_NODE } from '#client/constants';
import { ok, test } from '../../test'; import { ok, test } from '../../test';
export default test({ export default test({
@ -41,7 +42,7 @@ export default test({
// get all childNodes of template3 except comments // get all childNodes of template3 except comments
let childNodes = []; let childNodes = [];
for (const node of template3.content.childNodes) { for (const node of template3.content.childNodes) {
if (node.nodeType !== 8) { if (node.nodeType !== COMMENT_NODE) {
childNodes.push(/** @type {Element} */ (node)); childNodes.push(/** @type {Element} */ (node));
} }
} }

Loading…
Cancel
Save