Skip to content

fix: ensure function calls to identifiers are made reactive #13264

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

Merged
merged 6 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/red-ladybugs-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: treat pure call expressions as potentially reactive if they reference local bindings
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,6 @@ export function CallExpression(node, context) {
break;
}

if (context.state.expression && !is_pure(node.callee, context)) {
context.state.expression.has_call = true;
context.state.expression.has_state = true;
}

if (context.state.render_tag) {
// Find out which of the render tag arguments contains this call expression
const arg_idx = unwrap_optional(context.state.render_tag.expression).arguments.findIndex(
Expand All @@ -174,4 +169,15 @@ export function CallExpression(node, context) {
} else {
context.next();
}

if (context.state.expression) {
// TODO We assume that any dependencies are stateful, which isn't necessarily the case — see
// https://github.com/sveltejs/svelte/issues/13266. This check also includes dependencies
// outside the call expression itself (e.g. `{blah && pure()}`) resulting in additional
// false positives, but for now we accept that trade-off
if (!is_pure(node.callee, context) || context.state.expression.dependencies.size > 0) {
context.state.expression.has_call = true;
context.state.expression.has_state = true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<script module>
let foo = true;
</script>
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export let obj = $state({ a: 1, b: 2 });
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, target }) {
const [b1] = target.querySelectorAll('button');
b1.click();
flushSync();
assert.htmlEqual(target.innerHTML, `<button>Replace</button>\n9,10,11`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import { obj } from "./Data.svelte.js";

function replaceProp() {
Object.assign(obj, {a:9, b:10, c:11});
}
</script>

<button onclick={replaceProp}>Replace</button>

{Object.values(obj)}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ const __DECLARED_ASSET_3__ = "__VITE_ASSET__2AM7_y_g__";

export default function Inline_module_vars($$payload) {
$$payload.out += `<picture><source${$.attr("srcset", __DECLARED_ASSET_0__)} type="image/avif"> <source${$.attr("srcset", __DECLARED_ASSET_1__)} type="image/webp"> <source${$.attr("srcset", __DECLARED_ASSET_2__)} type="image/png"> <img${$.attr("src", __DECLARED_ASSET_3__)} alt="production test" width="1440" height="1440"></picture>`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ import * as $ from "svelte/internal/client";
var root = $.template(`<p></p> <p></p> <!>`, 1);

export default function Purity($$anchor) {
let min = 0;
let max = 100;
let number = 50;
let value = 'hello';
var fragment = root();
var p = $.first_child(fragment);

p.textContent = Math.max(min, Math.min(max, number));
p.textContent = Math.max(0, Math.min(0, 100));

var p_1 = $.sibling(p, 2);

p_1.textContent = location.href;

var node = $.sibling(p_1, 2);

Child(node, { prop: encodeURIComponent(value) });
Child(node, { prop: encodeURIComponent('hello') });
$.append($$anchor, fragment);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import * as $ from "svelte/internal/server";

export default function Purity($$payload) {
let min = 0;
let max = 100;
let number = 50;
let value = 'hello';

$$payload.out += `<p>${$.escape(Math.max(min, Math.min(max, number)))}</p> <p>${$.escape(location.href)}</p> `;
Child($$payload, { prop: encodeURIComponent(value) });
$$payload.out += `<p>${$.escape(Math.max(0, Math.min(0, 100)))}</p> <p>${$.escape(location.href)}</p> `;
Child($$payload, { prop: encodeURIComponent('hello') });
$$payload.out += `<!---->`;
}
12 changes: 2 additions & 10 deletions packages/svelte/tests/snapshot/samples/purity/index.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
<script>
let min = 0;
let max = 100;
let number = 50;

let value = 'hello';
</script>

<p>{Math.max(min, Math.min(max, number))}</p>
<p>{Math.max(0, Math.min(0, 100))}</p>
<p>{location.href}</p>

<Child prop={encodeURIComponent(value)} />
<Child prop={encodeURIComponent('hello')} />