This repository was archived by the owner on Jul 19, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +76
-13
lines changed Expand file tree Collapse file tree 4 files changed +76
-13
lines changed Original file line number Diff line number Diff line change 1+ module CC
2+ module Engine
3+ module Analyzers
4+ class SexpLines
5+ attr_reader :begin_line , :end_line
6+
7+ def initialize ( root_sexp )
8+ @root_sexp = root_sexp
9+ calculate
10+ end
11+
12+ private
13+
14+ attr_reader :root_sexp
15+
16+ def calculate
17+ @begin_line = root_sexp . line
18+ @end_line = root_sexp . end_line || root_sexp . line
19+
20+ root_sexp . deep_each do |sexp |
21+ @begin_line = [ @begin_line , sexp . line ] . min
22+ @end_line = [ @end_line , sexp . end_line || sexp . line ] . max
23+ end
24+ end
25+ end
26+ end
27+ end
28+ end
Original file line number Diff line number Diff line change @@ -69,25 +69,16 @@ def format_other_locations
6969 end
7070
7171 def format_sexp ( sexp )
72+ lines = SexpLines . new ( sexp )
7273 {
7374 "path" : sexp . file . gsub ( %r(^./) , "" ) ,
7475 "lines" : {
75- "begin" : sexp . line ,
76- "end" : sexp . end_line || sexp_max_line ( sexp , sexp . line )
77- }
76+ "begin" : lines . begin_line ,
77+ "end" : lines . end_line ,
78+ } ,
7879 }
7980 end
8081
81- def sexp_max_line ( sexp_tree , default )
82- max = default
83-
84- sexp_tree . deep_each do |sexp |
85- max = sexp . line if sexp . line > max
86- end
87-
88- max
89- end
90-
9182 def content_body
9283 @_content_body ||= { "body" : File . read ( read_up_path ) }
9384 end
Original file line number Diff line number Diff line change 66require 'cc/engine/analyzers/reporter'
77require 'cc/engine/analyzers/engine_config'
88require 'cc/engine/analyzers/sexp'
9+ require 'cc/engine/analyzers/sexp_lines'
910require 'flay'
1011require 'json'
1112
Original file line number Diff line number Diff line change 1+ require "spec_helper"
2+ require "cc/engine/duplication"
3+
4+ module CC ::Engine ::Analyzers
5+ RSpec . describe SexpLines do
6+ describe "violation location" do
7+ it "gets appropriate locations for rescue blocks" do
8+ source = <<-SOURCE
9+ begin
10+ foo
11+ rescue SyntaxError => e
12+ Jekyll.logger.warn "YAML Exception reading \# {File.join(base, name)}: \# {e.message}"
13+ rescue Exception => e
14+ Jekyll.logger.warn "Error reading file \# {File.join(base, name)}: \# {e.message}"
15+ end
16+ SOURCE
17+ flay = Flay . new ( {
18+ diff : false ,
19+ mass : CC ::Engine ::Analyzers ::Ruby ::Main ::DEFAULT_MASS_THRESHOLD ,
20+ summary : false ,
21+ verbose : false ,
22+ number : true ,
23+ timeout : 10 ,
24+ liberal : false ,
25+ fuzzy : false ,
26+ only : nil ,
27+ } )
28+
29+ sexp = RubyParser . new . process ( source , "file.rb" )
30+ flay . process_sexp ( sexp )
31+ report = flay . analyze [ 0 ]
32+ sexps = flay . hashes [ report . structural_hash ]
33+ locations = sexps . map { |sexp | SexpLines . new ( sexp ) }
34+
35+ expect ( locations . count ) . to eq 2
36+ expect ( locations [ 0 ] . begin_line ) . to eq ( 3 )
37+ expect ( locations [ 0 ] . end_line ) . to eq ( 7 )
38+ expect ( locations [ 1 ] . begin_line ) . to eq ( 5 )
39+ expect ( locations [ 1 ] . end_line ) . to eq ( 7 )
40+ end
41+ end
42+ end
43+ end
You can’t perform that action at this time.
0 commit comments