From 2826227e35f4ed78ee1785a305270e58bc292245 Mon Sep 17 00:00:00 2001 From: Jordan Gilroy Date: Sun, 15 Nov 2026 14:48:38 +0000 Subject: [PATCH] Show explicit stylesheet locks when selector usage is uncertain MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A possible outside HTML source can leave every unmatched global selector uncertain, hiding an explicit stylesheet lock in the panel. Classify those locked selectors as kept so the panel reflects the user’s retention choice. Unlocking restores the underlying uncertainty. --- electron/properties/selectorUsage.ts | 13 ++++---- .../electron/properties/selectorUsage.test.ts | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/electron/properties/selectorUsage.ts b/electron/properties/selectorUsage.ts index 07719ac28..6f9ad4c56 100644 --- a/electron/properties/selectorUsage.ts +++ b/electron/properties/selectorUsage.ts @@ -189,15 +189,14 @@ function hitsFor(region: StyleRegion, sheet: AstroSheet | undefined, scans: Proj return hits; } -// A locked stylesheet (or one in a locked folder) is a library meant to sit -// unused until needed: a selector the proof finds unused there is `kept`, so -// every cleanup — the panel's, the delete offer and `style:removeSelectors` -// itself, which re-runs this check — leaves it alone. Other verdicts stand: -// "used" and "can't tell" are never removed anyway. +// An explicit stylesheet lock is the user's decision to retain its selectors. +// Surface that decision as `kept` even when outside data prevents an unused +// proof: uncertainty must not hide the lock in the panel. Used and untracked +// selectors retain their verdicts because they do not need retention protection. function lockedVerdict(verdict: SelectorVerdict, sheet: { readonly locked: boolean }): SelectorVerdict { if (sheet.locked) { - if (verdict.kind === 'unused') { - assert(verdict.names.length > 0, 'lockedVerdict: an unused selector names what is missing'); + if (verdict.kind === 'unused' || verdict.kind === 'unknown') { + assert(verdict.names.length > 0, 'lockedVerdict: a protected selector names what is missing'); return { kind: 'kept', names: verdict.names }; } return verdict; diff --git a/test/electron/properties/selectorUsage.test.ts b/test/electron/properties/selectorUsage.test.ts index 8a02ec275..dae2961a8 100644 --- a/test/electron/properties/selectorUsage.test.ts +++ b/test/electron/properties/selectorUsage.test.ts @@ -203,6 +203,36 @@ test('a locked stylesheet or folder keeps its unused selectors out of every clea assert.equal(fs.readFileSync(path.join(root, 'src/styles/patterns.css'), 'utf8'), ''); }); +test('explicit stylesheet and folder locks take priority over outside-HTML uncertainty', async () => { + const patterns = '.pattern-unused { color: red }\n.pattern-used { color: blue }\nh1 { color: green }\n'; + const root = project({ + 'src/styles/patterns.css': patterns, + 'src/lumos/utilities.css': '.utility-unused { color: red }\n', + 'src/styles/unlocked.css': '.unlocked-unused { color: red }\n', + 'src/pages/index.astro': '---\nimport { content } from \'../lib/content\';\n---\n

Hello

\n', + 'src/lib/content.ts': "export const content = await fetch('https://example.com/content').then(response => response.text());\n", + }); + const locks = parseStyleKeepList({ version: 1, keep: [], lockedStylesheets: ['src/styles/patterns.css', 'src/lumos/'] }); + const locked = await checkSelectorUsage(root, locks); + assert.deepEqual(kinds(locked.report), { + '.pattern-unused': 'kept', + '.pattern-used': 'used', + h1: 'untracked', + '.unlocked-unused': 'unknown', + '.utility-unused': 'kept', + }); + assert.deepEqual(verdictOf(locked.report, '.pattern-unused'), { kind: 'kept', names: ['pattern-unused'] }); + const entry = locked.report.entries.find((candidate) => candidate.selector === '.pattern-unused'); + assert.ok(entry, 'the locked selector exists in the report'); + const refused = await removeUnusedSelectors(root, locks, { ids: [entry.id], files: locked.report.files }); + assert.equal(refused.ok ? 'ok' : refused.error.code, 'changed'); + assert.equal(fs.readFileSync(path.join(root, 'src/styles/patterns.css'), 'utf8'), patterns); + + const unlocked = await report(root); + assert.equal(verdictOf(unlocked, '.pattern-unused').kind, 'unknown', 'unlocking restores the uncertainty instead of permitting cleanup'); + assert.equal(verdictOf(unlocked, '.utility-unused').kind, 'unknown'); +}); + test('only top-level names are requirements; keyframes, comments and other languages are left alone', async () => { const root = project({ 'src/styles/global.css':