Class: Rails::CssUnused::StylesheetScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/css_unused/stylesheet_scanner.rb

Overview

Extracts class selectors from CSS/SCSS/Sass files (static parse, no full AST).

Constant Summary collapse

CLASS_SELECTOR_PATTERN =

.class-name, .foo.bar, div.class — capture class tokens after dots

/\.([a-zA-Z_][\w-]*(?:--[\w-]+)?)/
BLOCK_COMMENT =

Strip comments before matching

%r{/\*.*?\*/}m
LINE_COMMENT =
%r{//[^\n]*}

Instance Method Summary collapse

Constructor Details

#initialize(root:, config: CssUnused.configuration) ⇒ StylesheetScanner

Returns a new instance of StylesheetScanner.



14
15
16
17
# File 'lib/rails/css_unused/stylesheet_scanner.rb', line 14

def initialize(root:, config: CssUnused.configuration)
  @root = Pathname(root)
  @config = config
end

Instance Method Details

#defined_classesObject



19
20
21
22
23
24
# File 'lib/rails/css_unused/stylesheet_scanner.rb', line 19

def defined_classes
  classes = Set.new
  each_stylesheet_file { |content| classes.merge(extract_from(strip_comments(content))) }
  classes.subtract(normalized_ignore_list)
  classes
end