chore: add proxy date usage warning

chore: add proxy date usage warning

chore: add proxy date usage warning

chore: add proxy date usage warning
pull/10626/head
Dominic Gannaway 9 months ago
parent 83b92372c2
commit fddd15e04d

@ -0,0 +1,5 @@
---
"svelte": patch
---
chore: add proxy date usage warning

@ -87,6 +87,13 @@ export function proxy(value, immutable = true, owners) {
return proxy; return proxy;
} }
if (DEV && prototype === Date.prototype) {
// eslint-disable-next-line no-console
console.warn(
`Date objects used within the $state() rune will not be reactive. Consider using Svelte's reactive version of the Date object from 'svelte/reactivity':` +
`\n\nimport { Date } from 'svelte/reactivity';\n\nconst date = new Date();`
);
}
} }
return value; return value;

@ -0,0 +1,44 @@
import { test } from '../../test';
/** @type {typeof console.warn} */
let warn;
/** @type {typeof console.trace} */
let trace;
/** @type {any[]} */
let warnings = [];
export default test({
compileOptions: {
dev: true
},
before_test: () => {
warn = console.warn;
trace = console.trace;
console.warn = (...args) => {
warnings.push(...args);
};
console.trace = () => {};
},
after_test: () => {
console.warn = warn;
console.trace = trace;
warnings = [];
},
async test({ assert, target }) {
const btn = target.querySelector('button');
await btn?.click();
assert.deepEqual(warnings, [
`Date objects used within the $state() rune will not be reactive. Consider using Svelte's reactive version of the Date object from 'svelte/reactivity':` +
`\n\nimport { Date } from 'svelte/reactivity';\n\nconst date = new Date();`
]);
}
});

@ -0,0 +1,10 @@
<script>
let state = $state({
date: null,
});
</script>
<button onclick={() => {
state.date = new Date();
}}>click</button>
{state.date}
Loading…
Cancel
Save