From 72b32640e236e57ffa8bd7baf4ae54ffcdb361ff Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 8 Jan 2024 15:22:12 -0800 Subject: [PATCH 1/4] perf: save some bytes with more concise comparisons --- .../svelte/src/internal/client/runtime.js | 228 ++++++++---------- packages/svelte/src/store/utils.js | 2 +- 2 files changed, 103 insertions(+), 127 deletions(-) diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 8631529e62a0..cb04f9c96ba8 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -111,7 +111,7 @@ export function set_is_ssr(ssr) { */ function is_runes(context) { const component_context = context || current_component_context; - return component_context !== null && component_context.r; + return !!component_context && component_context.r; } /** @@ -128,7 +128,7 @@ export function batch_inspect(target, prop, receiver) { return Reflect.apply(value, receiver, arguments); } finally { is_batching_effect = previously_batching_effect; - if (last_inspected_signal !== null) { + if (last_inspected_signal) { for (const fn of last_inspected_signal.inspect) fn(); last_inspected_signal = null; } @@ -256,10 +256,10 @@ function create_computation_signal(flags, value, block) { */ function push_reference(target_signal, ref_signal) { const references = target_signal.r; - if (references === null) { - target_signal.r = [ref_signal]; - } else { + if (references) { references.push(ref_signal); + } else { + target_signal.r = [ref_signal]; } } @@ -270,30 +270,30 @@ function push_reference(target_signal, ref_signal) { */ function is_signal_dirty(signal) { const flags = signal.f; - if ((flags & DIRTY) !== 0 || signal.v === UNINITIALIZED) { + if (flags & DIRTY || signal.v === UNINITIALIZED) { return true; } - if ((flags & MAYBE_DIRTY) !== 0) { + if (flags & MAYBE_DIRTY) { const dependencies = /** @type {import('./types.js').ComputationSignal} **/ (signal).d; - if (dependencies !== null) { + if (dependencies) { const length = dependencies.length; let i; for (i = 0; i < length; i++) { const dependency = dependencies[i]; - if ((dependency.f & MAYBE_DIRTY) !== 0 && !is_signal_dirty(dependency)) { + if (dependency.f & MAYBE_DIRTY && !is_signal_dirty(dependency)) { set_signal_status(dependency, CLEAN); continue; } // The flags can be marked as dirty from the above is_signal_dirty call. - if ((dependency.f & DIRTY) !== 0) { - if ((dependency.f & DERIVED) !== 0) { + if (dependency.f & DIRTY) { + if (dependency.f & DERIVED) { update_derived( /** @type {import('./types.js').ComputationSignal} **/ (dependency), true ); // Might have been mutated from above get. - if ((signal.f & DIRTY) !== 0) { + if (signal.f & DIRTY) { return true; } } else { @@ -320,7 +320,7 @@ function execute_signal_fn(signal) { const previous_block = current_block; const previous_component_context = current_component_context; const previous_skip_consumer = current_skip_consumer; - const is_render_effect = (signal.f & RENDER_EFFECT) !== 0; + const is_render_effect = signal.f & RENDER_EFFECT; const previous_untracking = current_untracking; current_dependencies = /** @type {null | import('./types.js').Signal[]} */ (null); current_dependencies_index = 0; @@ -328,11 +328,11 @@ function execute_signal_fn(signal) { current_consumer = signal; current_block = signal.b; current_component_context = signal.x; - current_skip_consumer = current_effect === null && (signal.f & UNOWNED) !== 0; + current_skip_consumer = !current_effect && !!(signal.f & UNOWNED); current_untracking = false; // Render effects are invoked when the UI is about to be updated - run beforeUpdate at that point - if (is_render_effect && current_component_context?.u != null) { + if (is_render_effect && current_component_context?.u) { // update_callbacks.execute() current_component_context.u.e(); } @@ -348,14 +348,13 @@ function execute_signal_fn(signal) { } let dependencies = /** @type {import('./types.js').Signal[]} **/ (signal.d); - if (current_dependencies !== null) { + if (current_dependencies) { let i; - if (dependencies !== null) { + if (dependencies) { // Include any dependencies up until the current_dependencies_index. - const full_dependencies = - current_dependencies_index === 0 - ? dependencies - : dependencies.slice(0, current_dependencies_index).concat(current_dependencies); + const full_dependencies = !current_dependencies_index + ? dependencies + : dependencies.slice(0, current_dependencies_index).concat(current_dependencies); const dep_length = full_dependencies.length; // If we have more than 16 elements in the array then use a Set for faster performance // TODO: evaluate if we should always just use a Set or not here? @@ -364,7 +363,7 @@ function execute_signal_fn(signal) { for (i = current_dependencies_index; i < dep_length; i++) { const dependency = full_dependencies[i]; if ( - (current_dependencies_set !== null && !current_dependencies_set.has(dependency)) || + (current_dependencies_set && !current_dependencies_set.has(dependency)) || !full_dependencies.includes(dependency) ) { remove_consumer(signal, dependency, false); @@ -372,7 +371,7 @@ function execute_signal_fn(signal) { } } - if (dependencies !== null && current_dependencies_index > 0) { + if (dependencies && current_dependencies_index > 0) { dependencies.length = current_dependencies_index + current_dependencies.length; for (i = 0; i < current_dependencies.length; i++) { dependencies[current_dependencies_index + i] = current_dependencies[i]; @@ -388,14 +387,14 @@ function execute_signal_fn(signal) { const dependency = dependencies[i]; const consumers = dependency.c; - if (consumers === null) { + if (!consumers) { dependency.c = [signal]; } else if (consumers[consumers.length - 1] !== signal) { consumers.push(signal); } } } - } else if (dependencies !== null && current_dependencies_index < dependencies.length) { + } else if (dependencies && current_dependencies_index < dependencies.length) { remove_consumers(signal, current_dependencies_index, false); dependencies.length = current_dependencies_index; } @@ -422,11 +421,11 @@ function execute_signal_fn(signal) { function remove_consumer(signal, dependency, remove_unowned) { const consumers = dependency.c; let consumers_length = 0; - if (consumers !== null) { + if (consumers) { consumers_length = consumers.length - 1; const index = consumers.indexOf(signal); if (index !== -1) { - if (consumers_length === 0) { + if (!consumers_length) { dependency.c = null; } else { // Swap with last element and then remove. @@ -435,7 +434,7 @@ function remove_consumer(signal, dependency, remove_unowned) { } } } - if (remove_unowned && consumers_length === 0 && (dependency.f & UNOWNED) !== 0) { + if (remove_unowned && !consumers_length && dependency.f & UNOWNED) { // If the signal is unowned then we need to make sure to change it to dirty. set_signal_status(dependency, DIRTY); remove_consumers( @@ -455,13 +454,13 @@ function remove_consumer(signal, dependency, remove_unowned) { */ function remove_consumers(signal, start_index, remove_unowned) { const dependencies = signal.d; - if (dependencies !== null) { - const active_dependencies = start_index === 0 ? null : dependencies.slice(0, start_index); + if (dependencies) { + const active_dependencies = !start_index ? null : dependencies.slice(0, start_index); let i; for (i = start_index; i < dependencies.length; i++) { const dependency = dependencies[i]; // Avoid removing a consumer if we know that it is active (start_index will not be 0) - if (active_dependencies === null || !active_dependencies.includes(dependency)) { + if (!active_dependencies || !active_dependencies.includes(dependency)) { remove_consumer(signal, dependency, remove_unowned); } } @@ -476,11 +475,11 @@ function remove_consumers(signal, start_index, remove_unowned) { function destroy_references(signal) { const references = signal.r; signal.r = null; - if (references !== null) { + if (references) { let i; for (i = 0; i < references.length; i++) { const reference = references[i]; - if ((reference.f & IS_EFFECT) !== 0) { + if (reference.f & IS_EFFECT) { destroy_signal(reference); } else { remove_consumers(reference, 0, true); @@ -490,26 +489,12 @@ function destroy_references(signal) { } } -/** - * @param {import('./types.js').Block} block - * @param {unknown} error - * @returns {void} - */ -function report_error(block, error) { - /** @type {import('./types.js').Block | null} */ - let current_block = block; - - if (current_block !== null) { - throw error; - } -} - /** * @param {import('./types.js').EffectSignal} signal * @returns {void} */ export function execute_effect(signal) { - if ((signal.f & DESTROYED) !== 0) { + if (signal.f & DESTROYED) { return; } const teardown = signal.v; @@ -518,7 +503,7 @@ export function execute_effect(signal) { try { destroy_references(signal); - if (teardown !== null) { + if (teardown) { teardown(); } const possible_teardown = execute_signal_fn(signal); @@ -527,18 +512,14 @@ export function execute_effect(signal) { } } catch (error) { const block = signal.b; - if (block !== null) { - report_error(block, error); - } else { - throw error; - } + throw error; } finally { current_effect = previous_effect; } const component_context = signal.x; if ( is_runes(component_context) && // Don't rerun pre effects more than once to accomodate for "$: only runs once" behavior - (signal.f & PRE_EFFECT) !== 0 && + signal.f & PRE_EFFECT && current_queued_pre_and_render_effects.length > 0 ) { flush_local_pre_effects(component_context); @@ -570,11 +551,11 @@ function flush_queued_effects(effects) { for (i = 0; i < length; i++) { const signal = effects[i]; const flags = signal.f; - if ((flags & (DESTROYED | INERT)) === 0) { + if (!(flags & (DESTROYED | INERT))) { if (is_signal_dirty(signal)) { set_signal_status(signal, CLEAN); execute_effect(signal); - } else if ((flags & MAYBE_DIRTY) !== 0) { + } else if (flags & MAYBE_DIRTY) { set_signal_status(signal, CLEAN); } } @@ -621,7 +602,7 @@ export function schedule_effect(signal, sync) { queueMicrotask(process_microtask); } } - if ((flags & EFFECT) !== 0) { + if (flags & EFFECT) { current_queued_effects.push(signal); } else { current_queued_pre_and_render_effects.push(signal); @@ -667,7 +648,7 @@ export function flush_local_render_effects() { const effects = []; for (let i = 0; i < current_queued_pre_and_render_effects.length; i++) { const effect = current_queued_pre_and_render_effects[i]; - if ((effect.f & RENDER_EFFECT) !== 0 && effect.x === current_component_context) { + if (effect.f & RENDER_EFFECT && effect.x === current_component_context) { effects.push(effect); current_queued_pre_and_render_effects.splice(i, 1); i--; @@ -684,7 +665,7 @@ export function flush_local_pre_effects(context) { const effects = []; for (let i = 0; i < current_queued_pre_and_render_effects.length; i++) { const effect = current_queued_pre_and_render_effects[i]; - if ((effect.f & PRE_EFFECT) !== 0 && effect.x === context) { + if (effect.f & PRE_EFFECT && effect.x === context) { effects.push(effect); current_queued_pre_and_render_effects.splice(i, 1); i--; @@ -714,7 +695,7 @@ export function flushSync(fn) { current_queued_effects = effects; flush_queued_effects(previous_queued_pre_and_render_effects); flush_queued_effects(previous_queued_effects); - if (fn !== undefined) { + if (fn) { fn(); } if (current_queued_pre_and_render_effects.length > 0 || effects.length > 0) { @@ -756,10 +737,7 @@ function update_derived(signal, force_schedule) { updating_derived = true; const value = execute_signal_fn(signal); updating_derived = previous_updating_derived; - const status = - current_skip_consumer || (current_effect === null && (signal.f & UNOWNED) !== 0) - ? DIRTY - : CLEAN; + const status = current_skip_consumer || (!current_effect && signal.f & UNOWNED) ? DIRTY : CLEAN; set_signal_status(signal, status); const equals = /** @type {import('./types.js').EqualsFunctions} */ (signal.e); if (!equals(value, signal.v)) { @@ -821,7 +799,7 @@ export function store_get(store, store_name, stores) { * @param {import('./types.js').SourceSignal} source */ function connect_store_to_signal(store, source) { - if (store == null) { + if (!store) { set(source, undefined); return EMPTY_FUNC; } @@ -877,7 +855,7 @@ export function get(signal) { } const flags = signal.f; - if ((flags & DESTROYED) !== 0) { + if (flags & DESTROYED) { return signal.v; } @@ -886,31 +864,31 @@ export function get(signal) { } // Register the dependency on the current consumer signal. - if (current_consumer !== null && (current_consumer.f & MANAGED) === 0 && !current_untracking) { - const unowned = (current_consumer.f & UNOWNED) !== 0; + if (current_consumer && !(current_consumer.f & MANAGED) && !current_untracking) { + const unowned = current_consumer.f & UNOWNED; const dependencies = current_consumer.d; if ( - current_dependencies === null && - dependencies !== null && + !current_dependencies && + dependencies && dependencies[current_dependencies_index] === signal && - !(unowned && current_effect !== null) + !(unowned && current_effect) ) { current_dependencies_index++; } else if ( - dependencies === null || - current_dependencies_index === 0 || + !dependencies || + !current_dependencies_index || dependencies[current_dependencies_index - 1] !== signal ) { - if (current_dependencies === null) { + if (!current_dependencies) { current_dependencies = [signal]; } else if (signal !== current_dependencies[current_dependencies.length - 1]) { current_dependencies.push(signal); } } if ( - current_untracked_writes !== null && - current_effect !== null && - (current_effect.f & CLEAN) !== 0 && + current_untracked_writes && + current_effect && + current_effect.f & CLEAN && current_untracked_writes.includes(signal) ) { set_signal_status(current_effect, DIRTY); @@ -918,7 +896,7 @@ export function get(signal) { } } - if ((flags & DERIVED) !== 0 && is_signal_dirty(signal)) { + if (flags & DERIVED && is_signal_dirty(signal)) { if (DEV) { // we want to avoid tracking indirect dependencies const previous_inspect_fn = inspect_fn; @@ -1013,15 +991,15 @@ export function mutate_store(store, expression, new_value) { */ export function mark_subtree_inert(signal, inert) { const flags = signal.f; - const is_already_inert = (flags & INERT) !== 0; + const is_already_inert = !!(flags & INERT); if (is_already_inert !== inert) { signal.f ^= INERT; - if (!inert && (flags & IS_EFFECT) !== 0 && (flags & CLEAN) === 0) { + if (!inert && flags & IS_EFFECT && !(flags & CLEAN)) { schedule_effect(/** @type {import('./types.js').EffectSignal} */ (signal), false); } } const references = signal.r; - if (references !== null) { + if (references) { let i; for (i = 0; i < references.length; i++) { mark_subtree_inert(references[i], inert); @@ -1039,14 +1017,14 @@ export function mark_subtree_inert(signal, inert) { function mark_signal_consumers(signal, to_status, force_schedule) { const runes = is_runes(signal.x); const consumers = signal.c; - if (consumers !== null) { + if (consumers) { const length = consumers.length; let i; for (i = 0; i < length; i++) { const consumer = consumers[i]; const flags = consumer.f; - const unowned = (flags & UNOWNED) !== 0; - const dirty = (flags & DIRTY) !== 0; + const unowned = flags & UNOWNED; + const dirty = flags & DIRTY; // We skip any effects that are already dirty (but not unowned). Additionally, we also // skip if the consumer is the same as the current effect (except if we're not in runes or we // are in force schedule mode). @@ -1057,8 +1035,8 @@ function mark_signal_consumers(signal, to_status, force_schedule) { // If the signal is not clean, then skip over it – with the exception of unowned signals that // are already dirty. Unowned signals might be dirty because they are not captured as part of an // effect. - if ((flags & CLEAN) !== 0 || (dirty && unowned)) { - if ((consumer.f & IS_EFFECT) !== 0) { + if (flags & CLEAN || (dirty && unowned)) { + if (consumer.f & IS_EFFECT) { schedule_effect(/** @type {import('./types.js').EffectSignal} */ (consumer), false); } else { mark_signal_consumers(consumer, MAYBE_DIRTY, force_schedule); @@ -1078,9 +1056,9 @@ export function set_signal_value(signal, value) { if ( !current_untracking && !ignore_mutation_validation && - current_consumer !== null && + current_consumer && is_runes(signal.x) && - (current_consumer.f & DERIVED) !== 0 + current_consumer.f & DERIVED ) { throw new Error( 'ERR_SVELTE_UNSAFE_MUTATION' + @@ -1092,7 +1070,7 @@ export function set_signal_value(signal, value) { ); } if ( - (signal.f & SOURCE) !== 0 && + signal.f & SOURCE && !(/** @type {import('./types.js').EqualsFunctions} */ (signal.e)(value, signal.v)) ) { const component_context = signal.x; @@ -1107,18 +1085,18 @@ export function set_signal_value(signal, value) { // if ( is_runes(component_context) && - current_effect !== null && - current_effect.c === null && - (current_effect.f & CLEAN) !== 0 + current_effect && + !current_effect.c && + current_effect.f & CLEAN ) { - if (current_dependencies !== null && current_dependencies.includes(signal)) { + if (current_dependencies && current_dependencies.includes(signal)) { set_signal_status(current_effect, DIRTY); schedule_effect(current_effect, false); } else { - if (current_untracked_writes === null) { - current_untracked_writes = [signal]; - } else { + if (current_untracked_writes) { current_untracked_writes.push(signal); + } else { + current_untracked_writes = [signal]; } } } @@ -1126,9 +1104,9 @@ export function set_signal_value(signal, value) { // If we have afterUpdates locally on the component, but we're within a render effect // then we will need to manually invoke the beforeUpdate/afterUpdate logic. // TODO: should we put this being a is_runes check and only run it in non-runes mode? - if (current_effect === null && current_queued_pre_and_render_effects.length === 0) { + if (!current_effect && !current_queued_pre_and_render_effects.length) { const update_callbacks = component_context?.u; - if (update_callbacks != null) { + if (update_callbacks) { run_all(update_callbacks.b); const managed = managed_effect(() => { destroy_signal(managed); @@ -1170,14 +1148,14 @@ export function destroy_signal(signal) { signal.c = null; set_signal_status(signal, DESTROYED); - if (destroy !== null) { + if (destroy) { if (is_array(destroy)) { run_all(destroy); } else { destroy(); } } - if (teardown !== null && (flags & IS_EFFECT) !== 0) { + if (teardown && flags & IS_EFFECT) { teardown(); } } @@ -1189,7 +1167,7 @@ export function destroy_signal(signal) { */ /*#__NO_SIDE_EFFECTS__*/ export function derived(init) { - const is_unowned = current_effect === null; + const is_unowned = !current_effect; const flags = is_unowned ? DERIVED | UNOWNED : DERIVED; const signal = /** @type {import('./types.js').ComputationSignal} */ ( create_computation_signal(flags | CLEAN, UNINITIALIZED, current_block) @@ -1260,7 +1238,7 @@ function internal_create_effect(type, init, sync, block, schedule) { if (schedule) { schedule_effect(signal, sync); } - if (current_effect !== null && (type & MANAGED) === 0) { + if (current_effect && !(type & MANAGED)) { push_reference(current_effect, signal); } return signal; @@ -1270,7 +1248,7 @@ function internal_create_effect(type, init, sync, block, schedule) { * @returns {boolean} */ export function effect_active() { - return current_effect ? (current_effect.f & MANAGED) === 0 : false; + return current_effect ? !(current_effect.f & MANAGED) : false; } /** @@ -1278,16 +1256,14 @@ export function effect_active() { * @returns {import('./types.js').EffectSignal} */ export function user_effect(init) { - if (current_effect === null) { + if (!current_effect) { throw new Error( 'ERR_SVELTE_ORPHAN_EFFECT' + (DEV ? ': The Svelte $effect rune can only be used during component initialisation.' : '') ); } const apply_component_effect_heuristics = - current_effect.f & RENDER_EFFECT && - current_component_context !== null && - !current_component_context.m; + current_effect.f & RENDER_EFFECT && current_component_context && !current_component_context.m; const effect = internal_create_effect( EFFECT, init, @@ -1298,7 +1274,7 @@ export function user_effect(init) { if (apply_component_effect_heuristics) { let effects = /** @type {import('./types.js').ComponentContext} */ (current_component_context) .e; - if (effects === null) { + if (!effects) { effects = /** @type {import('./types.js').ComponentContext} */ (current_component_context).e = []; } @@ -1348,7 +1324,7 @@ export function managed_pre_effect(init, sync) { * @returns {import('./types.js').EffectSignal} */ export function pre_effect(init) { - if (current_effect === null) { + if (!current_effect) { throw new Error( 'ERR_SVELTE_ORPHAN_EFFECT' + (DEV @@ -1356,7 +1332,7 @@ export function pre_effect(init) { : '') ); } - const sync = current_effect !== null && (current_effect.f & RENDER_EFFECT) !== 0; + const sync = current_effect && !!(current_effect.f & RENDER_EFFECT); return internal_create_effect( PRE_EFFECT, () => { @@ -1417,7 +1393,7 @@ export function managed_render_effect(init, block = current_block, sync = true) */ export function push_destroy_fn(signal, destroy_fn) { let destroy = signal.y; - if (destroy === null) { + if (!destroy) { signal.y = destroy_fn; } else if (is_array(destroy)) { destroy.push(destroy_fn); @@ -1445,7 +1421,7 @@ export function set_signal_status(signal, status) { export function is_signal(val) { return ( typeof val === 'object' && - val !== null && + val && typeof (/** @type {import('./types.js').Signal} */ (val).f) === 'number' ); } @@ -1459,7 +1435,7 @@ export function is_signal(val) { export function is_lazy_property(val) { return ( typeof val === 'object' && - val !== null && + val && /** @type {import('./types.js').LazyProperty} */ (val).t === LAZY_PROPERTY ); } @@ -1472,7 +1448,7 @@ export function is_lazy_property(val) { export function is_store(val) { return ( typeof val === 'object' && - val !== null && + !!val && typeof (/** @type {import('./types.js').Store} */ (val).subscribe) === 'function' ); } @@ -1488,8 +1464,8 @@ export function is_store(val) { * @returns {(() => V | ((arg: V) => V) | ((arg: V, mutation: boolean) => V))} */ export function prop(props, key, flags, initial) { - var immutable = (flags & PROPS_IS_IMMUTABLE) !== 0; - var runes = (flags & PROPS_IS_RUNES) !== 0; + var immutable = flags & PROPS_IS_IMMUTABLE; + var runes = flags & PROPS_IS_RUNES; var setter = get_descriptor(props, key)?.set; if (DEV && setter && runes && initial !== undefined) { @@ -1501,7 +1477,7 @@ export function prop(props, key, flags, initial) { if (prop_value === undefined && initial !== undefined) { // @ts-expect-error would need a cumbersome method overload to type this - if ((flags & PROPS_IS_LAZY_INITIAL) !== 0) initial = initial(); + if (flags & PROPS_IS_LAZY_INITIAL) initial = initial(); if (DEV && runes) { initial = readonly(proxy(/** @type {any} */ (initial))); @@ -1519,7 +1495,7 @@ export function prop(props, key, flags, initial) { }; // easy mode — prop is never written to - if ((flags & PROPS_IS_UPDATED) === 0) { + if (!(flags & PROPS_IS_UPDATED)) { return getter; } @@ -1614,14 +1590,14 @@ export function safe_equal(a, b) { /** @returns {Map} */ export function get_or_init_context_map() { const component_context = current_component_context; - if (component_context === null) { + if (!component_context) { throw new Error( 'ERR_SVELTE_ORPHAN_CONTEXT' + (DEV ? 'Context can only be used during component initialisation.' : '') ); } let context_map = component_context.c; - if (context_map === null) { + if (!context_map) { const parent_context = get_parent_context(component_context); context_map = component_context.c = new Map(parent_context || undefined); } @@ -1634,9 +1610,9 @@ export function get_or_init_context_map() { */ function get_parent_context(component_context) { let parent = component_context.p; - while (parent !== null) { + while (parent) { const context_map = parent.c; - if (context_map !== null) { + if (context_map) { return context_map; } parent = parent.p; @@ -1818,12 +1794,12 @@ export function push(props, runes = false) { */ export function pop(accessors) { const context_stack_item = current_component_context; - if (context_stack_item !== null) { - if (accessors !== undefined) { + if (context_stack_item) { + if (accessors) { context_stack_item.a = accessors; } const effects = context_stack_item.e; - if (effects !== null) { + if (effects) { context_stack_item.e = null; for (let i = 0; i < effects.length; i++) { schedule_effect(effects[i], false); diff --git a/packages/svelte/src/store/utils.js b/packages/svelte/src/store/utils.js index 979a2518d8f0..bfc8bb3c1a44 100644 --- a/packages/svelte/src/store/utils.js +++ b/packages/svelte/src/store/utils.js @@ -8,7 +8,7 @@ import { EMPTY_FUNC } from '../internal/common.js'; * @returns {() => void} */ export function subscribe_to_store(store, run, invalidate) { - if (store == null) { + if (!store) { // @ts-expect-error run(undefined); From 87ac2a8f7fb40c171b8e246fe27a2a129bd73150 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 8 Jan 2024 16:02:13 -0800 Subject: [PATCH 2/4] i guess there's more client code besides just runtime.js --- packages/svelte/src/internal/client/each.js | 46 +++---- .../svelte/src/internal/client/hydration.js | 6 +- .../svelte/src/internal/client/operations.js | 10 +- .../svelte/src/internal/client/reconciler.js | 16 +-- packages/svelte/src/internal/client/render.js | 119 +++++++++--------- 5 files changed, 99 insertions(+), 98 deletions(-) diff --git a/packages/svelte/src/internal/client/each.js b/packages/svelte/src/internal/client/each.js index c51af7857212..0602ed8ff0e1 100644 --- a/packages/svelte/src/internal/client/each.js +++ b/packages/svelte/src/internal/client/each.js @@ -74,8 +74,8 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re transition.f(() => { transitions.delete(transition); if (transitions.size === 0) { - if (fallback.e !== null) { - if (fallback.d !== null) { + if (fallback.e) { + if (fallback.d) { remove(fallback.d); fallback.d = null; } @@ -98,7 +98,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re const effect = render_effect( () => { const dom = block.d; - if (dom !== null) { + if (dom) { remove(dom); block.d = null; } @@ -136,24 +136,24 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re : maybe_array == null ? [] : Array.from(maybe_array); - if (key_fn !== null) { + if (key_fn) { keys = array.map(key_fn); } else if ((flags & EACH_KEYED) === 0) { array.map(no_op); } const length = array.length; - if (fallback_fn !== null) { + if (fallback_fn) { if (length === 0) { - if (block.v.length !== 0 || render === null) { + if (block.v.length !== 0 || !render) { clear_each(block); create_fallback_effect(); return; } - } else if (block.v.length === 0 && current_fallback !== null) { + } else if (block.v.length === 0 && current_fallback) { const fallback = current_fallback; const transitions = fallback.s; if (transitions.size === 0) { - if (fallback.d !== null) { + if (fallback.d) { remove(fallback.d); fallback.d = null; } @@ -162,7 +162,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re } } } - if (render !== null) { + if (render) { execute_effect(render); } }, @@ -177,13 +177,13 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re const anchor_node = block.a; const is_controlled = (flags & EACH_IS_CONTROLLED) !== 0; let fallback = current_fallback; - while (fallback !== null) { + while (fallback) { const dom = fallback.d; - if (dom !== null) { + if (dom) { remove(dom); } const effect = fallback.e; - if (effect !== null) { + if (effect) { destroy_signal(effect); } fallback = fallback.p; @@ -280,7 +280,7 @@ function reconcile_indexed_array( } else { var item; b_blocks = Array(b); - if (current_hydration_fragment !== null) { + if (current_hydration_fragment) { /** @type {Node} */ var hydrating_node = current_hydration_fragment[0]; for (; index < length; index++) { @@ -350,7 +350,7 @@ function reconcile_tracked_array( keys ) { var a_blocks = each_block.v; - const is_computed_key = keys !== null; + const is_computed_key = !!keys; var active_transitions = each_block.s; /** @type {number | void} */ @@ -384,7 +384,7 @@ function reconcile_tracked_array( var item; var idx; b_blocks = Array(b); - if (current_hydration_fragment !== null) { + if (current_hydration_fragment) { var fragment; /** @type {Node} */ @@ -469,7 +469,7 @@ function reconcile_tracked_array( } else if (start > b_end) { b = start; do { - if ((block = a_blocks[b++]) !== null) { + if ((block = a_blocks[b++])) { destroy_each_item_block(block, active_transitions, apply_transitions); } } while (b <= a_end); @@ -493,7 +493,7 @@ function reconcile_tracked_array( pos = pos < a ? a : MOVED_BLOCK; sources[a - start] = b; b_blocks[a] = block; - } else if (block !== null) { + } else if (block) { destroy_each_item_block(block, active_transitions, apply_transitions); } } @@ -628,7 +628,7 @@ function mark_lis(a) { function insert_each_item_block(block, dom, is_controlled, sibling) { var current = /** @type {import('./types.js').TemplateNode} */ (block.d); - if (sibling === null) { + if (!sibling) { if (is_controlled) { return insert(current, /** @type {Element} */ (dom), null); } else { @@ -668,7 +668,7 @@ function destroy_active_transition_blocks(active_transitions) { for (; i < length; i++) { block = active_transitions[i]; transition = block.r; - if (transition !== null) { + if (!transition) { block.r = null; destroy_each_item_block(block, null, false); } @@ -714,7 +714,7 @@ function update_each_item_block(block, item, index, type) { const index_is_reactive = (type & EACH_INDEX_REACTIVE) !== 0; // Handle each item animations const each_animation = block.a; - if (transitions !== null && (type & EACH_KEYED) !== 0 && each_animation !== null) { + if (transitions && (type & EACH_KEYED) !== 0 && each_animation) { each_animation(block, transitions, index, index_is_reactive); } if (index_is_reactive) { @@ -739,7 +739,7 @@ export function destroy_each_item_block( ) { const transitions = block.s; - if (apply_transitions && transitions !== null) { + if (apply_transitions && transitions) { // We might have pending key transitions, if so remove them first for (let other of transitions) { if (other.r === 'key') { @@ -750,14 +750,14 @@ export function destroy_each_item_block( block.s = null; } else { trigger_transitions(transitions, 'out'); - if (transition_block !== null) { + if (transition_block) { transition_block.push(block); } return; } } const dom = block.d; - if (!controlled && dom !== null) { + if (!controlled && dom) { remove(dom); } destroy_signal(/** @type {import('./types.js').EffectSignal} */ (block.e)); diff --git a/packages/svelte/src/internal/client/hydration.js b/packages/svelte/src/internal/client/hydration.js index e7643a57cc60..3ca337fc3fa3 100644 --- a/packages/svelte/src/internal/client/hydration.js +++ b/packages/svelte/src/internal/client/hydration.js @@ -27,7 +27,7 @@ export function get_hydration_fragment(node) { /** @type {null | string} */ let target_depth = null; - while (current_node !== null) { + while (current_node) { const node_type = current_node.nodeType; const next_sibling = current_node.nextSibling; if (node_type === 8) { @@ -61,7 +61,7 @@ export function get_hydration_fragment(node) { export function hydrate_block_anchor(anchor_node, is_controlled) { /** @type {Node} */ let target_node = anchor_node; - if (current_hydration_fragment !== null) { + if (current_hydration_fragment) { if (is_controlled) { target_node = /** @type {Node} */ (target_node.firstChild); } @@ -79,7 +79,7 @@ export function hydrate_block_anchor(anchor_node, is_controlled) { set_current_hydration_fragment(fragment); } else { const first_child = /** @type {Element | null} */ (target_node.firstChild); - set_current_hydration_fragment(first_child === null ? [] : [first_child]); + set_current_hydration_fragment(!first_child ? [] : [first_child]); } } } diff --git a/packages/svelte/src/internal/client/operations.js b/packages/svelte/src/internal/client/operations.js index e8cbe2271874..d40db2d359d3 100644 --- a/packages/svelte/src/internal/client/operations.js +++ b/packages/svelte/src/internal/client/operations.js @@ -166,9 +166,9 @@ export function clone_node(node, deep) { /*#__NO_SIDE_EFFECTS__*/ export function child(node) { const child = first_child_get.call(node); - if (current_hydration_fragment !== null) { + if (current_hydration_fragment) { // Child can be null if we have an element with a single child, like `

{text}

`, where `text` is empty - if (child === null) { + if (!child) { const text = document.createTextNode(''); node.appendChild(text); return text; @@ -186,9 +186,9 @@ export function child(node) { */ /*#__NO_SIDE_EFFECTS__*/ export function child_frag(node) { - if (current_hydration_fragment !== null) { + if (current_hydration_fragment) { const first_node = /** @type {Node[]} */ (node)[0]; - if (current_hydration_fragment !== null && first_node !== null) { + if (current_hydration_fragment && first_node) { return capture_fragment_from_node(first_node); } return first_node; @@ -204,7 +204,7 @@ export function child_frag(node) { /*#__NO_SIDE_EFFECTS__*/ export function sibling(node) { const next_sibling = next_sibling_get.call(node); - if (current_hydration_fragment !== null && next_sibling !== null) { + if (current_hydration_fragment && next_sibling) { return capture_fragment_from_node(next_sibling); } return next_sibling; diff --git a/packages/svelte/src/internal/client/reconciler.js b/packages/svelte/src/internal/client/reconciler.js index 80c221e448c5..f3bb5207fe2d 100644 --- a/packages/svelte/src/internal/client/reconciler.js +++ b/packages/svelte/src/internal/client/reconciler.js @@ -21,18 +21,18 @@ export function insert(current, parent_element, sibling) { var node; for (; i < current.length; i++) { node = current[i]; - if (sibling === null) { - append_child(/** @type {Element} */ (parent_element), /** @type {Node} */ (node)); - } else { + if (sibling) { sibling.before(/** @type {Node} */ (node)); + } else { + append_child(/** @type {Element} */ (parent_element), /** @type {Node} */ (node)); } } return current[0]; - } else if (current !== null) { - if (sibling === null) { - append_child(/** @type {Element} */ (parent_element), /** @type {Node} */ (current)); - } else { + } else if (current) { + if (sibling) { sibling.before(/** @type {Node} */ (current)); + } else { + append_child(/** @type {Element} */ (parent_element), /** @type {Node} */ (current)); } } return /** @type {Text | Element | Comment} */ (current); @@ -71,7 +71,7 @@ export function remove(current) { */ export function reconcile_html(dom, value, svg) { hydrate_block_anchor(dom); - if (current_hydration_fragment !== null) { + if (current_hydration_fragment) { return current_hydration_fragment; } var html = value + ''; diff --git a/packages/svelte/src/internal/client/render.js b/packages/svelte/src/internal/client/render.js index 5670ef582b5e..8ea68aacf364 100644 --- a/packages/svelte/src/internal/client/render.js +++ b/packages/svelte/src/internal/client/render.js @@ -124,14 +124,14 @@ export function svg_replace(node) { * @returns {Element | DocumentFragment | Node[]} */ function open_template(is_fragment, use_clone_node, anchor, template_element_fn) { - if (current_hydration_fragment !== null) { - if (anchor !== null) { + if (current_hydration_fragment) { + if (anchor) { hydrate_block_anchor(anchor, false); } // In ssr+hydration optimization mode, we might remove the template_element, // so we need to is_fragment flag to properly handle hydrated content accordingly. const fragment = current_hydration_fragment; - if (fragment !== null) { + if (fragment) { return is_fragment ? fragment : /** @type {Element} */ (fragment[0]); } } @@ -196,7 +196,7 @@ function close_template(dom, is_fragment, anchor) { ? dom : /** @type {import('./types.js').TemplateNode[]} */ (Array.from(dom.childNodes)) : dom; - if (anchor !== null) { + if (anchor) { if (current_hydration_fragment === null) { insert(current, null, anchor); } @@ -352,7 +352,7 @@ export function class_name(dom, value) { // @ts-expect-error need to add __className to patched prototype const prev_class_name = dom.__className; const next_class_name = to_class(value); - const is_hydrating = current_hydration_fragment !== null; + const is_hydrating = current_hydration_fragment; if (is_hydrating && dom.className === next_class_name) { // In case of hydration don't reset the class as it's already correct. // @ts-expect-error need to add __className to patched prototype @@ -389,7 +389,7 @@ export function text(dom, value) { // @ts-expect-error need to add __value to patched prototype const prev_node_value = dom.__nodeValue; const next_node_value = stringify(value); - if (current_hydration_fragment !== null && dom.nodeValue === next_node_value) { + if (current_hydration_fragment && dom.nodeValue === next_node_value) { // In case of hydration don't reset the nodeValue as it's already correct. // @ts-expect-error need to add __nodeValue to patched prototype dom.__nodeValue = next_node_value; @@ -676,7 +676,7 @@ export function bind_playback_rate(media, get_value, update) { * @param {(paused: boolean) => void} update */ export function bind_paused(media, get_value, update) { - let mounted = current_hydration_fragment !== null; + let mounted = !!current_hydration_fragment; let paused = get_value(); const callback = () => { if (paused !== media.paused) { @@ -979,7 +979,7 @@ export function bind_select_value(dom, get_value, update) { if (mounting && value === undefined) { /** @type {HTMLOptionElement | null} */ let selected_option = dom.querySelector(':checked'); - if (selected_option !== null) { + if (selected_option) { value = get_option_value(selected_option); update(value); } @@ -1050,7 +1050,7 @@ function get_binding_group_value(group, __value, checked) { export function bind_group(group, group_index, dom, get_value, update) { const is_checkbox = dom.getAttribute('type') === 'checkbox'; let binding_group = group; - if (group_index !== null) { + if (group_index) { for (const index of group_index) { const group = binding_group; // @ts-ignore @@ -1298,7 +1298,7 @@ function handle_event_propagation(root_element, event) { } }); - while (current_target !== null) { + while (current_target) { /** @type {null | Element} */ const parent_element = current_target.parentNode || /** @type {any} */ (current_target).host || null; @@ -1332,7 +1332,7 @@ function handle_event_propagation(root_element, event) { export function slot(anchor_node, slot_fn, slot_props, fallback_fn) { hydrate_block_anchor(anchor_node); if (slot_fn === undefined) { - if (fallback_fn !== null) { + if (fallback_fn) { fallback_fn(anchor_node); } } else { @@ -1390,7 +1390,7 @@ function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn) { trigger_transitions(alternate_transitions, 'in'); } } - } else if (current_hydration_fragment !== null) { + } else if (current_hydration_fragment) { const comment_text = /** @type {Comment} */ (current_hydration_fragment?.[0])?.data; if ( !comment_text || @@ -1415,7 +1415,7 @@ function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn) { // Managed effect const consequent_effect = render_effect( () => { - if (consequent_dom !== null) { + if (consequent_dom) { remove(consequent_dom); consequent_dom = null; } @@ -1437,12 +1437,12 @@ function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn) { // Managed effect const alternate_effect = render_effect( () => { - if (alternate_dom !== null) { + if (alternate_dom) { remove(alternate_dom); alternate_dom = null; } if (!block.v) { - if (alternate_fn !== null) { + if (alternate_fn) { alternate_fn(anchor_node); } if (!has_mounted_branch) { @@ -1459,10 +1459,10 @@ function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn) { ); block.ae = alternate_effect; push_destroy_fn(if_effect, () => { - if (consequent_dom !== null) { + if (consequent_dom) { remove(consequent_dom); } - if (alternate_dom !== null) { + if (alternate_dom) { remove(alternate_dom); } destroy_signal(consequent_effect); @@ -1480,15 +1480,16 @@ export function head(render_fn) { const block = create_head_block(); // The head function may be called after the first hydration pass and ssr comment nodes may still be present, // therefore we need to skip that when we detect that we're not in hydration mode. - const hydration_fragment = - current_hydration_fragment !== null ? get_hydration_fragment(document.head.firstChild) : null; + const hydration_fragment = current_hydration_fragment + ? get_hydration_fragment(document.head.firstChild) + : null; const previous_hydration_fragment = current_hydration_fragment; set_current_hydration_fragment(hydration_fragment); try { const head_effect = render_effect( () => { const current = block.d; - if (current !== null) { + if (current) { remove(current); block.d = null; } @@ -1504,7 +1505,7 @@ export function head(render_fn) { ); push_destroy_fn(head_effect, () => { const current = block.d; - if (current !== null) { + if (current) { remove(current); } }); @@ -1566,20 +1567,20 @@ export function element(anchor_node, tag_fn, render_fn, is_svg = false) { const render_effect_signal = render_effect( () => { const next_element = tag - ? current_hydration_fragment !== null + ? current_hydration_fragment ? /** @type {HTMLElement | SVGElement} */ (current_hydration_fragment[0]) : is_svg ? document.createElementNS('http://www.w3.org/2000/svg', tag) : document.createElement(tag) : null; const prev_element = element; - if (prev_element !== null) { + if (prev_element) { block.d = null; } element = next_element; - if (element !== null && render_fn !== undefined) { + if (element && render_fn !== undefined) { let anchor; - if (current_hydration_fragment !== null) { + if (current_hydration_fragment) { // Use the existing ssr comment as the anchor so that the inner open and close // methods can pick up the existing nodes correctly anchor = /** @type {Comment} */ (element.firstChild); @@ -1589,11 +1590,11 @@ export function element(anchor_node, tag_fn, render_fn, is_svg = false) { } render_fn(element, anchor); } - const has_prev_element = prev_element !== null; + const has_prev_element = prev_element; if (has_prev_element) { remove(prev_element); } - if (element !== null) { + if (element) { insert(element, null, anchor_node); if (has_prev_element) { const parent_block = block.p; @@ -1605,7 +1606,7 @@ export function element(anchor_node, tag_fn, render_fn, is_svg = false) { true ); push_destroy_fn(element_effect, () => { - if (element !== null) { + if (element) { remove(element); block.d = null; element = null; @@ -1643,8 +1644,8 @@ export function component(anchor_node, component_fn, render_fn) { transition.f(() => { transitions.delete(transition); if (transitions.size === 0) { - if (render.e !== null) { - if (render.d !== null) { + if (render.e) { + if (render.d) { remove(render.d); render.d = null; } @@ -1666,7 +1667,7 @@ export function component(anchor_node, component_fn, render_fn) { const effect = render_effect( () => { const current = block.d; - if (current !== null) { + if (current) { remove(current); block.d = null; } @@ -1690,7 +1691,7 @@ export function component(anchor_node, component_fn, render_fn) { } const transitions = render.s; if (transitions.size === 0) { - if (render.d !== null) { + if (render.d) { remove(render.d); render.d = null; } @@ -1717,13 +1718,13 @@ export function component(anchor_node, component_fn, render_fn) { ); push_destroy_fn(component_effect, () => { let render = current_render; - while (render !== null) { + while (render) { const dom = render.d; - if (dom !== null) { + if (dom) { remove(dom); } const effect = render.e; - if (effect !== null) { + if (effect) { destroy_signal(effect); } render = render.p; @@ -1769,8 +1770,8 @@ function await_block(anchor_node, input, pending_fn, then_fn, catch_fn) { transition.f(() => { transitions.delete(transition); if (transitions.size === 0) { - if (render.e !== null) { - if (render.d !== null) { + if (render.e) { + if (render.d) { remove(render.d); render.d = null; } @@ -1794,15 +1795,15 @@ function await_block(anchor_node, input, pending_fn, then_fn, catch_fn) { if (resolved_value === UNINITIALIZED) { // pending = true block.n = true; - if (pending_fn !== null) { + if (pending_fn) { pending_fn(anchor_node); } - } else if (then_fn !== null) { + } else if (then_fn) { // pending = false block.n = false; then_fn(anchor_node, resolved_value); } - } else if (catch_fn !== null) { + } else if (catch_fn) { // pending = false block.n = false; catch_fn(anchor_node, error); @@ -1825,7 +1826,7 @@ function await_block(anchor_node, input, pending_fn, then_fn, catch_fn) { } const transitions = render.s; if (transitions.size === 0) { - if (render.d !== null) { + if (render.d) { remove(render.d); render.d = null; } @@ -1884,13 +1885,13 @@ function await_block(anchor_node, input, pending_fn, then_fn, catch_fn) { push_destroy_fn(await_effect, () => { let render = current_render; latest_token = {}; - while (render !== null) { + while (render) { const dom = render.d; - if (dom !== null) { + if (dom) { remove(dom); } const effect = render.e; - if (effect !== null) { + if (effect) { destroy_signal(effect); } render = render.p; @@ -1929,8 +1930,8 @@ export function key(anchor_node, key, render_fn) { transition.f(() => { transitions.delete(transition); if (transitions.size === 0) { - if (render.e !== null) { - if (render.d !== null) { + if (render.e) { + if (render.d) { remove(render.d); render.d = null; } @@ -1969,7 +1970,7 @@ export function key(anchor_node, key, render_fn) { } const transitions = render.s; if (transitions.size === 0) { - if (render.d !== null) { + if (render.d) { remove(render.d); render.d = null; } @@ -2000,13 +2001,13 @@ export function key(anchor_node, key, render_fn) { mounted = true; push_destroy_fn(key_effect, () => { let render = current_render; - while (render !== null) { + while (render) { const dom = render.d; - if (dom !== null) { + if (dom) { remove(dom); } const effect = render.e; - if (effect !== null) { + if (effect) { destroy_signal(effect); } render = render.p; @@ -2030,7 +2031,7 @@ export function cssProps(anchor, is_html, props, component) { /** @type {Text | Comment} */ let component_anchor; - if (current_hydration_fragment !== null) { + if (current_hydration_fragment) { // Hydration: css props element is surrounded by a ssr comment ... tag = /** @type {HTMLElement | SVGElement} */ (current_hydration_fragment[0]); // ... and the child(ren) of the css props element is also surround by a ssr comment @@ -2196,7 +2197,7 @@ export function action(dom, action, value_fn) { * @returns {void} */ export function remove_input_attr_defaults(dom) { - if (current_hydration_fragment !== null) { + if (current_hydration_fragment) { attr(dom, 'value', null); attr(dom, 'checked', null); } @@ -2208,7 +2209,7 @@ export function remove_input_attr_defaults(dom) { * @returns {void} */ export function remove_textarea_child(dom) { - if (current_hydration_fragment !== null && dom.firstChild !== null) { + if (current_hydration_fragment && dom.firstChild) { dom.textContent = ''; } } @@ -2643,7 +2644,7 @@ const spread_props_handler = { while (i--) { let p = target.props[i]; if (is_function(p)) p = p(); - if (typeof p === 'object' && p !== null && key in p) return p[key]; + if (typeof p === 'object' && p && key in p) return p[key]; } }, getOwnPropertyDescriptor(target, key) { @@ -2651,7 +2652,7 @@ const spread_props_handler = { while (i--) { let p = target.props[i]; if (is_function(p)) p = p(); - if (typeof p === 'object' && p !== null && key in p) return get_descriptor(p, key); + if (typeof p === 'object' && p && key in p) return get_descriptor(p, key); } }, has(target, key) { @@ -2795,7 +2796,7 @@ export function mount(component, options) { ); block.e = effect; } catch (error) { - if (options.recover !== false && hydration_fragment !== null) { + if (options.recover !== false && hydration_fragment) { // eslint-disable-next-line no-console console.error( 'ERR_SVELTE_HYDRATION_MISMATCH' + @@ -2860,10 +2861,10 @@ export function mount(component, options) { } root_event_handles.delete(event_handle); const dom = block.d; - if (dom !== null) { + if (dom) { remove(dom); } - if (hydration_fragment !== null) { + if (hydration_fragment) { remove(hydration_fragment); } destroy_signal(/** @type {import('./types.js').EffectSignal} */ (block.e)); @@ -2906,7 +2907,7 @@ export function snippet_effect(get_snippet, node, args) { const snippet = get_snippet(); untrack(() => snippet(node, args)); return () => { - if (block.d !== null) { + if (block.d) { remove(block.d); } }; From 0ed05cfcc0ec94be79aac7f71571deaf252caf90 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 8 Jan 2024 16:16:20 -0800 Subject: [PATCH 3/4] a few more --- packages/svelte/src/internal/client/each.js | 65 +++++++++---------- packages/svelte/src/internal/client/loop.js | 4 +- .../svelte/src/internal/client/reconciler.js | 2 +- packages/svelte/src/internal/client/render.js | 48 +++++++------- 4 files changed, 56 insertions(+), 63 deletions(-) diff --git a/packages/svelte/src/internal/client/each.js b/packages/svelte/src/internal/client/each.js index 0602ed8ff0e1..2d39abe4ad26 100644 --- a/packages/svelte/src/internal/client/each.js +++ b/packages/svelte/src/internal/client/each.js @@ -50,7 +50,7 @@ function no_op() {} * @returns {void} */ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, reconcile_fn) { - const is_controlled = (flags & EACH_IS_CONTROLLED) !== 0; + const is_controlled = !!(flags & EACH_IS_CONTROLLED); const block = create_each_block(flags, anchor_node); /** @type {null | import('./types.js').Render} */ @@ -73,15 +73,13 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re transitions.add(transition); transition.f(() => { transitions.delete(transition); - if (transitions.size === 0) { - if (fallback.e) { - if (fallback.d) { - remove(fallback.d); - fallback.d = null; - } - destroy_signal(fallback.e); - fallback.e = null; + if (!transitions.size && fallback.e) { + if (fallback.d) { + remove(fallback.d); + fallback.d = null; } + destroy_signal(fallback.e); + fallback.e = null; } }); }; @@ -103,7 +101,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re block.d = null; } let anchor = block.a; - const is_controlled = (block.f & EACH_IS_CONTROLLED) !== 0; + const is_controlled = !!(block.f & EACH_IS_CONTROLLED); if (is_controlled) { anchor = empty(); block.a.appendChild(anchor); @@ -122,7 +120,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re /** @param {import('./types.js').EachBlock} block */ const clear_each = (block) => { const flags = block.f; - const is_controlled = (flags & EACH_IS_CONTROLLED) !== 0; + const is_controlled = !!(flags & EACH_IS_CONTROLLED); const anchor_node = block.a; reconcile_fn(array, block, anchor_node, is_controlled, render_fn, flags, true, keys); }; @@ -138,21 +136,21 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re : Array.from(maybe_array); if (key_fn) { keys = array.map(key_fn); - } else if ((flags & EACH_KEYED) === 0) { + } else if (!(flags & EACH_KEYED)) { array.map(no_op); } const length = array.length; if (fallback_fn) { - if (length === 0) { - if (block.v.length !== 0 || !render) { + if (!length) { + if (block.v.length || !render) { clear_each(block); create_fallback_effect(); return; } - } else if (block.v.length === 0 && current_fallback) { + } else if (!block.v.length && current_fallback) { const fallback = current_fallback; const transitions = fallback.s; - if (transitions.size === 0) { + if (!transitions.size) { if (fallback.d) { remove(fallback.d); fallback.d = null; @@ -175,7 +173,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re push_destroy_fn(each, () => { const flags = block.f; const anchor_node = block.a; - const is_controlled = (flags & EACH_IS_CONTROLLED) !== 0; + const is_controlled = !!(flags & EACH_IS_CONTROLLED); let fallback = current_fallback; while (fallback) { const dom = fallback.d; @@ -263,14 +261,14 @@ function reconcile_indexed_array( var b_blocks; var block; - if (active_transitions.length !== 0) { + if (active_transitions.length) { destroy_active_transition_blocks(active_transitions); } - if (b === 0) { + if (!b) { b_blocks = []; // Remove old blocks - if (is_controlled && a !== 0) { + if (is_controlled && a) { clear_text_content(dom); } while (index < length) { @@ -363,14 +361,14 @@ function reconcile_tracked_array( var b_blocks; var block; - if (active_transitions.length !== 0) { + if (active_transitions.length) { destroy_active_transition_blocks(active_transitions); } - if (b === 0) { + if (!b) { b_blocks = []; // Remove old blocks - if (is_controlled && a !== 0) { + if (is_controlled && a) { clear_text_content(dom); } while (a > 0) { @@ -406,7 +404,7 @@ function reconcile_tracked_array( block = each_item_block(item, key, idx, render_fn, flags); b_blocks[idx] = block; } - } else if (a === 0) { + } else if (!a) { // Create new blocks while (b > 0) { idx = b_end - --b; @@ -417,9 +415,8 @@ function reconcile_tracked_array( insert_each_item_block(block, dom, is_controlled, null); } } else { - var is_animated = (flags & EACH_IS_ANIMATED) !== 0; - var should_update_block = - (flags & (EACH_ITEM_REACTIVE | EACH_INDEX_REACTIVE)) !== 0 || is_animated; + var is_animated = !!(flags & EACH_IS_ANIMATED); + var should_update_block = flags & (EACH_ITEM_REACTIVE | EACH_INDEX_REACTIVE) || is_animated; var start = 0; /** @type {null | Text | Element | Comment} */ @@ -705,16 +702,16 @@ export function get_first_element(block) { * @returns {void} */ function update_each_item_block(block, item, index, type) { - if ((type & EACH_ITEM_REACTIVE) !== 0) { + if (type & EACH_ITEM_REACTIVE) { set_signal_value(block.v, item); } else if (is_lazy_property(block.v)) { block.v.o[block.v.p] = item; } const transitions = block.s; - const index_is_reactive = (type & EACH_INDEX_REACTIVE) !== 0; + const index_is_reactive = !!(type & EACH_INDEX_REACTIVE); // Handle each item animations const each_animation = block.a; - if (transitions && (type & EACH_KEYED) !== 0 && each_animation) { + if (transitions && type & EACH_KEYED && each_animation) { each_animation(block, transitions, index, index_is_reactive); } if (index_is_reactive) { @@ -746,7 +743,7 @@ export function destroy_each_item_block( transitions.delete(other); } } - if (transitions.size === 0) { + if (!transitions.size) { block.s = null; } else { trigger_transitions(transitions, 'out'); @@ -775,15 +772,15 @@ export function destroy_each_item_block( * @returns {import('./types.js').EachItemBlock} */ function each_item_block(item, key, index, render_fn, flags) { - const each_item_not_reactive = (flags & EACH_ITEM_REACTIVE) === 0; + const each_item_not_reactive = !(flags & EACH_ITEM_REACTIVE); const item_value = each_item_not_reactive ? item - : (flags & EACH_IS_IMMUTABLE) === 0 + : !(flags & EACH_IS_IMMUTABLE) ? mutable_source(item) : source(item); - const index_value = (flags & EACH_INDEX_REACTIVE) === 0 ? index : source(index); + const index_value = !(flags & EACH_INDEX_REACTIVE) ? index : source(index); const block = create_each_item_block(item_value, index_value, key); const effect = render_effect( diff --git a/packages/svelte/src/internal/client/loop.js b/packages/svelte/src/internal/client/loop.js index 8bae0a6198e9..8f123c4cb200 100644 --- a/packages/svelte/src/internal/client/loop.js +++ b/packages/svelte/src/internal/client/loop.js @@ -13,7 +13,7 @@ function run_tasks(now) { task.f(); } }); - if (tasks.size !== 0) raf.tick(run_tasks); + if (tasks.size) raf.tick(run_tasks); } /** @@ -33,7 +33,7 @@ export function clear_loops() { export function loop(callback) { /** @type {import('./private.js').TaskEntry} */ let task; - if (tasks.size === 0) raf.tick(run_tasks); + if (!tasks.size) raf.tick(run_tasks); return { promise: new Promise((fulfill) => { tasks.add((task = { c: callback, f: fulfill })); diff --git a/packages/svelte/src/internal/client/reconciler.js b/packages/svelte/src/internal/client/reconciler.js index f3bb5207fe2d..9584420fb3bf 100644 --- a/packages/svelte/src/internal/client/reconciler.js +++ b/packages/svelte/src/internal/client/reconciler.js @@ -49,7 +49,7 @@ export function remove(current) { var node; for (; i < current.length; i++) { node = current[i]; - if (i === 0) { + if (!i) { first_node = node; } if (node.isConnected) { diff --git a/packages/svelte/src/internal/client/render.js b/packages/svelte/src/internal/client/render.js index 8ea68aacf364..f36956d3b54b 100644 --- a/packages/svelte/src/internal/client/render.js +++ b/packages/svelte/src/internal/client/render.js @@ -831,7 +831,7 @@ class ResizeObserverSingleton { return () => { const listeners = this.#listeners.get(element); listeners.delete(listener); - if (listeners.size === 0) { + if (!listeners.size) { this.#listeners.delete(element); /** @type {ResizeObserver} */ (this.#observer).unobserve(element); } @@ -1368,23 +1368,23 @@ function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn) { const consequent_transitions = block.c; const alternate_transitions = block.a; if (result) { - if (alternate_transitions === null || alternate_transitions.size === 0) { + if (alternate_transitions === null || !alternate_transitions.size) { execute_effect(alternate_effect); } else { trigger_transitions(alternate_transitions, 'out'); } - if (consequent_transitions === null || consequent_transitions.size === 0) { + if (consequent_transitions === null || !consequent_transitions.size) { execute_effect(consequent_effect); } else { trigger_transitions(consequent_transitions, 'in'); } } else { - if (consequent_transitions === null || consequent_transitions.size === 0) { + if (consequent_transitions === null || !consequent_transitions.size) { execute_effect(consequent_effect); } else { trigger_transitions(consequent_transitions, 'out'); } - if (alternate_transitions === null || alternate_transitions.size === 0) { + if (alternate_transitions === null || !alternate_transitions.size) { execute_effect(alternate_effect); } else { trigger_transitions(alternate_transitions, 'in'); @@ -1643,15 +1643,13 @@ export function component(anchor_node, component_fn, render_fn) { transitions.add(transition); transition.f(() => { transitions.delete(transition); - if (transitions.size === 0) { - if (render.e) { - if (render.d) { - remove(render.d); - render.d = null; - } - destroy_signal(render.e); - render.e = null; + if (!transitions.size && render.e) { + if (render.d) { + remove(render.d); + render.d = null; } + destroy_signal(render.e); + render.e = null; } }); }; @@ -1690,7 +1688,7 @@ export function component(anchor_node, component_fn, render_fn) { return; } const transitions = render.s; - if (transitions.size === 0) { + if (!transitions.size) { if (render.d) { remove(render.d); render.d = null; @@ -1769,15 +1767,13 @@ function await_block(anchor_node, input, pending_fn, then_fn, catch_fn) { transitions.add(transition); transition.f(() => { transitions.delete(transition); - if (transitions.size === 0) { - if (render.e) { - if (render.d) { - remove(render.d); - render.d = null; - } - destroy_signal(render.e); - render.e = null; + if (!transitions.size && render.e) { + if (render.d) { + remove(render.d); + render.d = null; } + destroy_signal(render.e); + render.e = null; } }); }; @@ -1825,7 +1821,7 @@ function await_block(anchor_node, input, pending_fn, then_fn, catch_fn) { return; } const transitions = render.s; - if (transitions.size === 0) { + if (!transitions.size) { if (render.d) { remove(render.d); render.d = null; @@ -1929,7 +1925,7 @@ export function key(anchor_node, key, render_fn) { transitions.add(transition); transition.f(() => { transitions.delete(transition); - if (transitions.size === 0) { + if (!transitions.size) { if (render.e) { if (render.d) { remove(render.d); @@ -1969,7 +1965,7 @@ export function key(anchor_node, key, render_fn) { return; } const transitions = render.s; - if (transitions.size === 0) { + if (!transitions.size) { if (render.d) { remove(render.d); render.d = null; @@ -2456,7 +2452,7 @@ export function spread_attributes_effect(dom, attrs, lowercase_attributes, css_h */ export function spread_attributes(dom, prev, attrs, lowercase_attributes, css_hash) { const next = object_assign({}, ...attrs); - const has_hash = css_hash.length !== 0; + const has_hash = css_hash.length; for (const key in prev) { if (!(key in next)) { next[key] = null; From 0f4c7e807c91615ba0657b61e32a4fad1c136e73 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 8 Jan 2024 16:25:22 -0800 Subject: [PATCH 4/4] a few more --- packages/svelte/src/internal/client/each.js | 14 +++++++------- packages/svelte/src/internal/client/runtime.js | 12 ++++++------ packages/svelte/src/internal/client/transitions.js | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/svelte/src/internal/client/each.js b/packages/svelte/src/internal/client/each.js index 2d39abe4ad26..a99b0e8e8a30 100644 --- a/packages/svelte/src/internal/client/each.js +++ b/packages/svelte/src/internal/client/each.js @@ -371,7 +371,7 @@ function reconcile_tracked_array( if (is_controlled && a) { clear_text_content(dom); } - while (a > 0) { + while (a) { block = a_blocks[--a]; destroy_each_item_block(block, active_transitions, apply_transitions, is_controlled); } @@ -387,7 +387,7 @@ function reconcile_tracked_array( /** @type {Node} */ var hydrating_node = current_hydration_fragment[0]; - while (b > 0) { + while (b) { // Hydrate block idx = b_end - --b; item = array[idx]; @@ -406,7 +406,7 @@ function reconcile_tracked_array( } } else if (!a) { // Create new blocks - while (b > 0) { + while (b) { idx = b_end - --b; item = array[idx]; key = is_computed_key ? keys[idx] : item; @@ -502,7 +502,7 @@ function reconcile_tracked_array( var should_create; if (is_animated) { var i = b_length; - while (i-- > 0) { + while (i--) { b_end = i + start; a = sources[i]; if (pos === MOVED_BLOCK) { @@ -514,7 +514,7 @@ function reconcile_tracked_array( } var last_block; var last_sibling; - while (b_length-- > 0) { + while (b_length--) { b_end = b_length + start; a = sources[b_length]; should_create = a === -1; @@ -597,7 +597,7 @@ function mark_lis(a) { } if (k < a[index[lo]]) { - if (lo > 0) { + if (lo) { parent[i] = index[lo - 1]; } index[lo] = i; @@ -657,7 +657,7 @@ function get_first_child(block) { function destroy_active_transition_blocks(active_transitions) { var length = active_transitions.length; - if (length > 0) { + if (length) { var i = 0; var block; var transition; diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index cb04f9c96ba8..c2b88ab5295c 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -371,7 +371,7 @@ function execute_signal_fn(signal) { } } - if (dependencies && current_dependencies_index > 0) { + if (dependencies && current_dependencies_index) { dependencies.length = current_dependencies_index + current_dependencies.length; for (i = 0; i < current_dependencies.length; i++) { dependencies[current_dependencies_index + i] = current_dependencies[i]; @@ -520,7 +520,7 @@ export function execute_effect(signal) { if ( is_runes(component_context) && // Don't rerun pre effects more than once to accomodate for "$: only runs once" behavior signal.f & PRE_EFFECT && - current_queued_pre_and_render_effects.length > 0 + current_queued_pre_and_render_effects.length ) { flush_local_pre_effects(component_context); } @@ -545,7 +545,7 @@ function infinite_loop_guard() { */ function flush_queued_effects(effects) { const length = effects.length; - if (length > 0) { + if (length) { infinite_loop_guard(); let i; for (i = 0; i < length; i++) { @@ -566,7 +566,7 @@ function flush_queued_effects(effects) { function process_microtask() { is_micro_task_queued = false; - if (current_queued_microtasks.length > 0) { + if (current_queued_microtasks.length) { const tasks = current_queued_microtasks.slice(); current_queued_microtasks = []; run_all(tasks); @@ -698,7 +698,7 @@ export function flushSync(fn) { if (fn) { fn(); } - if (current_queued_pre_and_render_effects.length > 0 || effects.length > 0) { + if (current_queued_pre_and_render_effects.length || effects.length) { flushSync(); } if (is_micro_task_queued) { @@ -1551,7 +1551,7 @@ export function prop(props, key, flags, initial) { get(inner_current_value); } - if (arguments.length > 0) { + if (arguments.length) { if (mutation || (immutable ? value !== current : safe_not_equal(value, current))) { from_child = true; set(inner_current_value, mutation ? current : value); diff --git a/packages/svelte/src/internal/client/transitions.js b/packages/svelte/src/internal/client/transitions.js index 3317520e6d13..92ca85420749 100644 --- a/packages/svelte/src/internal/client/transitions.js +++ b/packages/svelte/src/internal/client/transitions.js @@ -569,7 +569,7 @@ export function trigger_transitions(transitions, target_direction, from) { mark_subtree_inert(transition.e, true); } } - if (outros.length > 0) { + if (outros.length) { // Defer the outros to a microtask const e = managed_pre_effect(() => { destroy_signal(e);