Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 3db399f

Browse files
committed
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.
1 parent 7a67842 commit 3db399f

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

lib/cc/engine/analyzers/reporter.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def initialize(engine_config, language_strategy, io)
1212
@engine_config = engine_config
1313
@language_strategy = language_strategy
1414
@io = io
15+
@reports = Set.new
1516
end
1617

1718
def run
@@ -35,7 +36,12 @@ def process_files
3536

3637
def report
3738
flay.report(StringIO.new).each do |issue|
38-
io.puts "#{new_violation(issue).to_json}\0"
39+
violation = new_violation(issue)
40+
41+
unless reports.include?(violation.report_name)
42+
reports.add(violation.report_name)
43+
io.puts "#{violation.format.to_json}\0"
44+
end
3945
end
4046
end
4147

@@ -46,6 +52,8 @@ def process_sexp(sexp)
4652

4753
private
4854

55+
attr_reader :reports
56+
4957
def flay
5058
@flay ||= Flay.new(flay_options)
5159
end
@@ -58,7 +66,7 @@ def mass_threshold
5866

5967
def new_violation(issue)
6068
hashes = flay.hashes[issue.structural_hash]
61-
Violation.new(language_strategy.base_points, issue, hashes).format
69+
Violation.new(language_strategy.base_points, issue, hashes)
6270
end
6371

6472
def flay_options

lib/cc/engine/analyzers/violation.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def format
2626
}
2727
end
2828

29+
def report_name
30+
"#{current_sexp.file}-#{current_sexp.line}"
31+
end
32+
2933
private
3034

3135
attr_reader :base_points, :hashes

spec/cc/engine/analyzers/javascript/main_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@
5858
expect(run_engine(engine_conf)).to be_empty
5959
end
6060

61+
it "does not report the same line for multiple issues" do
62+
create_source_file("dup.jsx", <<-EOJSX)
63+
<a className='button button-primary full' href='#' onClick={this.onSubmit.bind(this)}>Login</a>
64+
EOJSX
65+
66+
result = run_engine(engine_conf).strip
67+
issues = result.split("\0")
68+
expect(issues.length).to eq 1
69+
end
70+
6171
def create_source_file(path, content)
6272
File.write(File.join(@code, path), content)
6373
end

0 commit comments

Comments
 (0)