Skip to content

Commit d856c50

Browse files
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
1 parent d946066 commit d856c50

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

.changeset/clever-stingrays-shout.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: support `array.lastIndexOf` without second argument

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,18 @@ export function init_array_prototype_warnings() {
2828
};
2929

3030
array_prototype.lastIndexOf = function (item, from_index) {
31-
const index = lastIndexOf.call(this, item, from_index);
31+
// we need to specify this.length - 1 because it's probably using something like
32+
// `arguments` inside so passing undefined is different from not passing anything
33+
const index = lastIndexOf.call(this, item, from_index ?? this.length - 1);
3234

3335
if (index === -1) {
34-
const test = lastIndexOf.call(get_proxied_value(this), get_proxied_value(item), from_index);
36+
// we need to specify this.length - 1 because it's probably using something like
37+
// `arguments` inside so passing undefined is different from not passing anything
38+
const test = lastIndexOf.call(
39+
get_proxied_value(this),
40+
get_proxied_value(item),
41+
from_index ?? this.length - 1
42+
);
3543

3644
if (test !== -1) {
3745
w.state_proxy_equality_mismatch('array.lastIndexOf(...)');
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ok, test } from '../../test';
2+
import { flushSync } from 'svelte';
3+
4+
export default test({
5+
mode: ['client'],
6+
async test({ assert, logs }) {
7+
assert.equal(logs[0], logs[1]);
8+
}
9+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
const arr = [0, 1, 2];
3+
console.log(arr.lastIndexOf(2));
4+
console.log(arr.lastIndexOf(2, arr.length - 1));
5+
</script>

0 commit comments

Comments
 (0)