From e8901aafb776afa1a5ce06ba8ff5496ac3844159 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Mon, 14 Sep 2015 00:53:06 -0400 Subject: [PATCH 1/4] added reporting exact selector matches with a test --- lib/csscss/cli.rb | 9 +++++---- lib/csscss/redundancy_analyzer.rb | 24 ++++++++++++++++++++++-- lib/csscss/reporter.rb | 9 ++++++++- test/csscss/redundancy_analyzer_test.rb | 19 +++++++++++++++++-- test/csscss/reporter_test.rb | 4 ++-- 5 files changed, 54 insertions(+), 11 deletions(-) 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..7f63492 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..14aa496 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 From 4a548289dd0e552afdd06dc93bdf44fc0f6f8d1b Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Mon, 14 Sep 2015 00:22:38 -0500 Subject: [PATCH 2/4] added changelog comment --- CHANGELOG.md | 4 ++++ lib/csscss/reporter.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) 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/reporter.rb b/lib/csscss/reporter.rb index 7f63492..0c4ae91 100644 --- a/lib/csscss/reporter.rb +++ b/lib/csscss/reporter.rb @@ -2,7 +2,7 @@ module Csscss class Reporter def initialize(redundancies, exactSelectors) @redundancies = redundancies - @exactMatchSelectors = exactSelectors + @exactMatchSelectors = exactSelectors end def report(options = {}) From f86a07327d4cb3db2631855db32f4373a29e21b5 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Mon, 14 Sep 2015 10:02:17 -0600 Subject: [PATCH 3/4] fixed weird indentation --- lib/csscss/reporter.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/csscss/reporter.rb b/lib/csscss/reporter.rb index 0c4ae91..d00c79d 100644 --- a/lib/csscss/reporter.rb +++ b/lib/csscss/reporter.rb @@ -20,11 +20,11 @@ def report(options = {}) 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 + @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 From 07d07b3f1bc739e41e9fa11b1b53001c736912c4 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Mon, 14 Sep 2015 10:11:20 -0600 Subject: [PATCH 4/4] fixed spacing around => --- test/csscss/redundancy_analyzer_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/csscss/redundancy_analyzer_test.rb b/test/csscss/redundancy_analyzer_test.rb index 14aa496..8817f22 100644 --- a/test/csscss/redundancy_analyzer_test.rb +++ b/test/csscss/redundancy_analyzer_test.rb @@ -297,9 +297,9 @@ module Csscss redundacyAnalyizer = RedundancyAnalyzer.new(cssForExactMatchedSelectors) redundancies = redundacyAnalyizer.redundancies redundacyAnalyizer.matchedSelectors.must_equal([ - {:name=>"h1, h2", :count=>1}, - {:name=>".joe", :count=>2}, - {:name=>".baz", :count=>1} + {:name => "h1, h2", :count => 1}, + {:name => ".joe", :count => 2}, + {:name => ".baz", :count => 1} ]) end # TODO: someday