fix: encounter svelte:element in blocks as sibling during pruning css (#15165)

Fixes #14890
pull/15172/head
7nik 7 months ago committed by GitHub
parent 73fa9d33c8
commit 5b30fbfb0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: encounter svelte:element in blocks as sibling during pruning css

@ -933,11 +933,9 @@ function get_possible_element_siblings(node, adjacent_only, seen = new Set()) {
/** /**
* @param {Compiler.AST.EachBlock | Compiler.AST.IfBlock | Compiler.AST.AwaitBlock | Compiler.AST.KeyBlock | Compiler.AST.SlotElement} node * @param {Compiler.AST.EachBlock | Compiler.AST.IfBlock | Compiler.AST.AwaitBlock | Compiler.AST.KeyBlock | Compiler.AST.SlotElement} node
* @param {boolean} adjacent_only * @param {boolean} adjacent_only
* @returns {Map<Compiler.AST.RegularElement, NodeExistsValue>} * @returns {Map<Compiler.AST.RegularElement | Compiler.AST.SvelteElement, NodeExistsValue>}
*/ */
function get_possible_last_child(node, adjacent_only) { function get_possible_last_child(node, adjacent_only) {
/** @typedef {Map<Compiler.AST.RegularElement, NodeExistsValue>} NodeMap */
/** @type {Array<Compiler.AST.Fragment | undefined | null>} */ /** @type {Array<Compiler.AST.Fragment | undefined | null>} */
let fragments = []; let fragments = [];
@ -960,7 +958,7 @@ function get_possible_last_child(node, adjacent_only) {
break; break;
} }
/** @type {NodeMap} */ /** @type {Map<Compiler.AST.RegularElement | Compiler.AST.SvelteElement, NodeExistsValue>} NodeMap */
const result = new Map(); const result = new Map();
let exhaustive = node.type !== 'SlotElement'; let exhaustive = node.type !== 'SlotElement';
@ -1001,9 +999,10 @@ function has_definite_elements(result) {
} }
/** /**
* @template T * @template T2
* @param {Map<T, NodeExistsValue>} from * @template {T2} T1
* @param {Map<T, NodeExistsValue>} to * @param {Map<T1, NodeExistsValue>} from
* @param {Map<T2, NodeExistsValue>} to
* @returns {void} * @returns {void}
*/ */
function add_to_map(from, to) { function add_to_map(from, to) {
@ -1028,7 +1027,7 @@ function higher_existence(exist1, exist2) {
* @param {boolean} adjacent_only * @param {boolean} adjacent_only
*/ */
function loop_child(children, adjacent_only) { function loop_child(children, adjacent_only) {
/** @type {Map<Compiler.AST.RegularElement, NodeExistsValue>} */ /** @type {Map<Compiler.AST.RegularElement | Compiler.AST.SvelteElement, NodeExistsValue>} */
const result = new Map(); const result = new Map();
let i = children.length; let i = children.length;
@ -1041,6 +1040,8 @@ function loop_child(children, adjacent_only) {
if (adjacent_only) { if (adjacent_only) {
break; break;
} }
} else if (child.type === 'SvelteElement') {
result.set(child, NODE_PROBABLY_EXISTS);
} else if (is_block(child)) { } else if (is_block(child)) {
const child_result = get_possible_last_child(child, adjacent_only); const child_result = get_possible_last_child(child, adjacent_only);
add_to_map(child_result, result); add_to_map(child_result, result);

@ -5,15 +5,15 @@ export default test({
{ {
code: 'css_unused_selector', code: 'css_unused_selector',
end: { end: {
character: 496, character: 627,
column: 10, column: 10,
line: 26 line: 32
}, },
message: 'Unused CSS selector ".x + .bar"', message: 'Unused CSS selector ".x + .bar"',
start: { start: {
character: 487, character: 618,
column: 1, column: 1,
line: 26 line: 32
} }
} }
] ]

@ -9,5 +9,8 @@
.x.svelte-xyz ~ .foo:where(.svelte-xyz) span:where(.svelte-xyz) { color: green; } .x.svelte-xyz ~ .foo:where(.svelte-xyz) span:where(.svelte-xyz) { color: green; }
.x.svelte-xyz ~ .bar:where(.svelte-xyz) { color: green; } .x.svelte-xyz ~ .bar:where(.svelte-xyz) { color: green; }
.z.svelte-xyz + .z:where(.svelte-xyz) { color: green; }
.z.svelte-xyz ~ .z:where(.svelte-xyz) { color: green; }
/* no match */ /* no match */
/* (unused) .x + .bar { color: green; }*/ /* (unused) .x + .bar { color: green; }*/

@ -10,6 +10,9 @@
</p> </p>
<p class="bar">bar</p> <p class="bar">bar</p>
</div> </div>
{#each [1]}
<svelte:element class="z" this={tag}></svelte:element>
{/each}
<style> <style>
.before + .foo { color: green; } .before + .foo { color: green; }
@ -22,6 +25,9 @@
.x ~ .foo span { color: green; } .x ~ .foo span { color: green; }
.x ~ .bar { color: green; } .x ~ .bar { color: green; }
.z + .z { color: green; }
.z ~ .z { color: green; }
/* no match */ /* no match */
.x + .bar { color: green; } .x + .bar { color: green; }
</style> </style>

Loading…
Cancel
Save