From 30c438c279488613b8520a94c4334c2537a17de0 Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Fri, 27 Sep 2024 12:45:09 +0200 Subject: [PATCH] fix: don't consider children of rules when checking whether they are used or not (#13410) Fixes #13390 There's pruning logic in a different place where all unused selectors are commented out. If all selectors are unused, the whole prelude is commented out, resulting in invalid syntax. This is a case that shouldn't happen, therefore simplify the whole "is used" logic to only look at the prelude. --- .changeset/lazy-queens-agree.md | 5 +++++ .../compiler/phases/3-transform/css/index.js | 16 ++------------ .../samples/unused-nested-at-rule/_config.js | 21 +++++++++++++++++++ .../unused-nested-at-rule/expected.css | 6 ++++++ .../unused-nested-at-rule/input.svelte | 7 +++++++ 5 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 .changeset/lazy-queens-agree.md create mode 100644 packages/svelte/tests/css/samples/unused-nested-at-rule/_config.js create mode 100644 packages/svelte/tests/css/samples/unused-nested-at-rule/expected.css create mode 100644 packages/svelte/tests/css/samples/unused-nested-at-rule/input.svelte diff --git a/.changeset/lazy-queens-agree.md b/.changeset/lazy-queens-agree.md new file mode 100644 index 0000000000..4b15b02a8c --- /dev/null +++ b/.changeset/lazy-queens-agree.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: don't consider children of rules when checking whether they are used or not diff --git a/packages/svelte/src/compiler/phases/3-transform/css/index.js b/packages/svelte/src/compiler/phases/3-transform/css/index.js index 0b88a481a0..f24baed32b 100644 --- a/packages/svelte/src/compiler/phases/3-transform/css/index.js +++ b/packages/svelte/src/compiler/phases/3-transform/css/index.js @@ -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; } } @@ -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); } /** diff --git a/packages/svelte/tests/css/samples/unused-nested-at-rule/_config.js b/packages/svelte/tests/css/samples/unused-nested-at-rule/_config.js new file mode 100644 index 0000000000..1271c37513 --- /dev/null +++ b/packages/svelte/tests/css/samples/unused-nested-at-rule/_config.js @@ -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 + } + } + ] +}); diff --git a/packages/svelte/tests/css/samples/unused-nested-at-rule/expected.css b/packages/svelte/tests/css/samples/unused-nested-at-rule/expected.css new file mode 100644 index 0000000000..9886643cc7 --- /dev/null +++ b/packages/svelte/tests/css/samples/unused-nested-at-rule/expected.css @@ -0,0 +1,6 @@ + + /* (unused) .unused { + @media (min-width: 400px) { + color: red; + } + }*/ diff --git a/packages/svelte/tests/css/samples/unused-nested-at-rule/input.svelte b/packages/svelte/tests/css/samples/unused-nested-at-rule/input.svelte new file mode 100644 index 0000000000..4683799ca9 --- /dev/null +++ b/packages/svelte/tests/css/samples/unused-nested-at-rule/input.svelte @@ -0,0 +1,7 @@ + \ No newline at end of file