From b6d56e1d98553eee02a07d69ba3c02f39cb9be2b Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 16 Dec 2024 18:51:12 +0000 Subject: [PATCH] fix: remove unnecessary array state mutation warnings --- .changeset/lovely-geckos-visit.md | 5 ++ .../src/internal/client/dev/equality.js | 70 ------------------- .../src/internal/client/dom/operations.js | 3 - 3 files changed, 5 insertions(+), 73 deletions(-) create mode 100644 .changeset/lovely-geckos-visit.md diff --git a/.changeset/lovely-geckos-visit.md b/.changeset/lovely-geckos-visit.md new file mode 100644 index 000000000000..806db8eb6bc0 --- /dev/null +++ b/.changeset/lovely-geckos-visit.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: remove unnecessary array state mutation warnings diff --git a/packages/svelte/src/internal/client/dev/equality.js b/packages/svelte/src/internal/client/dev/equality.js index 170f7baf95e9..a05210a9e5d9 100644 --- a/packages/svelte/src/internal/client/dev/equality.js +++ b/packages/svelte/src/internal/client/dev/equality.js @@ -1,76 +1,6 @@ import * as w from '../warnings.js'; import { get_proxied_value } from '../proxy.js'; -export function init_array_prototype_warnings() { - const array_prototype = Array.prototype; - // The REPL ends up here over and over, and this prevents it from adding more and more patches - // of the same kind to the prototype, which would slow down everything over time. - // @ts-expect-error - const cleanup = Array.__svelte_cleanup; - if (cleanup) { - cleanup(); - } - - const { indexOf, lastIndexOf, includes } = array_prototype; - - array_prototype.indexOf = function (item, from_index) { - const index = indexOf.call(this, item, from_index); - - if (index === -1) { - const test = indexOf.call(get_proxied_value(this), get_proxied_value(item), from_index); - - if (test !== -1) { - w.state_proxy_equality_mismatch('array.indexOf(...)'); - } - } - - return index; - }; - - array_prototype.lastIndexOf = function (item, from_index) { - // we need to specify this.length - 1 because it's probably using something like - // `arguments` inside so passing undefined is different from not passing anything - const index = lastIndexOf.call(this, item, from_index ?? this.length - 1); - - if (index === -1) { - // we need to specify this.length - 1 because it's probably using something like - // `arguments` inside so passing undefined is different from not passing anything - const test = lastIndexOf.call( - get_proxied_value(this), - get_proxied_value(item), - from_index ?? this.length - 1 - ); - - if (test !== -1) { - w.state_proxy_equality_mismatch('array.lastIndexOf(...)'); - } - } - - return index; - }; - - array_prototype.includes = function (item, from_index) { - const has = includes.call(this, item, from_index); - - if (!has) { - const test = includes.call(get_proxied_value(this), get_proxied_value(item), from_index); - - if (test) { - w.state_proxy_equality_mismatch('array.includes(...)'); - } - } - - return has; - }; - - // @ts-expect-error - Array.__svelte_cleanup = () => { - array_prototype.indexOf = indexOf; - array_prototype.lastIndexOf = lastIndexOf; - array_prototype.includes = includes; - }; -} - /** * @param {any} a * @param {any} b diff --git a/packages/svelte/src/internal/client/dom/operations.js b/packages/svelte/src/internal/client/dom/operations.js index 627bf917eee1..771dacaf2c33 100644 --- a/packages/svelte/src/internal/client/dom/operations.js +++ b/packages/svelte/src/internal/client/dom/operations.js @@ -1,7 +1,6 @@ /** @import { TemplateNode } from '#client' */ import { hydrate_node, hydrating, set_hydrate_node } from './hydration.js'; import { DEV } from 'esm-env'; -import { init_array_prototype_warnings } from '../dev/equality.js'; import { get_descriptor } from '../../shared/utils.js'; // export these for reference in the compiled code, making global name deduplication unnecessary @@ -54,8 +53,6 @@ export function init_operations() { if (DEV) { // @ts-expect-error element_prototype.__svelte_meta = null; - - init_array_prototype_warnings(); } }