Skip to content

Commit e78cfd3

Browse files
trueadmRich-Harris
andauthored
fix: correctly handle SvelteDate methods with arguments (#12738)
* fix: correctly handle SvelteDate methods with arguments * tweak * Update packages/svelte/src/reactivity/date.js * Update packages/svelte/src/reactivity/date.js Co-authored-by: Rich Harris <rich.harris@vercel.com> * Update packages/svelte/src/reactivity/date.test.ts --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>
1 parent 8bde2d5 commit e78cfd3

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

.changeset/dirty-pens-look.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: correctly handle SvelteDate methods with arguments

packages/svelte/src/reactivity/date.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ export class SvelteDate extends Date {
3232
if (method.startsWith('get') || method.startsWith('to')) {
3333
// @ts-ignore
3434
proto[method] = function (...args) {
35+
// don't memoize if there are arguments
36+
// @ts-ignore
37+
if (args.length > 0) {
38+
get(this.#time);
39+
// @ts-ignore
40+
return date_proto[method].apply(this, args);
41+
}
42+
3543
var d = this.#deriveds.get(method);
3644

3745
if (d === undefined) {

packages/svelte/src/reactivity/date.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,30 @@ test('Date fine grained tests', () => {
555555
cleanup();
556556
});
557557

558+
test('Date.toLocaleString', () => {
559+
const date = new SvelteDate(initial_date);
560+
561+
const log: any = [];
562+
563+
const cleanup = effect_root(() => {
564+
render_effect(() => {
565+
log.push(date.toLocaleString(undefined, { month: 'long', year: 'numeric' }));
566+
});
567+
render_effect(() => {
568+
log.push(date.toLocaleString(undefined, { month: 'long' }));
569+
});
570+
});
571+
572+
flushSync();
573+
574+
assert.deepEqual(log, [
575+
initial_date.toLocaleString(undefined, { month: 'long', year: 'numeric' }),
576+
initial_date.toLocaleString(undefined, { month: 'long' })
577+
]);
578+
579+
cleanup();
580+
});
581+
558582
test('Date.instanceOf', () => {
559583
assert.equal(new SvelteDate() instanceof Date, true);
560584
});

0 commit comments

Comments
 (0)