Skip to content

Commit ec5a340

Browse files
gtm-nayankelvinsjk
authored andcommitted
fix: do not add module declared variables as dependencies (sveltejs#9122)
closes sveltejs#5943
1 parent 558c8d5 commit ec5a340

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

.changeset/famous-news-perform.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: do not add module declared variables as dependencies

packages/svelte/src/compiler/compile/render_dom/wrappers/shared/is_dynamic.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import { is_reserved_keyword } from '../../../utils/reserved_keywords.js';
33
/** @param {import('../../../../interfaces.js').Var} variable */
44
export default function is_dynamic(variable) {
55
if (variable) {
6-
if (variable.mutated || variable.reassigned) return true; // dynamic internal state
7-
if (!variable.module && variable.writable && variable.export_name) return true; // writable props
6+
// Only variables declared in the instance script tags should be considered dynamic
7+
const is_declared_in_reactive_context = !variable.module && !variable.global;
8+
9+
if (is_declared_in_reactive_context && (variable.mutated || variable.reassigned)) return true; // dynamic internal state
10+
if (is_declared_in_reactive_context && variable.writable && variable.export_name) return true; // writable props
811
if (is_reserved_keyword(variable.name)) return true;
912
}
1013
return false;

packages/svelte/test/js/samples/reactive-class-optimized/expected.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ function create_fragment(ctx) {
5353
div7 = element("div");
5454
t7 = space();
5555
div8 = element("div");
56-
toggle_class(div0, "update1", reactiveModuleVar);
57-
toggle_class(div1, "update2", /*reactiveConst*/ ctx[0].x);
58-
toggle_class(div2, "update3", nonReactiveGlobal && /*reactiveConst*/ ctx[0].x);
59-
toggle_class(div3, "update4", /*$reactiveStoreVal*/ ctx[2]);
60-
toggle_class(div4, "update5", /*$reactiveDeclaration*/ ctx[3]);
56+
toggle_class(div0, "update2", /*reactiveConst*/ ctx[0].x);
57+
toggle_class(div1, "update3", nonReactiveGlobal && /*reactiveConst*/ ctx[0].x);
58+
toggle_class(div2, "update4", /*$reactiveStoreVal*/ ctx[2]);
59+
toggle_class(div3, "update5", /*$reactiveDeclaration*/ ctx[3]);
60+
toggle_class(div4, "update1", reassignedModuleVar);
6161
toggle_class(div5, "static1", nonReactiveModuleVar);
6262
toggle_class(div6, "static2", nonReactiveGlobal);
6363
toggle_class(div7, "static3", nonReactiveModuleVar && nonReactiveGlobal);
@@ -83,24 +83,20 @@ function create_fragment(ctx) {
8383
insert(target, div8, anchor);
8484
},
8585
p(ctx, [dirty]) {
86-
if (dirty & /*reactiveModuleVar*/ 0) {
87-
toggle_class(div0, "update1", reactiveModuleVar);
88-
}
89-
9086
if (dirty & /*reactiveConst*/ 1) {
91-
toggle_class(div1, "update2", /*reactiveConst*/ ctx[0].x);
87+
toggle_class(div0, "update2", /*reactiveConst*/ ctx[0].x);
9288
}
9389

9490
if (dirty & /*nonReactiveGlobal, reactiveConst*/ 1) {
95-
toggle_class(div2, "update3", nonReactiveGlobal && /*reactiveConst*/ ctx[0].x);
91+
toggle_class(div1, "update3", nonReactiveGlobal && /*reactiveConst*/ ctx[0].x);
9692
}
9793

9894
if (dirty & /*$reactiveStoreVal*/ 4) {
99-
toggle_class(div3, "update4", /*$reactiveStoreVal*/ ctx[2]);
95+
toggle_class(div2, "update4", /*$reactiveStoreVal*/ ctx[2]);
10096
}
10197

10298
if (dirty & /*$reactiveDeclaration*/ 8) {
103-
toggle_class(div4, "update5", /*$reactiveDeclaration*/ ctx[3]);
99+
toggle_class(div3, "update5", /*$reactiveDeclaration*/ ctx[3]);
104100
}
105101
},
106102
i: noop,
@@ -130,7 +126,7 @@ function create_fragment(ctx) {
130126
}
131127

132128
let nonReactiveModuleVar = Math.random();
133-
let reactiveModuleVar = Math.random();
129+
let reassignedModuleVar = Math.random();
134130

135131
function instance($$self, $$props, $$invalidate) {
136132
let reactiveDeclaration;
@@ -144,13 +140,13 @@ function instance($$self, $$props, $$invalidate) {
144140
$$self.$$.on_destroy.push(() => $$unsubscribe_reactiveDeclaration());
145141
nonReactiveGlobal = Math.random();
146142
const reactiveConst = { x: Math.random() };
147-
reactiveModuleVar += 1;
143+
reassignedModuleVar += 1;
148144

149145
if (Math.random()) {
150146
reactiveConst.x += 1;
151147
}
152148

153-
$: $$subscribe_reactiveDeclaration($$invalidate(1, reactiveDeclaration = reactiveModuleVar * 2));
149+
$: $$subscribe_reactiveDeclaration($$invalidate(1, reactiveDeclaration = reassignedModuleVar * 2));
154150
return [reactiveConst, reactiveDeclaration, $reactiveStoreVal, $reactiveDeclaration];
155151
}
156152

packages/svelte/test/js/samples/reactive-class-optimized/input.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script context="module">
22
let nonReactiveModuleVar = Math.random();
3-
let reactiveModuleVar = Math.random();
3+
let reassignedModuleVar = Math.random();
44
</script>
55

66
<script>
@@ -9,22 +9,22 @@
99
nonReactiveGlobal = Math.random();
1010
const reactiveConst = {x: Math.random()};
1111
12-
$: reactiveDeclaration = reactiveModuleVar * 2;
12+
$: reactiveDeclaration = reassignedModuleVar * 2;
1313
14-
reactiveModuleVar += 1;
14+
reassignedModuleVar += 1;
1515
if (Math.random()) {
1616
reactiveConst.x += 1;
1717
}
1818
</script>
1919

2020
<!--These should all get updaters because they have at least one reactive dependency-->
21-
<div class:update1={reactiveModuleVar}></div>
2221
<div class:update2={reactiveConst.x}></div>
2322
<div class:update3={nonReactiveGlobal && reactiveConst.x}></div>
2423
<div class:update4={$reactiveStoreVal}></div>
2524
<div class:update5={$reactiveDeclaration}></div>
2625

2726
<!--These shouldn't get updates because they're purely non-reactive-->
27+
<div class:update1={reassignedModuleVar}></div>
2828
<div class:static1={nonReactiveModuleVar}></div>
2929
<div class:static2={nonReactiveGlobal}></div>
3030
<div class:static3={nonReactiveModuleVar && nonReactiveGlobal}></div>

0 commit comments

Comments
 (0)