Skip to content

Commit ca9a5c6

Browse files
committed
fix: correctly handle SvelteDate methods with arguments
1 parent e66416b commit ca9a5c6

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ export class SvelteDate extends Date {
3232
if (method.startsWith('get') || method.startsWith('to')) {
3333
// @ts-ignore
3434
proto[method] = function (...args) {
35-
var d = this.#deriveds.get(method);
35+
// @ts-ignore
36+
var can_cache = args.length === 0;
37+
var d = can_cache ? this.#deriveds.get(method) : undefined;
3638

3739
if (d === undefined) {
3840
d = derived(() => {
@@ -41,7 +43,9 @@ export class SvelteDate extends Date {
4143
return date_proto[method].apply(this, args);
4244
});
4345

44-
this.#deriveds.set(method, d);
46+
if (can_cache) {
47+
this.#deriveds.set(method, d);
48+
}
4549
}
4650

4751
return get(d);

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('Datae.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)