Commit c7937b9
Track defined and used CSS names for custom properties and at-rules (#611)
## Summary
This PR introduces a new `DefinedUsed` class 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 `DefinedUsed` class** (`src/defined-used.ts`): A utility class
that tracks defined and used names, computing four states:
- `defined`: Names that are declared
- `used`: Names that are referenced
- `unused`: Defined but never used
- `unknown`: Used but never defined
- **Custom properties tracking**: Monitors `--custom-property`
declarations and `var()` function usage, including nested fallbacks
- **Animation names tracking**: Tracks `@keyframes` definitions and
`animation-name`/`animation` property references
- **Container names tracking**: Monitors `container-name` property
declarations and `@container` query references
- **Layer names tracking**: Distinguishes between `@layer` ordering
statements (define) and `@layer` blocks/`@import layer()` (use)
- **Anchor names tracking**: Tracks `anchor-name` declarations and usage
in `position-anchor`, `anchor()`, and `anchor-size()` functions
- **Integration with analysis results**: Each tracked category now
includes `defined`, `used`, `unused`, and `unknown` arrays in the
analysis output
- **Comprehensive test coverage** (`src/defined-used.test.ts`): 50+
tests covering the `DefinedUsed` class and all tracked CSS name
categories
## Implementation Details
- The `DefinedUsed` class uses `Set` internally for deduplication and
efficient lookups
- Integration points throughout the CSS parser capture definitions and
usages at appropriate AST nodes
- Existing tests updated to use `toMatchObject()` where new fields are
added to maintain backward compatibility
- Anchor names specifically track the `--` prefix to distinguish from
other identifiers
closes #553
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 50fac71 commit c7937b9
5 files changed
Lines changed: 611 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
191 | 253 | | |
192 | 254 | | |
193 | 255 | | |
| |||
481 | 543 | | |
482 | 544 | | |
483 | 545 | | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
484 | 550 | | |
485 | 551 | | |
486 | 552 | | |
| |||
716 | 782 | | |
717 | 783 | | |
718 | 784 | | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
719 | 789 | | |
720 | 790 | | |
721 | 791 | | |
| |||
740 | 810 | | |
741 | 811 | | |
742 | 812 | | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
743 | 817 | | |
744 | 818 | | |
745 | 819 | | |
| |||
824 | 898 | | |
825 | 899 | | |
826 | 900 | | |
827 | | - | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
828 | 906 | | |
829 | 907 | | |
830 | 908 | | |
| |||
838 | 916 | | |
839 | 917 | | |
840 | 918 | | |
841 | | - | |
842 | | - | |
843 | 919 | | |
844 | | - | |
845 | | - | |
| 920 | + | |
| 921 | + | |
846 | 922 | | |
847 | 923 | | |
848 | 924 | | |
849 | 925 | | |
850 | | - | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
851 | 931 | | |
852 | 932 | | |
853 | 933 | | |
| |||
861 | 941 | | |
862 | 942 | | |
863 | 943 | | |
864 | | - | |
865 | | - | |
866 | 944 | | |
867 | | - | |
868 | | - | |
| 945 | + | |
| 946 | + | |
869 | 947 | | |
870 | 948 | | |
871 | 949 | | |
872 | 950 | | |
873 | | - | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
874 | 956 | | |
875 | 957 | | |
876 | 958 | | |
| |||
884 | 966 | | |
885 | 967 | | |
886 | 968 | | |
887 | | - | |
888 | | - | |
889 | 969 | | |
890 | | - | |
891 | | - | |
| 970 | + | |
| 971 | + | |
892 | 972 | | |
893 | 973 | | |
894 | 974 | | |
895 | 975 | | |
896 | | - | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
897 | 981 | | |
898 | 982 | | |
899 | 983 | | |
| |||
0 commit comments