From d856c500924cc620f60ee32047df3c4620c3a5ce Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Fri, 24 May 2024 10:34:35 +0200 Subject: [PATCH] fix: `array.lastIndexOf` without second argument (#11766) Fixes #11756 lastIndexOf seems to be using arguments internally so passing undefined is different from not passing it --- .changeset/clever-stingrays-shout.md | 5 +++++ packages/svelte/src/internal/client/dev/equality.js | 12 ++++++++++-- .../samples/array-lastindexof/_config.js | 9 +++++++++ .../samples/array-lastindexof/main.svelte | 5 +++++ 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 .changeset/clever-stingrays-shout.md create mode 100644 packages/svelte/tests/runtime-runes/samples/array-lastindexof/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/array-lastindexof/main.svelte diff --git a/.changeset/clever-stingrays-shout.md b/.changeset/clever-stingrays-shout.md new file mode 100644 index 0000000000..8bb9ff1acb --- /dev/null +++ b/.changeset/clever-stingrays-shout.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: support `array.lastIndexOf` without second argument diff --git a/packages/svelte/src/internal/client/dev/equality.js b/packages/svelte/src/internal/client/dev/equality.js index bac96b743f..c1c392ba87 100644 --- a/packages/svelte/src/internal/client/dev/equality.js +++ b/packages/svelte/src/internal/client/dev/equality.js @@ -28,10 +28,18 @@ export function init_array_prototype_warnings() { }; array_prototype.lastIndexOf = function (item, from_index) { - const index = lastIndexOf.call(this, 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) { - const test = lastIndexOf.call(get_proxied_value(this), get_proxied_value(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 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(...)'); diff --git a/packages/svelte/tests/runtime-runes/samples/array-lastindexof/_config.js b/packages/svelte/tests/runtime-runes/samples/array-lastindexof/_config.js new file mode 100644 index 0000000000..4aae45942f --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/array-lastindexof/_config.js @@ -0,0 +1,9 @@ +import { ok, test } from '../../test'; +import { flushSync } from 'svelte'; + +export default test({ + mode: ['client'], + async test({ assert, logs }) { + assert.equal(logs[0], logs[1]); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/array-lastindexof/main.svelte b/packages/svelte/tests/runtime-runes/samples/array-lastindexof/main.svelte new file mode 100644 index 0000000000..96fdcd1b6b --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/array-lastindexof/main.svelte @@ -0,0 +1,5 @@ + \ No newline at end of file