Skip to content

Commit 26d9a9a

Browse files
committed
fix: don't mark selector lists inside :global with multiple items as unused
Regression from #15762 Fixes #15816
1 parent bfb969a commit 26d9a9a

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

.changeset/wild-actors-retire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: don't mark selector lists inside `:global` with multiple items as unused

packages/svelte/src/compiler/phases/3-transform/css/index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ const visitors = {
198198
SelectorList(node, { state, next, path }) {
199199
// Only add comments if we're not inside a complex selector that itself is unused or a global block
200200
if (
201-
(!is_in_global_block(path) || node.children.length > 1) &&
201+
(!is_in_global_block(path) || (node.children.length > 1 && is_in_global_block(path, true))) &&
202202
!path.find((n) => n.type === 'ComplexSelector' && !n.metadata.used)
203203
) {
204204
const children = node.children;
@@ -376,11 +376,23 @@ const visitors = {
376376
};
377377

378378
/**
379-
*
380379
* @param {Array<AST.CSS.Node>} path
380+
* @param {boolean} check_parent_only
381381
*/
382-
function is_in_global_block(path) {
383-
return path.some((node) => node.type === 'Rule' && node.metadata.is_global_block);
382+
function is_in_global_block(path, check_parent_only = false) {
383+
for (let i = path.length - 1; i >= 0; i--) {
384+
const node = path[i];
385+
386+
if (node.type === 'Rule') {
387+
if (check_parent_only) {
388+
return node.metadata.is_global_block;
389+
} else if (node.metadata.is_global_block) {
390+
return true;
391+
}
392+
}
393+
}
394+
395+
return false;
384396
}
385397

386398
/**

packages/svelte/tests/css/samples/global-block/_config.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@ export default test({
77
code: 'css_unused_selector',
88
message: 'Unused CSS selector ".unused :global"',
99
start: {
10-
line: 69,
10+
line: 73,
1111
column: 1,
12-
character: 917
12+
character: 964
1313
},
1414
end: {
15-
line: 69,
15+
line: 73,
1616
column: 16,
17-
character: 932
17+
character: 979
1818
}
1919
},
2020
{
2121
code: 'css_unused_selector',
2222
message: 'Unused CSS selector "unused :global"',
2323
start: {
24-
line: 100,
24+
line: 104,
2525
column: 29,
26-
character: 1223
26+
character: 1270
2727
},
2828
end: {
29-
line: 100,
29+
line: 104,
3030
column: 43,
31-
character: 1237
31+
character: 1284
3232
}
3333
}
3434
]

packages/svelte/tests/css/samples/global-block/expected.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
.x {
44
color: green;
55
}
6+
7+
.a, .selector, .list {
8+
color: green;
9+
}
610
/*}*/
711

812
div.svelte-xyz {

packages/svelte/tests/css/samples/global-block/input.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
.x {
66
color: green;
77
}
8+
9+
.a, .selector, .list {
10+
color: green;
11+
}
812
}
913
1014
div :global {

0 commit comments

Comments
 (0)