Skip to content

Commit 30c438c

Browse files
fix: don't consider children of rules when checking whether they are used or not (#13410)
Fixes #13390 There's pruning logic in a different place where all unused selectors are commented out. If all selectors are unused, the whole prelude is commented out, resulting in invalid syntax. This is a case that shouldn't happen, therefore simplify the whole "is used" logic to only look at the prelude.
1 parent e3e8f22 commit 30c438c

File tree

5 files changed

+41
-14
lines changed

5 files changed

+41
-14
lines changed

.changeset/lazy-queens-agree.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 consider children of rules when checking whether they are used or not

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ function is_empty(rule) {
333333
}
334334

335335
if (child.type === 'Atrule') {
336-
return false; // TODO
336+
if (child.block === null || child.block.children.length > 0) return false;
337337
}
338338
}
339339

@@ -342,19 +342,7 @@ function is_empty(rule) {
342342

343343
/** @param {Css.Rule} rule */
344344
function is_used(rule) {
345-
for (const selector of rule.prelude.children) {
346-
if (selector.metadata.used) return true;
347-
}
348-
349-
for (const child of rule.block.children) {
350-
if (child.type === 'Rule' && is_used(child)) return true;
351-
352-
if (child.type === 'Atrule') {
353-
return true; // TODO
354-
}
355-
}
356-
357-
return false;
345+
return rule.prelude.children.some((selector) => selector.metadata.used);
358346
}
359347

360348
/**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
warnings: [
5+
{
6+
filename: 'SvelteComponent.svelte',
7+
code: 'css_unused_selector',
8+
message: 'Unused CSS selector ".unused"',
9+
start: {
10+
line: 2,
11+
column: 1,
12+
character: 9
13+
},
14+
end: {
15+
line: 2,
16+
column: 8,
17+
character: 16
18+
}
19+
}
20+
]
21+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
/* (unused) .unused {
3+
@media (min-width: 400px) {
4+
color: red;
5+
}
6+
}*/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<style>
2+
.unused {
3+
@media (min-width: 400px) {
4+
color: red;
5+
}
6+
}
7+
</style>

0 commit comments

Comments
 (0)