Skip to content

Commit 5b30fbf

Browse files
authored
fix: encounter svelte:element in blocks as sibling during pruning css (#15165)
Fixes #14890
1 parent 73fa9d3 commit 5b30fbf

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

.changeset/sweet-mails-clean.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: encounter svelte:element in blocks as sibling during pruning css

packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -933,11 +933,9 @@ function get_possible_element_siblings(node, adjacent_only, seen = new Set()) {
933933
/**
934934
* @param {Compiler.AST.EachBlock | Compiler.AST.IfBlock | Compiler.AST.AwaitBlock | Compiler.AST.KeyBlock | Compiler.AST.SlotElement} node
935935
* @param {boolean} adjacent_only
936-
* @returns {Map<Compiler.AST.RegularElement, NodeExistsValue>}
936+
* @returns {Map<Compiler.AST.RegularElement | Compiler.AST.SvelteElement, NodeExistsValue>}
937937
*/
938938
function get_possible_last_child(node, adjacent_only) {
939-
/** @typedef {Map<Compiler.AST.RegularElement, NodeExistsValue>} NodeMap */
940-
941939
/** @type {Array<Compiler.AST.Fragment | undefined | null>} */
942940
let fragments = [];
943941

@@ -960,7 +958,7 @@ function get_possible_last_child(node, adjacent_only) {
960958
break;
961959
}
962960

963-
/** @type {NodeMap} */
961+
/** @type {Map<Compiler.AST.RegularElement | Compiler.AST.SvelteElement, NodeExistsValue>} NodeMap */
964962
const result = new Map();
965963

966964
let exhaustive = node.type !== 'SlotElement';
@@ -1001,9 +999,10 @@ function has_definite_elements(result) {
1001999
}
10021000

10031001
/**
1004-
* @template T
1005-
* @param {Map<T, NodeExistsValue>} from
1006-
* @param {Map<T, NodeExistsValue>} to
1002+
* @template T2
1003+
* @template {T2} T1
1004+
* @param {Map<T1, NodeExistsValue>} from
1005+
* @param {Map<T2, NodeExistsValue>} to
10071006
* @returns {void}
10081007
*/
10091008
function add_to_map(from, to) {
@@ -1028,7 +1027,7 @@ function higher_existence(exist1, exist2) {
10281027
* @param {boolean} adjacent_only
10291028
*/
10301029
function loop_child(children, adjacent_only) {
1031-
/** @type {Map<Compiler.AST.RegularElement, NodeExistsValue>} */
1030+
/** @type {Map<Compiler.AST.RegularElement | Compiler.AST.SvelteElement, NodeExistsValue>} */
10321031
const result = new Map();
10331032

10341033
let i = children.length;
@@ -1041,6 +1040,8 @@ function loop_child(children, adjacent_only) {
10411040
if (adjacent_only) {
10421041
break;
10431042
}
1043+
} else if (child.type === 'SvelteElement') {
1044+
result.set(child, NODE_PROBABLY_EXISTS);
10441045
} else if (is_block(child)) {
10451046
const child_result = get_possible_last_child(child, adjacent_only);
10461047
add_to_map(child_result, result);

packages/svelte/tests/css/samples/general-siblings-combinator-svelteelement/_config.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ export default test({
55
{
66
code: 'css_unused_selector',
77
end: {
8-
character: 496,
8+
character: 627,
99
column: 10,
10-
line: 26
10+
line: 32
1111
},
1212
message: 'Unused CSS selector ".x + .bar"',
1313
start: {
14-
character: 487,
14+
character: 618,
1515
column: 1,
16-
line: 26
16+
line: 32
1717
}
1818
}
1919
]

packages/svelte/tests/css/samples/general-siblings-combinator-svelteelement/expected.css

+3
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@
99
.x.svelte-xyz ~ .foo:where(.svelte-xyz) span:where(.svelte-xyz) { color: green; }
1010
.x.svelte-xyz ~ .bar:where(.svelte-xyz) { color: green; }
1111

12+
.z.svelte-xyz + .z:where(.svelte-xyz) { color: green; }
13+
.z.svelte-xyz ~ .z:where(.svelte-xyz) { color: green; }
14+
1215
/* no match */
1316
/* (unused) .x + .bar { color: green; }*/

packages/svelte/tests/css/samples/general-siblings-combinator-svelteelement/input.svelte

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
</p>
1111
<p class="bar">bar</p>
1212
</div>
13+
{#each [1]}
14+
<svelte:element class="z" this={tag}></svelte:element>
15+
{/each}
1316

1417
<style>
1518
.before + .foo { color: green; }
@@ -22,6 +25,9 @@
2225
.x ~ .foo span { color: green; }
2326
.x ~ .bar { color: green; }
2427

28+
.z + .z { color: green; }
29+
.z ~ .z { color: green; }
30+
2531
/* no match */
2632
.x + .bar { color: green; }
2733
</style>

0 commit comments

Comments
 (0)