Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

added reporting exact selector matches with a test #95

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased - 9/14/2015 ##

* Added reporting exact matchs of selectors

## 1.3.3 - 5/30/2014 ##

* Upgrades parslet dependency to v1.6.1 and drops optimization monkeypatch
Expand Down
9 changes: 5 additions & 4 deletions lib/csscss/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ def execute
end.join("\n")

unless all_contents.strip.empty?
redundancies = RedundancyAnalyzer.new(all_contents).redundancies(
redundancyAnalyzer = RedundancyAnalyzer.new(all_contents)
redundancies = redundancyAnalyzer.redundancies(
minimum: @minimum,
ignored_properties: @ignored_properties,
ignored_selectors: @ignored_selectors,
match_shorthand: @match_shorthand
)

exactSelectorMatches = redundancyAnalyzer.matchedSelectors
if @json
puts JSONReporter.new(redundancies).report
puts JSONReporter.new(redundancies, exactSelectorMatches).report
else
report = Reporter.new(redundancies).report(verbose:@verbose, color:@color)
report = Reporter.new(redundancies, exactSelectorMatches).report(verbose:@verbose, color:@color)
puts report unless report.empty?
end
end
Expand Down
24 changes: 22 additions & 2 deletions lib/csscss/redundancy_analyzer.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
# TODO: this class really needs some work. It works, but it is horrid.
module Csscss

class RedundancyAnalyzer
def initialize(raw_css)
@raw_css = raw_css
@matchedSelectors = []
end
def matchedSelectors
return @matchedSelectors
end
def storeSelectors(selectors,newSelector)
added = false
if selectors != []
selectors.each do |selector|
if selector[:name] == newSelector
selector[:count] += 1
added = true
end
end
end
if !added
selectors << { name: newSelector, count: 1}
end
end

def redundancies(opts = {})
minimum = opts[:minimum]
ignored_properties = opts[:ignored_properties] || []
Expand All @@ -17,7 +35,9 @@ def redundancies(opts = {})
rule_sets.each do |rule_set|
next if ignored_selectors.include?(rule_set.selectors.selectors)
sel = rule_set.selectors

#store a count of all selectors
storeSelectors @matchedSelectors, sel[:selectors]

rule_set.declarations.each do |dec|
next if ignored_properties.include?(dec.property)

Expand Down
9 changes: 8 additions & 1 deletion lib/csscss/reporter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Csscss
class Reporter
def initialize(redundancies)
def initialize(redundancies, exactSelectors)
@redundancies = redundancies
@exactMatchSelectors = exactSelectors
end

def report(options = {})
Expand All @@ -18,6 +19,12 @@ def report(options = {})
declarations.each {|dec| io.puts(" - #{maybe_color(dec, :yellow, should_color)}") }
end
end

@exactMatchSelectors.each do |selector|
if selector[:count] > 1
io.puts %Q({#{maybe_color(selector[:name], :red, should_color)}} repeated #{maybe_color(selector[:count], :red, should_color)} times )
end
end

io.rewind
io.read
Expand Down
19 changes: 17 additions & 2 deletions test/csscss/redundancy_analyzer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,23 @@ module Csscss
[sel(".bar"), sel(".foo")] => [dec("padding", "0")]
})
end



it "finds selectors that are exactly the same .joe .joe" do
cssForExactMatchedSelectors = %$
h1, h2 { display: none; position: relative; outline:none}
.joe { display: none; width: 1px }
.joe { position: relative; width: 1px; outline: none }
.baz { display: none }
$

redundacyAnalyizer = RedundancyAnalyzer.new(cssForExactMatchedSelectors)
redundancies = redundacyAnalyizer.redundancies
redundacyAnalyizer.matchedSelectors.must_equal([
{:name => "h1, h2", :count => 1},
{:name => ".joe", :count => 2},
{:name => ".baz", :count => 1}
])
end
# TODO: someday
# it "reports duplication within the same selector" do
# css = %$
Expand Down
4 changes: 2 additions & 2 deletions test/csscss/reporter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Csscss
[sel(".foo"), sel(".bar")] => [dec("width", "1px"), dec("border", "black")],
[sel("h1, h2"), sel(".foo"), sel(".baz")] => [dec("display", "none")],
[sel("h1, h2"), sel(".bar")] => [dec("position", "relative")]
})
},[])

expected =<<-EXPECTED
{.foo} AND {.bar} share 2 declarations
Expand All @@ -31,7 +31,7 @@ module Csscss
end

it "prints a new line if there is nothing" do
reporter = Reporter.new({})
reporter = Reporter.new({},[])
reporter.report().must_equal ""
end
end
Expand Down