Skip to content

Commit b0dbc8f

Browse files
committed
fix: preserve the separator between selectors when an unused selector is in between
1 parent 7be3afb commit b0dbc8f

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

.changeset/eighty-icons-fold.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: preserve the separator between selectors when an unused selector is in between

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,13 @@ const visitors = {
162162
// Only add comments if we're not inside a complex selector that itself is unused
163163
if (!path.find((n) => n.type === 'ComplexSelector' && !n.metadata.used)) {
164164
let pruning = false;
165-
let last = node.children[0].start;
166165

167-
for (let i = 0; i < node.children.length; i += 1) {
168-
const selector = node.children[i];
166+
const children = node.children;
167+
168+
let last = children.start;
169+
170+
for (let i = 0; i < children.length; i += 1) {
171+
const selector = children[i];
169172

170173
if (selector.metadata.used === pruning) {
171174
if (pruning) {
@@ -177,7 +180,10 @@ const visitors = {
177180
if (i === 0) {
178181
state.code.prependRight(selector.start, '/* (unused) ');
179182
} else {
180-
state.code.overwrite(last, selector.start, ' /* (unused) ');
183+
// If this is not the last selector add a separator
184+
const separator = i !== children.length - 1 ? ',' : '';
185+
186+
state.code.overwrite(last, selector.start, `${separator} /* (unused) `);
181187
}
182188
}
183189

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

0 commit comments

Comments
 (0)