From 956c635fa388a541f3def765c358868728ad5791 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 15 Feb 2024 17:07:16 -0500 Subject: [PATCH] simplify --- .../compiler/phases/3-transform/css/index.js | 53 ++++++++----------- 1 file changed, 22 insertions(+), 31 deletions(-) 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 770865d449..d3054db81d 100644 --- a/packages/svelte/src/compiler/phases/3-transform/css/index.js +++ b/packages/svelte/src/compiler/phases/3-transform/css/index.js @@ -126,9 +126,7 @@ const visitors = { return; } - const used = node.prelude.children.filter((s) => s.metadata.used); - - if (used.length === 0) { + if (!node.prelude.children.some((s) => s.metadata.used)) { state.code.prependRight(node.start, '/* (unused) '); state.code.appendLeft(node.end, '*/'); escape_comment_close(node, state.code); @@ -139,45 +137,38 @@ const visitors = { next(); }, SelectorList(node, { state, next, path }) { - const used = node.children.filter((s) => s.metadata.used); - - if (used.length < node.children.length) { - let pruning = false; - let last = node.children[0].start; + 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, '*/'); + state.code.overwrite(i, i + 1, '*/'); + } else { + if (i === 0) { + state.code.prependRight(selector.start, '/* (unused) '); } else { - if (i === 0) { - state.code.prependRight(selector.start, '/* (unused) '); - } else { - state.code.overwrite(last, selector.start, ' /* (unused) '); - } + state.code.overwrite(last, selector.start, ' /* (unused) '); } - - pruning = !pruning; } - last = selector.end; + pruning = !pruning; } - if (pruning) { - state.code.appendLeft(last, '*/'); - } + last = selector.end; + } + + if (pruning) { + state.code.appendLeft(last, '*/'); } - const parent = /** @type {import('#compiler').Css.Node} */ (path.at(-1)); - next({ - ...state, - specificity: parent.type === 'Rule' ? { bumped: false } : state.specificity - }); + const specificity = path.at(-1)?.type === 'Rule' ? { bumped: false } : state.specificity; + next({ ...state, specificity }); }, ComplexSelector(node, context) { /** @param {import('#compiler').Css.SimpleSelector} selector */