Skip to content
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

fix: do not comment out unused selectors that are inside an unused selector #13746

Merged
merged 1 commit into from
Oct 21, 2024
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/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