Skip to content

Include erb files for analysis, following Flay #81

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ engines:
enabled: true
duplication:
enabled: true
exclude_fingerprints:
- b2dc8dbd27916d8321e298f59fc2f431 # Erubis < ::Erubis::Eruby
config:
languages:
- ruby
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
source 'https://rubygems.org'

gem 'erubis'
gem 'flay', git: 'https://github.com/codeclimate/flay.git'
gem 'concurrent-ruby', "~> 1.0.0"
gem 'ruby_parser'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ GEM
coderay (1.1.0)
concurrent-ruby (1.0.0)
diff-lcs (1.2.5)
erubis (2.7.0)
json (1.8.3)
method_source (0.8.2)
pry (0.10.3)
Expand Down Expand Up @@ -40,6 +41,7 @@ PLATFORMS

DEPENDENCIES
concurrent-ruby (~> 1.0.0)
erubis
flay!
json
pry
Expand Down
35 changes: 33 additions & 2 deletions lib/cc/engine/analyzers/ruby/main.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "erubis"
require "flay"
require "json"
require "cc/engine/analyzers/reporter"
Expand All @@ -10,12 +11,12 @@ module Ruby
class Main < CC::Engine::Analyzers::Base
LANGUAGE = "ruby"
DEFAULT_PATHS = [
"**/*.erb",
"**/*.rb",
"**/*.rake",
"**/Rakefile",
"**/Gemfile",
"**/*.gemspec"

]
DEFAULT_MASS_THRESHOLD = 18
BASE_POINTS = 1_500_000
Expand All @@ -33,7 +34,37 @@ def overage(mass)
end

def process_file(file)
RubyParser.new.process(File.binread(file), file, TIMEOUT)
if File.extname(file) == ".erb"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT think about extracting all of this into a Parser class like the other languages have? Keeping that logic here made sense when it was one-liner, but I think this is complex enough that extracting makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually prefer the single file because I think the main class is fairly small (< 5 methods), and adding encapsulating parser files might be adding needless encapsulation.

Let me know if you feel strongly though - and I can make a change.

process_erb file
else
RubyParser.new.process(File.binread(file), file, TIMEOUT)
end
end

def process_erb(file)
erb = File.binread(file)
ruby = Erubis.new(erb).src
RubyParser.new.process(ruby, file)
end

class Erubis < ::Erubis::Eruby
BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/

def add_expr_literal(src, code)
if code =~ BLOCK_EXPR
src << "@output_buffer.append= " << code
else
src << "@output_buffer.append=(" << code << ");"
end
end

def add_expr_escaped(src, code)
if code =~ BLOCK_EXPR
src << "@output_buffer.safe_append= " << code
else
src << "@output_buffer.safe_append=(" << code << ");"
end
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/cc/engine/analyzers/javascript/main_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'cc/engine/analyzers/reporter'
require 'cc/engine/analyzers/engine_config'
require 'cc/engine/analyzers/file_list'
require 'erubis'

RSpec.describe CC::Engine::Analyzers::Javascript::Main, in_tmpdir: true do
include AnalyzerSpecHelpers
Expand Down
39 changes: 39 additions & 0 deletions spec/cc/engine/analyzers/ruby/main_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,45 @@ module CC::Engine::Analyzers
expect(run_engine(engine_conf)).to eq("")
}.to output(/Skipping file/).to_stderr
end

it "analyzes erb files" do
create_source_file("recipe.erb", <<-EOERB)
<div class="container">
<h1>Select a Category</h1>
<ul>
<%categories.each do |category| %>
<li> <a href= "/category/<%=category.id%>"><%=category.name%> | <%=category.recipes.count%> Recipes </a></li>
<%end%>

<%categories.each do |category| %>
<li> <a href= "/category/<%=category.id%>"><%=category.name%> | <%=category.recipes.count%> Recipes </a></li>
<%end%>

<%categories.each do |category| %>
<li> <a href= "/category/<%=category.id%>"><%=category.name%> | <%=category.recipes.count%> Recipes </a></li>
<%end%>
</ul>
</div>
EOERB

issues = run_engine(engine_conf).strip.split("\0")
result = issues.first.strip
json = JSON.parse(result)

expect(json["type"]).to eq("issue")
expect(json["check_name"]).to eq("Similar code")
expect(json["description"]).to eq("Similar code found in 2 other locations (mass = 30)")
expect(json["categories"]).to eq(["Duplication"])
expect(json["location"]).to eq({
"path" => "recipe.erb",
"lines" => { "begin" => 4, "end" => 5 },
})
expect(json["remediation_points"]).to eq(2_700_000)
expect(json["other_locations"]).to eq([
{"path" => "recipe.erb", "lines" => { "begin" => 8, "end" => 9} },
{"path" => "recipe.erb", "lines" => { "begin" => 12, "end" => 13} }
])
end
end

describe "#calculate_points(mass)" do
Expand Down