Skip to content

Commit 45875e9

Browse files
committed
fix: improve array mutation validation warnings
1 parent 699dc6e commit 45875e9

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

.changeset/honest-news-bathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: improve array mutation validation warnings

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import * as w from '../warnings.js';
22
import { get_proxied_value } from '../proxy.js';
33

4+
/**
5+
* @param {any} v
6+
*/
7+
function is_object(v) {
8+
return typeof v === 'object' && v !== null;
9+
}
10+
411
export function init_array_prototype_warnings() {
512
const array_prototype = Array.prototype;
613
// The REPL ends up here over and over, and this prevents it from adding more and more patches
@@ -16,7 +23,7 @@ export function init_array_prototype_warnings() {
1623
array_prototype.indexOf = function (item, from_index) {
1724
const index = indexOf.call(this, item, from_index);
1825

19-
if (index === -1) {
26+
if (index === -1 && is_object(item)) {
2027
const test = indexOf.call(get_proxied_value(this), get_proxied_value(item), from_index);
2128

2229
if (test !== -1) {
@@ -32,7 +39,7 @@ export function init_array_prototype_warnings() {
3239
// `arguments` inside so passing undefined is different from not passing anything
3340
const index = lastIndexOf.call(this, item, from_index ?? this.length - 1);
3441

35-
if (index === -1) {
42+
if (index === -1 && is_object(item)) {
3643
// we need to specify this.length - 1 because it's probably using something like
3744
// `arguments` inside so passing undefined is different from not passing anything
3845
const test = lastIndexOf.call(
@@ -52,7 +59,7 @@ export function init_array_prototype_warnings() {
5259
array_prototype.includes = function (item, from_index) {
5360
const has = includes.call(this, item, from_index);
5461

55-
if (!has) {
62+
if (!has && is_object(item)) {
5663
const test = includes.call(get_proxied_value(this), get_proxied_value(item), from_index);
5764

5865
if (test) {

0 commit comments

Comments
 (0)