diff --git a/CHANGELOG.md b/CHANGELOG.md index 024f5ee..51252bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/csscss/cli.rb b/lib/csscss/cli.rb index 0adec9e..1e40c80 100644 --- a/lib/csscss/cli.rb +++ b/lib/csscss/cli.rb @@ -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 diff --git a/lib/csscss/redundancy_analyzer.rb b/lib/csscss/redundancy_analyzer.rb index be9de91..3ae20c3 100644 --- a/lib/csscss/redundancy_analyzer.rb +++ b/lib/csscss/redundancy_analyzer.rb @@ -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] || [] @@ -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) diff --git a/lib/csscss/reporter.rb b/lib/csscss/reporter.rb index 96f428c..d00c79d 100644 --- a/lib/csscss/reporter.rb +++ b/lib/csscss/reporter.rb @@ -1,7 +1,8 @@ module Csscss class Reporter - def initialize(redundancies) + def initialize(redundancies, exactSelectors) @redundancies = redundancies + @exactMatchSelectors = exactSelectors end def report(options = {}) @@ -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 diff --git a/test/csscss/redundancy_analyzer_test.rb b/test/csscss/redundancy_analyzer_test.rb index 8f72e25..8817f22 100644 --- a/test/csscss/redundancy_analyzer_test.rb +++ b/test/csscss/redundancy_analyzer_test.rb @@ -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 = %$ diff --git a/test/csscss/reporter_test.rb b/test/csscss/reporter_test.rb index eb8fcbf..bbf2444 100644 --- a/test/csscss/reporter_test.rb +++ b/test/csscss/reporter_test.rb @@ -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 @@ -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