From 3db399fb5629c43b13e0cbcdd0608c09c6b3f065 Mon Sep 17 00:00:00 2001 From: Blake Williams Date: Mon, 9 Nov 2015 15:30:25 -0500 Subject: [PATCH] Don't report same line for multiple duplications This uses Ruby's [`set`](http://ruby-doc.org/stdlib-2.2.3/libdoc/set/rdoc/Set.html) class to keep track of which lines in every file is reported to prevent reporting the same line multiple times. This happens most frequently with JavaScript since the Babel generated ast is verbose and can expand a single line into a large s-expression tree which can have several duplications. --- lib/cc/engine/analyzers/reporter.rb | 12 ++++++++++-- lib/cc/engine/analyzers/violation.rb | 4 ++++ spec/cc/engine/analyzers/javascript/main_spec.rb | 10 ++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/cc/engine/analyzers/reporter.rb b/lib/cc/engine/analyzers/reporter.rb index e5d523fc..a21a4604 100644 --- a/lib/cc/engine/analyzers/reporter.rb +++ b/lib/cc/engine/analyzers/reporter.rb @@ -12,6 +12,7 @@ def initialize(engine_config, language_strategy, io) @engine_config = engine_config @language_strategy = language_strategy @io = io + @reports = Set.new end def run @@ -35,7 +36,12 @@ def process_files def report flay.report(StringIO.new).each do |issue| - io.puts "#{new_violation(issue).to_json}\0" + violation = new_violation(issue) + + unless reports.include?(violation.report_name) + reports.add(violation.report_name) + io.puts "#{violation.format.to_json}\0" + end end end @@ -46,6 +52,8 @@ def process_sexp(sexp) private + attr_reader :reports + def flay @flay ||= Flay.new(flay_options) end @@ -58,7 +66,7 @@ def mass_threshold def new_violation(issue) hashes = flay.hashes[issue.structural_hash] - Violation.new(language_strategy.base_points, issue, hashes).format + Violation.new(language_strategy.base_points, issue, hashes) end def flay_options diff --git a/lib/cc/engine/analyzers/violation.rb b/lib/cc/engine/analyzers/violation.rb index e4baf920..17b0baa6 100644 --- a/lib/cc/engine/analyzers/violation.rb +++ b/lib/cc/engine/analyzers/violation.rb @@ -26,6 +26,10 @@ def format } end + def report_name + "#{current_sexp.file}-#{current_sexp.line}" + end + private attr_reader :base_points, :hashes diff --git a/spec/cc/engine/analyzers/javascript/main_spec.rb b/spec/cc/engine/analyzers/javascript/main_spec.rb index 0f5e1c43..5cb46b8a 100644 --- a/spec/cc/engine/analyzers/javascript/main_spec.rb +++ b/spec/cc/engine/analyzers/javascript/main_spec.rb @@ -58,6 +58,16 @@ expect(run_engine(engine_conf)).to be_empty end + it "does not report the same line for multiple issues" do + create_source_file("dup.jsx", <<-EOJSX) + Login + EOJSX + + result = run_engine(engine_conf).strip + issues = result.split("\0") + expect(issues.length).to eq 1 + end + def create_source_file(path, content) File.write(File.join(@code, path), content) end