Skip to content
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
13 changes: 6 additions & 7 deletions electron/properties/selectorUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions test/electron/properties/selectorUsage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<h1 class="pattern-used">Hello</h1><article set:html={content} />\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':
Expand Down