Skip to content

feat: provide $state warnings for accidental equality #11610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/little-ligers-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

feat: provide $state warnings for accidental equality
4 changes: 4 additions & 0 deletions packages/svelte/messages/client-warnings/warnings.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@
> Mutating a value outside the component that created it is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead

> %component% mutated a value owned by %owner%. This is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead

## state_proxy_equality_mismatch

> Reactive `$state(...)` proxies and the values they proxy have different identities. Because of this, comparisons with `%operator%` will produce unexpected results. Consider using `$state.is(a, b)` instead
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked the previous message a bit better because it more clearly told me that there's something wrong. What's there now is a more general explanation of the situation, where it sounds like I should use $state.is in more places than I need to in reality.

Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,31 @@ export const javascript_visitors_runes = {
}

context.next();
},
BinaryExpression(node, { state, visit, next }) {
const operator = node.operator;

if (state.options.dev) {
if (operator === '===' || operator === '!==') {
return b.call(
'$.strict_equals',
/** @type {import('estree').Expression} */ (visit(node.left)),
/** @type {import('estree').Expression} */ (visit(node.right)),
operator === '!==' && b.literal(false)
);
}

if (operator === '==' || operator === '!=') {
return b.call(
'$.equals',
/** @type {import('estree').Expression} */ (visit(node.left)),
/** @type {import('estree').Expression} */ (visit(node.right)),
operator === '!=' && b.literal(false)
);
}
}

next();
}
};

Expand Down
92 changes: 92 additions & 0 deletions packages/svelte/src/internal/client/dev/equality.js
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions packages/svelte/src/internal/client/dom/operations.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { hydrate_anchor, hydrate_nodes, hydrating } from './hydration.js';
import { get_descriptor } from '../utils.js';
import { DEV } from 'esm-env';
import { init_array_prototype_warnings } from '../dev/equality.js';

// We cache the Node and Element prototype methods, so that we can avoid doing
// expensive prototype chain lookups.
Expand Down Expand Up @@ -74,6 +75,8 @@ export function init_operations() {
if (DEV) {
// @ts-expect-error
element_prototype.__svelte_meta = null;

init_array_prototype_warnings();
}

first_child_get = /** @type {(this: Node) => ChildNode | null} */ (
Expand Down
1 change: 1 addition & 0 deletions packages/svelte/src/internal/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,4 @@ export {
validate_store,
validate_void_dynamic_element
} from '../shared/validate.js';
export { strict_equals, equals } from './dev/equality.js';
13 changes: 13 additions & 0 deletions packages/svelte/src/internal/client/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,17 @@ export function ownership_invalid_mutation(component, owner) {
// TODO print a link to the documentation
console.warn("ownership_invalid_mutation");
}
}

/**
* Reactive `$state(...)` proxies and the values they proxy have different identities. Because of this, comparisons with `%operator%` will produce unexpected results. Consider using `$state.is(a, b)` instead
* @param {string} operator
*/
export function state_proxy_equality_mismatch(operator) {
if (DEV) {
console.warn(`%c[svelte] ${"state_proxy_equality_mismatch"}\n%c${`Reactive \`$state(...)\` proxies and the values they proxy have different identities. Because of this, comparisons with \`${operator}\` will produce unexpected results. Consider using \`$state.is(a, b)\` instead`}`, bold, normal);
} else {
// TODO print a link to the documentation
console.warn("state_proxy_equality_mismatch");
}
}
Loading