|
| 1 | +import * as w from '../warnings.js'; |
| 2 | +import { get_proxied_value } from '../proxy.js'; |
| 3 | + |
| 4 | +export function init_array_prototype_warnings() { |
| 5 | + const array_prototype = Array.prototype; |
| 6 | + const { indexOf, lastIndexOf, includes } = array_prototype; |
| 7 | + |
| 8 | + array_prototype.indexOf = function (item, from_index) { |
| 9 | + const index = indexOf.call(this, item, from_index); |
| 10 | + |
| 11 | + if (index === -1) { |
| 12 | + const test = indexOf.call(get_proxied_value(this), get_proxied_value(item), from_index); |
| 13 | + |
| 14 | + if (test !== -1) { |
| 15 | + w.state_proxy_equality_mismatch('array.indexOf(...)'); |
| 16 | + |
| 17 | + // eslint-disable-next-line no-console |
| 18 | + console.trace(); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + return index; |
| 23 | + }; |
| 24 | + |
| 25 | + array_prototype.lastIndexOf = function (item, from_index) { |
| 26 | + const index = lastIndexOf.call(this, item, from_index); |
| 27 | + |
| 28 | + if (index === -1) { |
| 29 | + const test = lastIndexOf.call(get_proxied_value(this), get_proxied_value(item), from_index); |
| 30 | + |
| 31 | + if (test !== -1) { |
| 32 | + w.state_proxy_equality_mismatch('array.lastIndexOf(...)'); |
| 33 | + |
| 34 | + // eslint-disable-next-line no-console |
| 35 | + console.trace(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return index; |
| 40 | + }; |
| 41 | + |
| 42 | + array_prototype.includes = function (item, from_index) { |
| 43 | + const has = includes.call(this, item, from_index); |
| 44 | + |
| 45 | + if (!has) { |
| 46 | + const test = includes.call(get_proxied_value(this), get_proxied_value(item), from_index); |
| 47 | + |
| 48 | + if (test) { |
| 49 | + w.state_proxy_equality_mismatch('array.includes(...)'); |
| 50 | + |
| 51 | + // eslint-disable-next-line no-console |
| 52 | + console.trace(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return has; |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * @param {any} a |
| 62 | + * @param {any} b |
| 63 | + * @param {boolean} equal |
| 64 | + * @returns {boolean} |
| 65 | + */ |
| 66 | +export function strict_equals(a, b, equal = true) { |
| 67 | + if ((a === b) !== (get_proxied_value(a) === get_proxied_value(b))) { |
| 68 | + w.state_proxy_equality_mismatch(equal ? '===' : '!=='); |
| 69 | + |
| 70 | + // eslint-disable-next-line no-console |
| 71 | + console.trace(); |
| 72 | + } |
| 73 | + |
| 74 | + return (a === b) === equal; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * @param {any} a |
| 79 | + * @param {any} b |
| 80 | + * @param {boolean} equal |
| 81 | + * @returns {boolean} |
| 82 | + */ |
| 83 | +export function equals(a, b, equal = true) { |
| 84 | + if ((a == b) !== (get_proxied_value(a) == get_proxied_value(b))) { |
| 85 | + w.state_proxy_equality_mismatch(equal ? '==' : '!='); |
| 86 | + |
| 87 | + // eslint-disable-next-line no-console |
| 88 | + console.trace(); |
| 89 | + } |
| 90 | + |
| 91 | + return (a == b) === equal; |
| 92 | +} |
0 commit comments