Skip to content

fix: don't mark selector lists inside :global with multiple items as unused #15817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wild-actors-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: don't mark selector lists inside `:global` with multiple items as unused
7 changes: 4 additions & 3 deletions packages/svelte/src/compiler/phases/3-transform/css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,12 @@ const visitors = {
next();
},
SelectorList(node, { state, next, path }) {
const parent = path.at(-1);

// Only add comments if we're not inside a complex selector that itself is unused or a global block
if (
(!is_in_global_block(path) || node.children.length > 1) &&
(!is_in_global_block(path) ||
(node.children.length > 1 && parent?.type === 'Rule' && parent.metadata.is_global_block)) &&
!path.find((n) => n.type === 'ComplexSelector' && !n.metadata.used)
) {
const children = node.children;
Expand Down Expand Up @@ -260,7 +263,6 @@ const visitors = {

// if this selector list belongs to a rule, require a specificity bump for the
// first scoped selector but only if we're at the top level
let parent = path.at(-1);
if (parent?.type === 'Rule') {
specificity = { bumped: false };

Expand Down Expand Up @@ -376,7 +378,6 @@ const visitors = {
};

/**
*
* @param {Array<AST.CSS.Node>} path
*/
function is_in_global_block(path) {
Expand Down
16 changes: 8 additions & 8 deletions packages/svelte/tests/css/samples/global-block/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ export default test({
code: 'css_unused_selector',
message: 'Unused CSS selector ".unused :global"',
start: {
line: 69,
line: 73,
column: 1,
character: 917
character: 964
},
end: {
line: 69,
line: 73,
column: 16,
character: 932
character: 979
}
},
{
code: 'css_unused_selector',
message: 'Unused CSS selector "unused :global"',
start: {
line: 100,
line: 104,
column: 29,
character: 1223
character: 1270
},
end: {
line: 100,
line: 104,
column: 43,
character: 1237
character: 1284
}
}
]
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/tests/css/samples/global-block/expected.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
.x {
color: green;
}

.a, .selector, .list {
color: green;
}
/*}*/

div.svelte-xyz {
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/tests/css/samples/global-block/input.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
.x {
color: green;
}

.a, .selector, .list {
color: green;
}
}

div :global {
Expand Down