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

Reactivity missing for a $derived member function call in Tagged Template syntax without object name #12687

Closed
osamutake opened this issue Aug 1, 2024 · 3 comments · Fixed by #12692
Labels
Milestone

Comments

@osamutake
Copy link

Describe the bug

First, remember that a function like showCount(label: string) can be called with the Tagged Template syntax of javascript as

showCount`Count:`

If such a function is declared with $derived as

let count = $state(0);
let showCount = $derived((s)=> s   ' '   this.count);

calling showCount from a .svelte file such as {showCount`Count:`} becomes reactive and is re-evaluated when count is updated.

When count and showCount are members of a class object, it is also reactive as expected, so that {obj.showCount`Count:`} is re-evaluated when obj.count is updated.

class TObj {
	count = $state(0);
	showCount = $derived((s: string)=>s   ' '   this.count);
};
	
let obj = new TObj();

An issue is observed when obj.showCount is stored in a variable.

const { showCount } = obj;

In this case, {showCount('Count:')} is still reactive but {showCount`Count:`} is no more.

Svelte 5 compiler fails to apply $.template_effect to $derive member function calls in Tagged Template syntax without object name.

Reproduction

Here is a demonstration code. Try it online at svelte-5-preview.vercel.app.

<script lang='ts'>
  class TObj {
    count = $state(0);
    showCount = $derived((s: string)=>s   ' '   this.count);
  };
	
  let obj = new TObj();

  const { showCount } = obj;

  let showCount2 = $derived((s: string)=>s   ' '   obj.count);

  function increment() {
    obj.count  = 1;
  }
</script>

<div>{showCount`Called by tagged template syntax without object name:`}</div>
<div>{showCount('Called by normal function syntax without object name:')}</div>
<div>{obj.showCount`Called by tagged template syntax with object name:`}</div>
<div>{obj.showCount('Called by normal function syntax with object name:')}</div>
<div>{showCount2`Called by tagged template syntax as an independent function:`}</div>

<button onclick={increment}>Click</button>

After clicking the button four times, the result was as follows:

Called by tagged template syntax without object name: 0
Called by normal function syntax without object name: 4
Called by tagged template syntax with object name: 4
Called by normal function syntax with object name: 4
Called by tagged template syntax as an independent function: 4
[Click]

Only the member function call in the tagged template syntax without object name is missing reactivity.

Logs

No response

System Info

This issue was locally tested under the following environment:

  System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 AMD Ryzen 5 5600U with Radeon Graphics
    Memory: 1.98 GB / 14.83 GB
  Binaries:
    Node: 22.4.1 - C:\Programs\nodejs\node.EXE
    npm: 10.8.1 - C:\Programs\nodejs\npm.CMD
    pnpm: 9.5.0 - C:\Programs\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.19041.4355
  npmPackages:
    svelte: 5.0.0-next.175 => 5.0.0-next.175


### Severity

annoyance
@Conduitry Conduitry added the bug label Aug 1, 2024
@Conduitry Conduitry added this to the 5.0 milestone Aug 1, 2024
@brunnerh
Copy link
Member

brunnerh commented Aug 1, 2024

One should not need to declare functions as $derived in Svelte 5, it has no relevance to a signals-based reactivity system.

This should also just work but does not right now:

let count = $state(0);
function showCount(s) {
	return s   ' '   count;
}
{showCount`...`}

@osamutake
Copy link
Author

@brunnerh Thanks for your helpful comment.
As you know, somehow $derived helps to have reactivity in your example in tagged syntax at this moment.
Probably that's the reason why I got believing $derived is needed in such a case...

@paoloricciuti
Copy link
Member

When deciding if applying a template_effect the compiler is probably just looking for CallExpression...will work on this this evening.

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

Successfully merging a pull request may close this issue.

4 participants