Skip to content

fix: don't consider used rules with atrules in the block #13410

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

Merged
merged 4 commits into from
Sep 27, 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/lazy-queens-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: don't consider children of rules when checking whether they are used or not
16 changes: 2 additions & 14 deletions packages/svelte/src/compiler/phases/3-transform/css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ function is_empty(rule) {
}

if (child.type === 'Atrule') {
return false; // TODO
if (child.block === null || child.block.children.length > 0) return false;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be child.block !== null? How does a media query with a null block looks like?

Copy link
Member

@dummdidumm dummdidumm Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an AtRule can also be something like @import ... which has no child block, which means something is happening within the rule

}
}

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

/** @param {Css.Rule} rule */
function is_used(rule) {
for (const selector of rule.prelude.children) {
if (selector.metadata.used) return true;
}

for (const child of rule.block.children) {
if (child.type === 'Rule' && is_used(child)) return true;

if (child.type === 'Atrule') {
return true; // TODO
}
}

return false;
return rule.prelude.children.some((selector) => selector.metadata.used);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions packages/svelte/tests/css/samples/unused-nested-at-rule/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { test } from '../../test';

export default test({
warnings: [
{
filename: 'SvelteComponent.svelte',
code: 'css_unused_selector',
message: 'Unused CSS selector ".unused"',
start: {
line: 2,
column: 1,
character: 9
},
end: {
line: 2,
column: 8,
character: 16
}
}
]
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

/* (unused) .unused {
@media (min-width: 400px) {
color: red;
}
}*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<style>
.unused {
@media (min-width: 400px) {
color: red;
}
}
</style>