Track defined and used CSS names for custom properties and at-rules#611
Merged
Conversation
…ames, container names, layer names, and anchor names
Introduces a reusable `DefinedUsed` class (src/defined-used.ts) that tracks
which names are declared versus referenced using O(1) Set lookups. The class is
exported from the public API so it can be reused by stylelint-plugin and
projectwallace.com.
For each entity type the semantics are:
- Custom properties: declared (`--foo: value`, `@property --foo`) vs used in `var(--foo)`
- Animation names: `@keyframes name` vs `animation-name`/`animation` shorthand
- Container names: `container-name`/`container` shorthand vs `@container name`
- Layer names: `@layer a, b;` ordering statements vs `@layer name { }` blocks and `@import layer(name)`
- Anchor names: `anchor-name: --foo` vs `position-anchor`, `anchor()`, `anchor-size()`
New fields added to:
- `properties.custom`: `defined`, `used`, `unused`, `unknown`
- `atrules.keyframes`: `defined`, `used`, `unused`, `unknown`
- `atrules.container.names`: `defined`, `used`, `unused`, `unknown`
- `atrules.layer`: `defined`, `used`, `unused`, `unknown`
- `properties.anchorNames` (new): `defined`, `used`, `unused`, `unknown`
Closes #553
https://claude.ai/code/session_013z1TNFDKSn5B5XsmKnWsGe
container-name supports multiple names (e.g. container-name: sidebar main). The containerNames Collection was incorrectly storing the full value text as a single entry; it now iterates identifiers the same way containerNamesTracking already did. Also combine the DefinedUsed value and type exports into one statement. https://claude.ai/code/session_013z1TNFDKSn5B5XsmKnWsGe
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #611 +/- ##
==========================================
+ Coverage 94.25% 94.76% +0.50%
==========================================
Files 18 19 +1
Lines 1027 1088 +61
Branches 325 349 +24
==========================================
+ Hits 968 1031 +63
+ Misses 48 47 -1
+ Partials 11 10 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Bundle ReportChanges will increase total bundle size by 3.73kB (4.64%) ⬆️. This is within the configured threshold ✅ Detailed changes
Affected Assets, Files, and Routes:view changes for bundle: analyzeCss-esmAssets Changed:
Files in
|
…-case tests All atrules tests that were using toMatchObject are now toEqual with the complete expected shape including defined/used/unused/unknown arrays. Expected values were verified against actual analyzer output. Also adds dedicated edge-case tests: - container-name: none is not tracked as a defined container name - animation-name: none is not tracked as a used animation name - position-anchor with a non-anchor value does not track https://claude.ai/code/session_013z1TNFDKSn5B5XsmKnWsGe
Previously spread both sets to arrays and then ran separate filter passes — 4 traversals and unnecessary intermediate work. Now builds defined+unused in one pass over #defined and used+unknown in one pass over #used. https://claude.ai/code/session_013z1TNFDKSn5B5XsmKnWsGe
…uting in analyze() Each define()/use() call now updates all four sets with O(1) operations: - define(): if already in used → remove from unknown; else add to unused - use(): if already in defined → remove from unused; else add to unknown analyze() becomes four spreads with no logic. The early-return guards on duplicate calls also make repeated define/use calls cheaper than before. https://claude.ai/code/session_013z1TNFDKSn5B5XsmKnWsGe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a new
DefinedUsedclass to track which CSS names (custom properties, animation names, container names, layer names, and anchor names) are defined versus used in a stylesheet. This enables detection of unused declarations and references to undefined names.Key Changes
New
DefinedUsedclass (src/defined-used.ts): A utility class that tracks defined and used names, computing four states:defined: Names that are declaredused: Names that are referencedunused: Defined but never usedunknown: Used but never definedCustom properties tracking: Monitors
--custom-propertydeclarations andvar()function usage, including nested fallbacksAnimation names tracking: Tracks
@keyframesdefinitions andanimation-name/animationproperty referencesContainer names tracking: Monitors
container-nameproperty declarations and@containerquery referencesLayer names tracking: Distinguishes between
@layerordering statements (define) and@layerblocks/@import layer()(use)Anchor names tracking: Tracks
anchor-namedeclarations and usage inposition-anchor,anchor(), andanchor-size()functionsIntegration with analysis results: Each tracked category now includes
defined,used,unused, andunknownarrays in the analysis outputComprehensive test coverage (
src/defined-used.test.ts): 50+ tests covering theDefinedUsedclass and all tracked CSS name categoriesImplementation Details
DefinedUsedclass usesSetinternally for deduplication and efficient lookupstoMatchObject()where new fields are added to maintain backward compatibility--prefix to distinguish from other identifierscloses #553