chore: finish up const tag work (#11931)

simplify code a bit, revealed a state-not-updated-correctly bug
pull/11926/head
Rich Harris 4 weeks 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

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

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

Loading…
Cancel
Save