Skip to content

Commit

Permalink
fix: do not comment out unused selectors that are inside an unused se…
Browse files Browse the repository at this point in the history
…lector

fixes #13680
  • Loading branch information
dummdidumm committed Oct 21, 2024
1 parent a08f063 commit 94958ad
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-kids-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: do not comment out unused selectors that are inside an unused selector
41 changes: 22 additions & 19 deletions packages/svelte/src/compiler/phases/3-transform/css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,34 +159,37 @@ const visitors = {
next();
},
SelectorList(node, { state, next, path }) {
let pruning = false;
let last = node.children[0].start;
// Only add comments if we're not inside a complex selector that itself is unused
if (!path.find((n) => n.type === 'ComplexSelector' && !n.metadata.used)) {
let pruning = false;
let last = node.children[0].start;

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

if (selector.metadata.used === pruning) {
if (pruning) {
let i = selector.start;
while (state.code.original[i] !== ',') i--;
if (selector.metadata.used === pruning) {
if (pruning) {
let i = selector.start;
while (state.code.original[i] !== ',') i--;

state.code.overwrite(i, i + 1, '*/');
} else {
if (i === 0) {
state.code.prependRight(selector.start, '/* (unused) ');
state.code.overwrite(i, i + 1, '*/');
} else {
state.code.overwrite(last, selector.start, ' /* (unused) ');
if (i === 0) {
state.code.prependRight(selector.start, '/* (unused) ');
} else {
state.code.overwrite(last, selector.start, ' /* (unused) ');
}
}

pruning = !pruning;
}

pruning = !pruning;
last = selector.end;
}

last = selector.end;
}

if (pruning) {
state.code.appendLeft(last, '*/');
if (pruning) {
state.code.appendLeft(last, '*/');
}
}

// if we're in a `:is(...)` or whatever, keep existing specificity bump state
Expand Down
22 changes: 18 additions & 4 deletions packages/svelte/tests/css/samples/has/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,32 @@ export default test({
line: 81
}
},
{
code: 'css_unused_selector',
message: 'Unused CSS selector ".unused:has(.unused)"',
start: {
line: 84,
column: 1,
character: 934
},
end: {
line: 84,
column: 21,
character: 954
}
},
{
code: 'css_unused_selector',
message: 'Unused CSS selector "x:has(> z)"',
start: {
line: 88,
line: 91,
column: 1,
character: 968
character: 1021
},
end: {
line: 88,
line: 91,
column: 11,
character: 978
character: 1031
}
}
]
Expand Down
3 changes: 3 additions & 0 deletions packages/svelte/tests/css/samples/has/expected.css
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
/* (unused) .unused x:has(y) {
color: red;
}*/
/* (unused) .unused:has(.unused)*/ x.svelte-xyz:has(y:where(.svelte-xyz)) {
color: green;
}

x.svelte-xyz:has(> y:where(.svelte-xyz)) {
color: green;
Expand Down
3 changes: 3 additions & 0 deletions packages/svelte/tests/css/samples/has/input.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
.unused x:has(y) {
color: red;
}
.unused:has(.unused), x:has(y) {
color: green;
}
x:has(> y) {
color: green;
Expand Down

0 comments on commit 94958ad

Please sign in to comment.