better comments

pull/14456/head
Rich Harris 2 years ago
parent 9fb988177c
commit e43baca49c

@ -20,6 +20,9 @@ export function visit_component(node, context) {
// link this node to all the snippets that it could render, so that we can prune CSS correctly // link this node to all the snippets that it could render, so that we can prune CSS correctly
node.metadata.snippets = new Set(); node.metadata.snippets = new Set();
// 'resolved' means we know which snippets this component might render. if it is `false`,
// then `node.metadata.snippets` is populated with every locally defined snippet
// once analysis is complete
let resolved = true; let resolved = true;
for (const attribute of node.attributes) { for (const attribute of node.attributes) {
@ -34,26 +37,26 @@ export function visit_component(node, context) {
const expression = get_attribute_expression(attribute); const expression = get_attribute_expression(attribute);
// given an attribute like `foo={bar}`, if `bar` resolves to an import or a prop
// then we know it doesn't reference a locally defined snippet. if it resolves
// to a `{#snippet bar()}` then we know _which_ snippet it resolves to. in all
// other cases, we can't know (without much more complex static analysis) which
// snippets the component might render, so we treat the component as unresolved
if (expression.type === 'Identifier') { if (expression.type === 'Identifier') {
const binding = context.state.scope.get(expression.name); const binding = context.state.scope.get(expression.name);
if ( resolved &&=
binding && !!binding &&
binding.declaration_kind !== 'import' && binding.declaration_kind !== 'import' &&
binding.kind !== 'prop' && binding.kind !== 'prop' &&
binding.kind !== 'rest_prop' && binding.kind !== 'rest_prop' &&
binding.kind !== 'bindable_prop' && binding.kind !== 'bindable_prop' &&
binding.initial?.type !== 'SnippetBlock' binding.initial?.type !== 'SnippetBlock';
) {
resolved = false;
}
if (binding?.initial?.type === 'SnippetBlock') { if (binding?.initial?.type === 'SnippetBlock') {
node.metadata.snippets.add(binding.initial); node.metadata.snippets.add(binding.initial);
} }
} else { } else {
// we can't safely know which snippets this component could render,
// so we deopt. this _could_ result in unused CSS not being discarded
resolved = false; resolved = false;
} }
} }

Loading…
Cancel
Save