Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve array mutation validation warnings #14729

Closed
wants to merge 1 commit into from
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/honest-news-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: improve array mutation validation warnings
13 changes: 10 additions & 3 deletions packages/svelte/src/internal/client/dev/equality.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import * as w from '../warnings.js';
import { get_proxied_value } from '../proxy.js';

/**
* @param {any} v
*/
function is_object(v) {
return typeof v === 'object' && v !== null;
}

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
Expand All @@ -16,7 +23,7 @@ export function init_array_prototype_warnings() {
array_prototype.indexOf = function (item, from_index) {
const index = indexOf.call(this, item, from_index);

if (index === -1) {
if (index === -1 && is_object(item)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I don't quite understand is why the proxied version would give a different result - is it not updated yet to the latest value?

Copy link
Contributor Author

@trueadm trueadm Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't modify the original object on mutations to the proxied object, so here the proxied version is [] and the original object is [1], so it triggers the warning because it first checks if the proxied array has it, then checks if the original array has it.

const test = indexOf.call(get_proxied_value(this), get_proxied_value(item), from_index);

if (test !== -1) {
Expand All @@ -32,7 +39,7 @@ export function init_array_prototype_warnings() {
// `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) {
if (index === -1 && is_object(item)) {
// 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(
Expand All @@ -52,7 +59,7 @@ export function init_array_prototype_warnings() {
array_prototype.includes = function (item, from_index) {
const has = includes.call(this, item, from_index);

if (!has) {
if (!has && is_object(item)) {
const test = includes.call(get_proxied_value(this), get_proxied_value(item), from_index);

if (test) {
Expand Down
Loading