Skip to content

Commit

Permalink
fix: array.lastIndexOf without second argument (#11766)
Browse files Browse the repository at this point in the history
Fixes #11756

lastIndexOf seems to be using arguments internally so passing undefined is different from not passing it
  • Loading branch information
paoloricciuti committed May 24, 2024
1 parent d946066 commit d856c50
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-stingrays-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: support `array.lastIndexOf` without second argument
12 changes: 10 additions & 2 deletions packages/svelte/src/internal/client/dev/equality.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(...)');
Expand Down
Original file line number Diff line number Diff line change
@@ -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]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
const arr = [0, 1, 2];
console.log(arr.lastIndexOf(2));
console.log(arr.lastIndexOf(2, arr.length - 1));
</script>

0 comments on commit d856c50

Please sign in to comment.