chore: finish up const tag work (#11931)

simplify code a bit, revealed a state-not-updated-correctly bug
pull/11926/head
Rich Harris 7 months ago committed by GitHub
parent 8aa8e699ff
commit 6cf2ec5f83
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -839,8 +839,7 @@ function serialize_inline_component(node, component_name, context) {
} }
} }
children[slot_name] = children[slot_name] || []; (children[slot_name] ||= []).push(child);
children[slot_name].push(child);
} }
// Serialize each slot // Serialize each slot

@ -1382,7 +1382,10 @@ const template_visitors = {
node.fragment.nodes, node.fragment.nodes,
inner_context.path, inner_context.path,
metadata.namespace, metadata.namespace,
context.state, {
...context.state,
scope: /** @type {import('../../scope').Scope} */ (context.state.scopes.get(node.fragment))
},
state.preserve_whitespace, state.preserve_whitespace,
state.options.preserveComments state.options.preserveComments
); );

@ -35,12 +35,10 @@ function sort_const_tags(nodes, state) {
/** /**
* @typedef {{ * @typedef {{
* node: import('#compiler').ConstTag; * node: import('#compiler').ConstTag;
* ids: import('#compiler').Binding[];
* deps: Set<import('#compiler').Binding>; * deps: Set<import('#compiler').Binding>;
* }} Tag * }} Tag
*/ */
const const_tags = [];
const other = []; const other = [];
/** @type {Map<import('#compiler').Binding, Tag>} */ /** @type {Map<import('#compiler').Binding, Tag>} */
@ -52,19 +50,12 @@ function sort_const_tags(nodes, state) {
if (node.type === 'ConstTag') { if (node.type === 'ConstTag') {
const declaration = node.declaration.declarations[0]; const declaration = node.declaration.declarations[0];
/** @type {Tag} */ const bindings = extract_identifiers(declaration.id).map((id) => {
const tag = { return /** @type {import('#compiler').Binding} */ (state.scope.get(id.name));
node, });
ids: extract_identifiers(declaration.id).map((id) => {
return /** @type {import('#compiler').Binding} */ (state.scope.get(id.name)); /** @type {Set<import('#compiler').Binding>} */
}), const deps = new Set();
/** @type {Set<import('#compiler').Binding>} */
deps: new Set()
};
for (const id of tag.ids) {
tags.set(id, tag);
}
walk(declaration.init, state, { walk(declaration.init, state, {
_, _,
@ -73,30 +64,30 @@ function sort_const_tags(nodes, state) {
if (is_reference(node, parent)) { if (is_reference(node, parent)) {
const binding = context.state.scope.get(node.name); const binding = context.state.scope.get(node.name);
if (binding) tag.deps.add(binding); if (binding) deps.add(binding);
} }
} }
}); });
const_tags.push(tag); for (const binding of bindings) {
tags.set(binding, { node, deps });
}
} else { } else {
other.push(node); other.push(node);
} }
} }
if (const_tags.length === 0) { if (tags.size === 0) {
return nodes; return nodes;
} }
/** @type {Array<[import('#compiler').Binding, import('#compiler').Binding]>} */ /** @type {Array<[import('#compiler').Binding, import('#compiler').Binding]>} */
const edges = []; const edges = [];
for (const tag of const_tags) { for (const [id, tag] of tags) {
for (const id of tag.ids) { for (const dep of tag.deps) {
for (const dep of tag.deps) { if (tags.has(dep)) {
if (tags.has(dep)) { edges.push([id, dep]);
edges.push([id, dep]);
}
} }
} }
} }
@ -124,7 +115,7 @@ function sort_const_tags(nodes, state) {
sorted.push(tag.node); sorted.push(tag.node);
} }
for (const tag of const_tags) { for (const tag of tags.values()) {
add(tag); add(tag);
} }

Loading…
Cancel
Save