Skip to content

Commit 6cf2ec5

Browse files
authored
chore: finish up const tag work (#11931)
simplify code a bit, revealed a state-not-updated-correctly bug
1 parent 8aa8e69 commit 6cf2ec5

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,7 @@ function serialize_inline_component(node, component_name, context) {
839839
}
840840
}
841841

842-
children[slot_name] = children[slot_name] || [];
843-
children[slot_name].push(child);
842+
(children[slot_name] ||= []).push(child);
844843
}
845844

846845
// Serialize each slot

packages/svelte/src/compiler/phases/3-transform/server/transform-server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,10 @@ const template_visitors = {
13821382
node.fragment.nodes,
13831383
inner_context.path,
13841384
metadata.namespace,
1385-
context.state,
1385+
{
1386+
...context.state,
1387+
scope: /** @type {import('../../scope').Scope} */ (context.state.scopes.get(node.fragment))
1388+
},
13861389
state.preserve_whitespace,
13871390
state.options.preserveComments
13881391
);

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

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@ function sort_const_tags(nodes, state) {
3535
/**
3636
* @typedef {{
3737
* node: import('#compiler').ConstTag;
38-
* ids: import('#compiler').Binding[];
3938
* deps: Set<import('#compiler').Binding>;
4039
* }} Tag
4140
*/
4241

43-
const const_tags = [];
4442
const other = [];
4543

4644
/** @type {Map<import('#compiler').Binding, Tag>} */
@@ -52,19 +50,12 @@ function sort_const_tags(nodes, state) {
5250
if (node.type === 'ConstTag') {
5351
const declaration = node.declaration.declarations[0];
5452

55-
/** @type {Tag} */
56-
const tag = {
57-
node,
58-
ids: extract_identifiers(declaration.id).map((id) => {
59-
return /** @type {import('#compiler').Binding} */ (state.scope.get(id.name));
60-
}),
61-
/** @type {Set<import('#compiler').Binding>} */
62-
deps: new Set()
63-
};
64-
65-
for (const id of tag.ids) {
66-
tags.set(id, tag);
67-
}
53+
const bindings = extract_identifiers(declaration.id).map((id) => {
54+
return /** @type {import('#compiler').Binding} */ (state.scope.get(id.name));
55+
});
56+
57+
/** @type {Set<import('#compiler').Binding>} */
58+
const deps = new Set();
6859

6960
walk(declaration.init, state, {
7061
_,
@@ -73,30 +64,30 @@ function sort_const_tags(nodes, state) {
7364

7465
if (is_reference(node, parent)) {
7566
const binding = context.state.scope.get(node.name);
76-
if (binding) tag.deps.add(binding);
67+
if (binding) deps.add(binding);
7768
}
7869
}
7970
});
8071

81-
const_tags.push(tag);
72+
for (const binding of bindings) {
73+
tags.set(binding, { node, deps });
74+
}
8275
} else {
8376
other.push(node);
8477
}
8578
}
8679

87-
if (const_tags.length === 0) {
80+
if (tags.size === 0) {
8881
return nodes;
8982
}
9083

9184
/** @type {Array<[import('#compiler').Binding, import('#compiler').Binding]>} */
9285
const edges = [];
9386

94-
for (const tag of const_tags) {
95-
for (const id of tag.ids) {
96-
for (const dep of tag.deps) {
97-
if (tags.has(dep)) {
98-
edges.push([id, dep]);
99-
}
87+
for (const [id, tag] of tags) {
88+
for (const dep of tag.deps) {
89+
if (tags.has(dep)) {
90+
edges.push([id, dep]);
10091
}
10192
}
10293
}
@@ -124,7 +115,7 @@ function sort_const_tags(nodes, state) {
124115
sorted.push(tag.node);
125116
}
126117

127-
for (const tag of const_tags) {
118+
for (const tag of tags.values()) {
128119
add(tag);
129120
}
130121

0 commit comments

Comments
 (0)