Skip to content
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

[SERVER] writable $derived in class results invalid setter expression #15672

Open
PuruVJ opened this issue Apr 2, 2025 · 1 comment
Open

Comments

@PuruVJ
Copy link
Collaborator

PuruVJ commented Apr 2, 2025

Describe the bug

I have this code for Box

export class Box<T> {
    #getter: () => T;
    #setter?: (value: T) => void;

    #derived = $derived.by(() => {
        let val = $state(this.#getter());

        return {
            get current() {
                return val;
            },
            set current(value) {
                val = value;
            }
        };
    });

    constructor(getter: () => T, setter?: (value: T) => void) {
        this.#getter = getter;
        this.#setter = setter;

        $effect(() => {
            this.#setter?.($state.snapshot(this.#derived.current) as T);
        });
    }

    get current(): T {
        return this.#derived.current;
    }

    set current(value: T) {
        this.#derived.current = value;
    }
}

Then, I tried to leverage writable $derived and rewrote to this:

export class Box<T> {
    #getter: () => T;
    #setter?: (value: T) => void;

    #derived = $derived.by(() => this.#getter());

    constructor(getter: () => T, setter?: (value: T) => void) {
        this.#getter = getter;
        this.#setter = setter;

        $effect(() => {
            this.#setter?.($state.snapshot(this.#derived) as T);
        });
    }

    get current(): T {
        return this.#derived;
    }

    set current(value: T) {
        this.#derived = value;
    }
}

This gave the issue when generated on server

Image

Reason: This is generating the following invalid code when generate: "server":

class Box {
		#getter;
		#setter;
		#derived = $.once(() => this.#getter());

		constructor(getter, setter) {
			this.#getter = getter;
			this.#setter = setter;
		}

		get current() {
			return this.#derived();
		}

		set current(value) {
			this.#derived() = value;
		}
	}

The line this.#derived() = value; is wrong.

Reproduction

https://svelte.dev/playground/hello-world?version=5.25.6#H4sIAAAAAAAAE31SwW6DMAz9FSvrIUgVu9OWajvtA3obO2TgtkhZgmJDO1X8-0yBwdi0KIfk-fk9J_ZNOfOBKlEvaK2Hiw-2AI1FyVhEaq2OpUVSyetN8WfV8TpA8CHrqapiatByh70bwr_w3DtGxyKjtpSHsmKwxp12mWLKVJq5jPFa-cCQW0MEz_66PaRwyxzIejghM4YEdAS7FA6bAaY7vBe8MbbGBA73eOPLQigDqcBQNljADlbDUQOfS4oHVR1F32QpkzjUOfugF55r-MctGivt1lxcXPvDZhmnMU5jfGKs8HjEnHVvPZNepu9jvSI2jDE5U9HZs-7Dw0sjMCRlzszb8dKOhlIf5HUI0h4dyaPmfgG5Dg5-aC7zaZY_fcyv_5jacCdNKrK3j_1MpCCjIoPAKpEuYPsmN1PaS-kKlRyNJWy_ACjOwAGtAgAA

Go to compile settings, turn on generate: server

Logs

System Info

System:
    OS: macOS 15.4
    CPU: (10) arm64 Apple M1 Pro
    Memory: 97.44 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.14.0 - ~/.volta/tools/image/node/22.14.0/bin/node
    npm: 10.9.2 - ~/.volta/tools/image/node/22.14.0/bin/npm
    pnpm: 9.2.0 - ~/.volta/bin/pnpm
    bun: 1.2.4 - ~/.bun/bin/bun
  Browsers:
    Edge: 134.0.3124.95
    Safari: 18.4
  npmPackages:
    svelte: 5.25.6 => 5.25.6

Severity

annoyance

@dummdidumm
Copy link
Member

dummdidumm commented Apr 2, 2025

We already noticed this during #15628 and marked it as a follow-up, but didn't think about the static-code-generation-side of things. Related #14977

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants