Skip to content

Commit d0955c6

Browse files
author
ABaldwinHunter
committed
Include erb files for analysis, following Flay
This erb-processing code is lifted directly from flay: https://github.com/seattlerb/flay/blob/master/lib/flay_erb.rb#L13 Add specs
1 parent 77f00d6 commit d0955c6

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
source 'https://rubygems.org'
22

3+
gem 'erubis'
34
gem 'flay', git: 'https://github.com/codeclimate/flay.git'
45
gem 'concurrent-ruby', "~> 1.0.0"
56
gem 'ruby_parser'

Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ GEM
1010
coderay (1.1.0)
1111
concurrent-ruby (1.0.0)
1212
diff-lcs (1.2.5)
13+
erubis (2.7.0)
1314
json (1.8.3)
1415
method_source (0.8.2)
1516
pry (0.10.3)
@@ -40,6 +41,7 @@ PLATFORMS
4041

4142
DEPENDENCIES
4243
concurrent-ruby (~> 1.0.0)
44+
erubis
4345
flay!
4446
json
4547
pry

lib/cc/engine/analyzers/ruby/main.rb

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require "erubis"
12
require "flay"
23
require "json"
34
require "cc/engine/analyzers/reporter"
@@ -10,12 +11,12 @@ module Ruby
1011
class Main < CC::Engine::Analyzers::Base
1112
LANGUAGE = "ruby"
1213
DEFAULT_PATHS = [
14+
"**/*.erb",
1315
"**/*.rb",
1416
"**/*.rake",
1517
"**/Rakefile",
1618
"**/Gemfile",
1719
"**/*.gemspec"
18-
1920
]
2021
DEFAULT_MASS_THRESHOLD = 18
2122
BASE_POINTS = 1_500_000
@@ -33,8 +34,45 @@ def overage(issue)
3334
end
3435

3536
def process_file(file)
36-
RubyParser.new.process(File.binread(file), file, TIMEOUT)
37+
if File.extname(file) == ".erb"
38+
process_erb file
39+
else
40+
RubyParser.new.process(File.binread(file), file, TIMEOUT)
41+
end
3742
end
43+
44+
def process_erb(file)
45+
erb = File.read(file)
46+
47+
ruby = Erubis.new(erb).src
48+
begin
49+
RubyParser.new.process(ruby, file)
50+
rescue => e
51+
warn ruby if option[:verbose]
52+
raise e
53+
end
54+
end
55+
56+
class Erubis < ::Erubis::Eruby # :nodoc:
57+
BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/
58+
59+
def add_expr_literal(src, code)
60+
if code =~ BLOCK_EXPR
61+
src << '@output_buffer.append= ' << code
62+
else
63+
src << '@output_buffer.append=(' << code << ');'
64+
end
65+
end
66+
67+
def add_expr_escaped(src, code)
68+
if code =~ BLOCK_EXPR
69+
src << "@output_buffer.safe_append= " << code
70+
else
71+
src << "@output_buffer.safe_append=(" << code << ");"
72+
end
73+
end
74+
end
75+
3876
end
3977
end
4078
end

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

+38
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,44 @@ module CC::Engine::Analyzers
5656
expect(run_engine(engine_conf)).to eq("")
5757
}.to output(/Skipping file/).to_stderr
5858
end
59+
60+
it "analyzes erb files" do
61+
create_source_file("recipe.erb", <<-EOERB)
62+
<div class="container">
63+
<h1>Select a Category</h1>
64+
<ul>
65+
<%categories.each do |category| %>
66+
<li> <a href= "/category/<%=category.id%>"><%=category.name%> | <%=category.recipes.count%> Recipes </a></li>
67+
<%end%>
68+
69+
<%categories.each do |category| %>
70+
<li> <a href= "/category/<%=category.id%>"><%=category.name%> | <%=category.recipes.count%> Recipes </a></li>
71+
<%end%>
72+
73+
<%categories.each do |category| %>
74+
<li> <a href= "/category/<%=category.id%>"><%=category.name%> | <%=category.recipes.count%> Recipes </a></li>
75+
<%end%>
76+
</ul>
77+
</div>
78+
EOERB
79+
80+
result = run_engine(engine_conf).strip
81+
json = JSON.parse(result)
82+
83+
expect(json["type"]).to eq("issue")
84+
expect(json["check_name"]).to eq("Similar code")
85+
expect(json["description"]).to eq("Similar code found in 2 other locations")
86+
expect(json["categories"]).to eq(["Duplication"])
87+
expect(json["location"]).to eq({
88+
"path" => "recipe.erb",
89+
"lines" => { "begin" => 4, "end" => 5 },
90+
})
91+
expect(json["remediation_points"]).to eq(8_700_000)
92+
expect(json["other_locations"]).to eq([
93+
{"path" => "recipe.erb", "lines" => { "begin" => 8, "end" => 9} },
94+
{"path" => "recipe.erb", "lines" => { "begin" => 12, "end" => 13} }
95+
])
96+
end
5997
end
6098

6199
describe "#calculate_points(issue)" do

0 commit comments

Comments
 (0)