Skip to content

Commit 2f6ff91

Browse files
committed
simplify
1 parent 9716c31 commit 2f6ff91

File tree

4 files changed

+37
-36
lines changed

4 files changed

+37
-36
lines changed

packages/svelte/src/compiler/phases/3-transform/client/utils.js

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -456,39 +456,30 @@ function get_hoistable_params(node, context) {
456456

457457
/** @type {import('estree').Identifier[]} */
458458
const params = [];
459-
let added_props = false;
460459

461460
/**
462-
* we only want to push if it's not already present to avoid name clashing
461+
* We only want to push if it's not already present to avoid name clashing
463462
* @param {import('estree').Identifier} id
464463
*/
465-
function safe_push(id) {
464+
function push_unique(id) {
466465
if (!params.find((param) => param.name === id.name)) {
467466
params.push(id);
468467
}
469468
}
470469

471470
for (const [reference] of scope.references) {
472-
const binding = scope.get(reference);
471+
let binding = scope.get(reference);
473472

474473
if (binding !== null && !scope.declarations.has(reference) && binding.initial !== node) {
475474
if (binding.kind === 'store_sub') {
476-
const is_from_prop =
477-
binding.declaration_kind === 'synthetic' &&
478-
[...binding.scope.declarations.values()].find(
479-
(declaration) => declaration.kind === 'prop' || declaration.kind === 'bindable_prop'
480-
);
481-
if (is_from_prop && !added_props) {
482-
// if the store come from props we want $$props to be pushed rather than the name
483-
// of the store since that variable doesn't exist
484-
added_props = true;
485-
safe_push(b.id('$$props'));
486-
} else {
487-
// We need both the subscription for getting the value and the store for updating
488-
safe_push(b.id(binding.node.name.slice(1)));
489-
}
490-
safe_push(b.id(binding.node.name));
491-
} else if (
475+
// We need both the subscription for getting the value and the store for updating
476+
push_unique(b.id(binding.node.name));
477+
binding = /** @type {import('#compiler').Binding} */ (
478+
scope.get(binding.node.name.slice(1))
479+
);
480+
}
481+
482+
if (
492483
// If it's a destructured derived binding, then we can extract the derived signal reference and use that.
493484
binding.expression !== null &&
494485
typeof binding.expression !== 'function' &&
@@ -498,22 +489,18 @@ function get_hoistable_params(node, context) {
498489
binding.expression.object.callee.name === '$.get' &&
499490
binding.expression.object.arguments[0].type === 'Identifier'
500491
) {
501-
safe_push(b.id(binding.expression.object.arguments[0].name));
492+
push_unique(b.id(binding.expression.object.arguments[0].name));
502493
} else if (
503494
// If we are referencing a simple $$props value, then we need to reference the object property instead
504495
(binding.kind === 'prop' || binding.kind === 'bindable_prop') &&
505496
!binding.reassigned &&
506497
binding.initial === null &&
507498
!context.state.analysis.accessors
508499
) {
509-
// Handle $$props.something use-cases
510-
if (!added_props) {
511-
added_props = true;
512-
safe_push(b.id('$$props'));
513-
}
500+
push_unique(b.id('$$props'));
514501
} else {
515502
// create a copy to remove start/end tags which would mess up source maps
516-
safe_push(b.id(binding.node.name));
503+
push_unique(b.id(binding.node.name));
517504
}
518505
}
519506
}

packages/svelte/tests/runtime-runes/samples/store-from-props-hoisting/_config.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ export default test({
44
compileOptions: {
55
dev: true
66
},
7-
recover: true,
8-
mode: ['client']
7+
async test({ assert, target }) {
8+
const button = target.querySelector('button');
9+
await button?.click();
10+
11+
assert.htmlEqual(
12+
target.innerHTML,
13+
`
14+
<button>1</button>
15+
`
16+
);
17+
}
918
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
const { attrs } = $props();
3+
function increment() {
4+
$attrs.count++;
5+
}
6+
</script>
7+
8+
<button onclick={increment}>{$attrs.count}</button>
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<script>
2-
const { attrs } = $props();
3-
function increment() {
4-
$attrs.count++;
5-
}
2+
import { writable } from "svelte/store";
3+
import Child from "./child.svelte";
4+
const attrs = writable({ count: 0 });
65
</script>
76

8-
<button onclick={increment}>
9-
+
10-
</button>
7+
<Child {attrs} />

0 commit comments

Comments
 (0)