Class: Rails::CssUnused::StylesheetScanner
- Inherits:
-
Object
- Object
- Rails::CssUnused::StylesheetScanner
- Defined in:
- lib/rails/css_unused/stylesheet_scanner.rb
Overview
Scans CSS / SCSS / Sass stylesheets and extracts every defined class selector.
Key improvements over v0.1:
- Tracks which FILE each class was defined in (for --show-source-files).
- Properly strips @charset, @import, @media, @keyframes, @font-face noise.
- Ignores pseudo-class arguments like :not(.foo) — .foo is counted as used.
- Handles BEM double-dash and double-underscore selectors.
- Skips file-extension false positives (.png, .jpg, .css, .js etc.)
- Skips @-rule word tokens that look like class selectors (.keyframe-name).
- Applies ignore_classes AND ignore_patterns from configuration.
Defined Under Namespace
Classes: DefinedClass
Constant Summary collapse
- CLASS_SELECTOR_PATTERN =
Match .classname anywhere a class selector can appear. Deliberately broad — noise is filtered by the skip logic below.
/(?<![:\w])\.(-?[a-zA-Z_][a-zA-Z0-9_-]*)/- BLOCK_COMMENT =
%r{/\*.*?\*/}m- LINE_COMMENT =
%r{//[^\n]*}- STRING_LITERAL =
strip quoted strings first
/(['"])(?:(?!\1).)*\1/- AT_RULE_NOISE =
At-rule keywords whose following token looks like a class selector but is actually metadata: @charset, @import, @namespace, @keyframes, etc.
%w[ charset import namespace supports keyframes font-face font-feature-values counter-style layer page media document ].freeze
- EXTENSION_NOISE =
File extensions that produce false positives when found after a dot.
%w[ png jpg jpeg gif svg webp ico bmp tiff css scss sass less js ts jsx tsx mjs cjs rb erb haml slim html htm xml json yaml yml pdf zip gz tar woff woff2 ttf eot map min ].freeze
Instance Method Summary collapse
-
#defined_classes ⇒ Object
Returns a Set of plain class name strings (for diff calculations).
-
#defined_classes_with_sources ⇒ Object
Returns Array<DefinedClass> with source file info.
-
#initialize(root:, config: CssUnused.configuration) ⇒ StylesheetScanner
constructor
A new instance of StylesheetScanner.
Constructor Details
#initialize(root:, config: CssUnused.configuration) ⇒ StylesheetScanner
Returns a new instance of StylesheetScanner.
45 46 47 48 |
# File 'lib/rails/css_unused/stylesheet_scanner.rb', line 45 def initialize(root:, config: CssUnused.configuration) @root = Pathname(root) @config = config end |
Instance Method Details
#defined_classes ⇒ Object
Returns a Set of plain class name strings (for diff calculations).
51 52 53 |
# File 'lib/rails/css_unused/stylesheet_scanner.rb', line 51 def defined_classes scan_all.map(&:name).to_set end |
#defined_classes_with_sources ⇒ Object
Returns Array<DefinedClass> with source file info.
56 57 58 |
# File 'lib/rails/css_unused/stylesheet_scanner.rb', line 56 def defined_classes_with_sources scan_all end |