mirror of https://github.com/sveltejs/svelte
feat: provide $state warnings for accidental equality (#11610)
* feat: provide $state warnings for accidental equality * tune * tune * tune * adjust test * fix treeshaking * fix bugs * fix bugs * refactor * revert test changes * tune * tune * tune * tune * fix up * fix * remove if(DEV) stuff * use console.trace, like we do for ownership warnings * tweak * tweak message, simplify logic --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>pull/11621/head
parent
f488a6e84a
commit
7ef686f8ee
@ -0,0 +1,5 @@
|
||||
---
|
||||
"svelte": patch
|
||||
---
|
||||
|
||||
feat: provide $state warnings for accidental equality
|
@ -0,0 +1,92 @@
|
||||
import * as w from '../warnings.js';
|
||||
import { get_proxied_value } from '../proxy.js';
|
||||
|
||||
export function init_array_prototype_warnings() {
|
||||
const array_prototype = Array.prototype;
|
||||
const { indexOf, lastIndexOf, includes } = array_prototype;
|
||||
|
||||
array_prototype.indexOf = function (item, from_index) {
|
||||
const index = indexOf.call(this, item, from_index);
|
||||
|
||||
if (index === -1) {
|
||||
const test = indexOf.call(get_proxied_value(this), get_proxied_value(item), from_index);
|
||||
|
||||
if (test !== -1) {
|
||||
w.state_proxy_equality_mismatch('array.indexOf(...)');
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.trace();
|
||||
}
|
||||
}
|
||||
|
||||
return index;
|
||||
};
|
||||
|
||||
array_prototype.lastIndexOf = function (item, from_index) {
|
||||
const index = lastIndexOf.call(this, item, from_index);
|
||||
|
||||
if (index === -1) {
|
||||
const test = lastIndexOf.call(get_proxied_value(this), get_proxied_value(item), from_index);
|
||||
|
||||
if (test !== -1) {
|
||||
w.state_proxy_equality_mismatch('array.lastIndexOf(...)');
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.trace();
|
||||
}
|
||||
}
|
||||
|
||||
return index;
|
||||
};
|
||||
|
||||
array_prototype.includes = function (item, from_index) {
|
||||
const has = includes.call(this, item, from_index);
|
||||
|
||||
if (!has) {
|
||||
const test = includes.call(get_proxied_value(this), get_proxied_value(item), from_index);
|
||||
|
||||
if (test) {
|
||||
w.state_proxy_equality_mismatch('array.includes(...)');
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.trace();
|
||||
}
|
||||
}
|
||||
|
||||
return has;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} a
|
||||
* @param {any} b
|
||||
* @param {boolean} equal
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function strict_equals(a, b, equal = true) {
|
||||
if ((a === b) !== (get_proxied_value(a) === get_proxied_value(b))) {
|
||||
w.state_proxy_equality_mismatch(equal ? '===' : '!==');
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.trace();
|
||||
}
|
||||
|
||||
return (a === b) === equal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} a
|
||||
* @param {any} b
|
||||
* @param {boolean} equal
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function equals(a, b, equal = true) {
|
||||
if ((a == b) !== (get_proxied_value(a) == get_proxied_value(b))) {
|
||||
w.state_proxy_equality_mismatch(equal ? '==' : '!=');
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.trace();
|
||||
}
|
||||
|
||||
return (a == b) === equal;
|
||||
}
|
Loading…
Reference in new issue