Skip to content

Commit 5b0a275

Browse files
committed
tweak message, simplify logic
1 parent 0bae144 commit 5b0a275

File tree

4 files changed

+25
-40
lines changed

4 files changed

+25
-40
lines changed

packages/svelte/messages/client-warnings/warnings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
2323
## state_proxy_equality_mismatch
2424

25-
> Detected an equality check between a $state proxy and a non-$state-proxy object for %method%. This equality check will always fail because the proxy has a different object identity. To ensure both operands are of the same kind for accurate results, consider using `$state.is(a, b)`.
25+
> 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

packages/svelte/src/compiler/phases/3-transform/client/visitors/javascript-runes.js

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -456,38 +456,21 @@ export const javascript_visitors_runes = {
456456
const operator = node.operator;
457457

458458
if (state.options.dev) {
459-
if (operator === '===') {
459+
if (operator === '===' || operator === '!==') {
460460
return b.call(
461461
'$.strict_equals',
462462
/** @type {import('estree').Expression} */ (visit(node.left)),
463-
/** @type {import('estree').Expression} */ (visit(node.right))
463+
/** @type {import('estree').Expression} */ (visit(node.right)),
464+
operator === '!==' && b.literal(false)
464465
);
465466
}
466-
if (operator === '!==') {
467-
return b.unary(
468-
'!',
469-
b.call(
470-
'$.strict_equals',
471-
/** @type {import('estree').Expression} */ (visit(node.left)),
472-
/** @type {import('estree').Expression} */ (visit(node.right))
473-
)
474-
);
475-
}
476-
if (operator === '==') {
467+
468+
if (operator === '==' || operator === '!=') {
477469
return b.call(
478470
'$.equals',
479471
/** @type {import('estree').Expression} */ (visit(node.left)),
480-
/** @type {import('estree').Expression} */ (visit(node.right))
481-
);
482-
}
483-
if (operator === '!=') {
484-
return b.unary(
485-
'!',
486-
b.call(
487-
'$.equals',
488-
/** @type {import('estree').Expression} */ (visit(node.left)),
489-
/** @type {import('estree').Expression} */ (visit(node.right))
490-
)
472+
/** @type {import('estree').Expression} */ (visit(node.right)),
473+
operator === '!=' && b.literal(false)
491474
);
492475
}
493476
}

packages/svelte/src/internal/client/dev/equality.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function init_array_prototype_warnings() {
1212
const test = indexOf.call(get_proxied_value(this), get_proxied_value(item), from_index);
1313

1414
if (test !== -1) {
15-
w.state_proxy_equality_mismatch('Array.indexOf');
15+
w.state_proxy_equality_mismatch('array.indexOf(...)');
1616

1717
// eslint-disable-next-line no-console
1818
console.trace();
@@ -29,7 +29,7 @@ export function init_array_prototype_warnings() {
2929
const test = lastIndexOf.call(get_proxied_value(this), get_proxied_value(item), from_index);
3030

3131
if (test !== -1) {
32-
w.state_proxy_equality_mismatch('Array.lastIndexOf');
32+
w.state_proxy_equality_mismatch('array.lastIndexOf(...)');
3333

3434
// eslint-disable-next-line no-console
3535
console.trace();
@@ -46,7 +46,7 @@ export function init_array_prototype_warnings() {
4646
const test = includes.call(get_proxied_value(this), get_proxied_value(item), from_index);
4747

4848
if (test) {
49-
w.state_proxy_equality_mismatch('Array.includes');
49+
w.state_proxy_equality_mismatch('array.includes(...)');
5050

5151
// eslint-disable-next-line no-console
5252
console.trace();
@@ -60,31 +60,33 @@ export function init_array_prototype_warnings() {
6060
/**
6161
* @param {any} a
6262
* @param {any} b
63+
* @param {boolean} equal
6364
* @returns {boolean}
6465
*/
65-
export function strict_equals(a, b) {
66-
if (a !== b && get_proxied_value(a) === get_proxied_value(b)) {
67-
w.state_proxy_equality_mismatch('=== operator');
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 ? '===' : '!==');
6869

6970
// eslint-disable-next-line no-console
7071
console.trace();
7172
}
7273

73-
return a === b;
74+
return (a === b) === equal;
7475
}
7576

7677
/**
7778
* @param {any} a
7879
* @param {any} b
80+
* @param {boolean} equal
7981
* @returns {boolean}
8082
*/
81-
export function equals(a, b) {
82-
if (a != b && get_proxied_value(a) == get_proxied_value(b)) {
83-
w.state_proxy_equality_mismatch('== operator');
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 ? '==' : '!=');
8486

8587
// eslint-disable-next-line no-console
8688
console.trace();
8789
}
8890

89-
return a == b;
91+
return (a == b) === equal;
9092
}

packages/svelte/src/internal/client/warnings.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ export function ownership_invalid_mutation(component, owner) {
7474
}
7575

7676
/**
77-
* Detected an equality check between a $state proxy and a non-$state-proxy object for %method%. This equality check will always fail because the proxy has a different object identity. To ensure both operands are of the same kind for accurate results, consider using `$state.is(a, b)`.
78-
* @param {string} method
77+
* 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
78+
* @param {string} operator
7979
*/
80-
export function state_proxy_equality_mismatch(method) {
80+
export function state_proxy_equality_mismatch(operator) {
8181
if (DEV) {
82-
console.warn(`%c[svelte] ${"state_proxy_equality_mismatch"}\n%c${`Detected an equality check between a $state proxy and a non-$state-proxy object for ${method}. This equality check will always fail because the proxy has a different object identity. To ensure both operands are of the same kind for accurate results, consider using \`$state.is(a, b)\`.`}`, bold, normal);
82+
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);
8383
} else {
8484
// TODO print a link to the documentation
8585
console.warn("state_proxy_equality_mismatch");

0 commit comments

Comments
 (0)