Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lovely-geckos-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: remove unnecessary array state mutation warnings
70 changes: 0 additions & 70 deletions packages/svelte/src/internal/client/dev/equality.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 0 additions & 3 deletions packages/svelte/src/internal/client/dom/operations.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -54,8 +53,6 @@ export function init_operations() {
if (DEV) {
// @ts-expect-error
element_prototype.__svelte_meta = null;

init_array_prototype_warnings();
}
}

Expand Down
Loading