-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ec545e0
feat: provide $state warnings for accidental equality
trueadm 4bf4622
tune
trueadm d077e62
tune
trueadm 35c0728
tune
trueadm eff231f
adjust test
trueadm 74cf728
fix treeshaking
trueadm 0e526bc
fix bugs
trueadm c430338
fix bugs
trueadm 3bf48de
refactor
trueadm a06017c
revert test changes
trueadm 6cb451d
tune
trueadm a05ac0d
tune
trueadm 85fe555
tune
trueadm 77be484
tune
trueadm 6c328af
fix up
trueadm f889aba
Merge branch 'main' into better-dev-equality-warnings
Rich-Harris 27ae296
fix
Rich-Harris 22c1ddc
remove if(DEV) stuff
Rich-Harris 36171ab
use console.trace, like we do for ownership warnings
Rich-Harris 0bae144
tweak
Rich-Harris 5b0a275
tweak message, simplify logic
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"svelte": patch | ||
--- | ||
|
||
feat: provide $state warnings for accidental equality |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.