Skip to content

Commit

Permalink
Merge pull request #81 from codeclimate/abh-erb-files
Browse files Browse the repository at this point in the history
Include erb files for analysis, following Flay
  • Loading branch information
Ashley Baldwin-Hunter committed Jan 28, 2016
2 parents 18dd32f + 620f2dd commit 3ec765e
Showing 6 changed files with 78 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@ engines:
enabled: true
duplication:
enabled: true
exclude_fingerprints:
- b2dc8dbd27916d8321e298f59fc2f431 # Erubis < ::Erubis::Eruby
config:
languages:
- ruby
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'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
@@ -40,6 +41,7 @@ PLATFORMS

DEPENDENCIES
concurrent-ruby (~> 1.0.0)
erubis
flay!
json
pry
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"
@@ -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
@@ -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"
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
1 change: 1 addition & 0 deletions spec/cc/engine/analyzers/javascript/main_spec.rb
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions spec/cc/engine/analyzers/ruby/main_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3ec765e

Please sign in to comment.