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
pull/11768/head
Paolo Ricciuti 4 months ago committed by GitHub
parent d946066c08
commit d856c50092
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: support `array.lastIndexOf` without second argument

@ -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(...)');

@ -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]);
}
});

@ -0,0 +1,5 @@
<script>
const arr = [0, 1, 2];
console.log(arr.lastIndexOf(2));
console.log(arr.lastIndexOf(2, arr.length - 1));
</script>
Loading…
Cancel
Save