Class: SeeLessEssEss::RemoveUnusedRules

Inherits:
Sass::Tree::Visitors::Base
  • Object
show all
Defined in:
lib/see-less-ess-ess.rb

Instance Method Summary collapse

Constructor Details

#initialize(checker) ⇒ RemoveUnusedRules

Returns a new instance of RemoveUnusedRules.



93
94
95
# File 'lib/see-less-ess-ess.rb', line 93

def initialize(checker)
  @checker = checker
end

Instance Method Details

#remove_unused_children(node) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/see-less-ess-ess.rb', line 105

def remove_unused_children(node)
  # Strip out any unused rules
  node.children.reject! do |child|
    if child.invisible?
      # Already wont be shown
      false
    elsif not child.respond_to? :resolved_rules
      # Only mess with RuleNodes
      false
    else
      child.resolved_rules.members.reject! do |sequence|
        @checker.does_not_use(sequence)
      end

      # Remove if there's nothing left
      child.resolved_rules.members.empty?
    end
  end

  yield
end

#visit(node) ⇒ Object

If an exception is raised, this adds proper metadata to the backtrace.



98
99
100
101
102
103
# File 'lib/see-less-ess-ess.rb', line 98

def visit(node)
  super(node)
rescue Sass::SyntaxError => e
  e.modify_backtrace(:filename => node.filename, :line => node.line)
  raise e
end

#visit_media(node, &block) ⇒ Object



130
131
132
# File 'lib/see-less-ess-ess.rb', line 130

def visit_media(node, &block)
  remove_unused_children(node, &block)
end

#visit_root(node, &block) ⇒ Object



127
128
129
# File 'lib/see-less-ess-ess.rb', line 127

def visit_root(node, &block)
  remove_unused_children(node, &block)
end