Skip to content

Commit 224fcde

Browse files
authored
fix: preserve the separator between selectors when an unused selector is in between (#13954)
fixes #13945
1 parent 253d01e commit 224fcde

File tree

8 files changed

+66
-6
lines changed

8 files changed

+66
-6
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: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,13 @@ const visitors = {
196196
SelectorList(node, { state, next, path }) {
197197
// Only add comments if we're not inside a complex selector that itself is unused
198198
if (!path.find((n) => n.type === 'ComplexSelector' && !n.metadata.used)) {
199+
const children = node.children;
199200
let pruning = false;
200-
let prune_start = node.children[0].start;
201+
let prune_start = children[0].start;
201202
let last = prune_start;
202203

203-
for (let i = 0; i < node.children.length; i += 1) {
204-
const selector = node.children[i];
204+
for (let i = 0; i < children.length; i += 1) {
205+
const selector = children[i];
205206

206207
if (selector.metadata.used === pruning) {
207208
if (pruning) {
@@ -221,10 +222,17 @@ const visitors = {
221222
state.code.prependRight(selector.start, '/* (unused) ');
222223
}
223224
} else {
225+
// If this is not the last selector add a separator
226+
const separator = i !== children.length - 1 ? ',' : '';
227+
224228
if (state.minify) {
225229
prune_start = last;
230+
if (separator) {
231+
while (state.code.original[prune_start - 1] !== ',') prune_start++;
232+
state.code.update(last, prune_start, separator);
233+
}
226234
} else {
227-
state.code.overwrite(last, selector.start, ' /* (unused) ');
235+
state.code.overwrite(last, selector.start, `${separator} /* (unused) `);
228236
}
229237
}
230238
}
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>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!--[--><div class="foo svelte-mnmfn6">foo</div><!--]-->
1+
<!--[--><div class="foo svelte-gfnjhw">foo</div><!--]-->
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<style id="svelte-mnmfn6">.foo.svelte-mnmfn6 {color:green;}.foo.svelte-mnmfn6 {color:green;}</style>
1+
<style id="svelte-gfnjhw">.foo.svelte-gfnjhw {color:green;}.foo.svelte-gfnjhw {color:green;} .foo.svelte-gfnjhw {color:green;}.foo.svelte-gfnjhw, .foo.svelte-gfnjhw {color:green;}</style>

packages/svelte/tests/server-side-rendering/samples/css-injected-options-minify/main.svelte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@
2020
.foo, .unused {
2121
color: green;
2222
}
23+
.unused, .foo {
24+
color: green;
25+
}
26+
.foo, .unused, .foo {
27+
color: green;
28+
}
2329
</style>

0 commit comments

Comments
 (0)