Skip to content

Commit fcfe222

Browse files
committed
Merge pull request #48 from codeclimate/jp/upgrade-rubocop
Bump rubocop to 0.37.2
2 parents 3c6f4ea + 88974fc commit fcfe222

15 files changed

+410
-282
lines changed

Gemfile

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
source 'https://rubygems.org'
22

33
gem "activesupport", require: false
4-
gem "rubocop", require: false
4+
gem "rubocop", "~> 0.37.2", require: false
55
gem "rubocop-rspec", require: false
66
gem "safe_yaml"
77
gem "pry", require: false
88

99
group :test do
10-
gem "mocha"
1110
gem "rake"
12-
gem "minitest"
13-
gem "minitest-reporters"
11+
gem "rspec"
1412
end

Gemfile.lock

+31-29
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,67 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
activesupport (4.2.4)
4+
activesupport (4.2.5.1)
55
i18n (~> 0.7)
66
json (~> 1.7, >= 1.7.7)
77
minitest (~> 5.1)
88
thread_safe (~> 0.3, >= 0.3.4)
99
tzinfo (~> 1.1)
10-
ansi (1.5.0)
1110
ast (2.2.0)
12-
astrolabe (1.3.1)
13-
parser (~> 2.2)
14-
builder (3.2.2)
1511
coderay (1.1.0)
12+
diff-lcs (1.2.5)
1613
i18n (0.7.0)
1714
json (1.8.3)
18-
metaclass (0.0.4)
1915
method_source (0.8.2)
20-
minitest (5.8.0)
21-
minitest-reporters (1.0.20)
22-
ansi
23-
builder
24-
minitest (>= 5.0)
25-
ruby-progressbar
26-
mocha (1.1.0)
27-
metaclass (~> 0.0.1)
28-
parser (2.2.3.0)
29-
ast (>= 1.1, < 3.0)
16+
minitest (5.8.4)
17+
parser (2.3.0.4)
18+
ast (~> 2.2)
3019
powerpack (0.1.1)
31-
pry (0.10.1)
20+
pry (0.10.3)
3221
coderay (~> 1.1.0)
3322
method_source (~> 0.8.1)
3423
slop (~> 3.4)
35-
rainbow (2.0.0)
36-
rake (10.4.2)
37-
rubocop (0.35.1)
38-
astrolabe (~> 1.3)
39-
parser (>= 2.2.3.0, < 3.0)
24+
rainbow (2.1.0)
25+
rake (10.5.0)
26+
rspec (3.3.0)
27+
rspec-core (~> 3.3.0)
28+
rspec-expectations (~> 3.3.0)
29+
rspec-mocks (~> 3.3.0)
30+
rspec-core (3.3.1)
31+
rspec-support (~> 3.3.0)
32+
rspec-expectations (3.3.0)
33+
diff-lcs (>= 1.2.0, < 2.0)
34+
rspec-support (~> 3.3.0)
35+
rspec-mocks (3.3.1)
36+
diff-lcs (>= 1.2.0, < 2.0)
37+
rspec-support (~> 3.3.0)
38+
rspec-support (3.3.0)
39+
rubocop (0.37.2)
40+
parser (>= 2.3.0.4, < 3.0)
4041
powerpack (~> 0.1)
4142
rainbow (>= 1.99.1, < 3.0)
4243
ruby-progressbar (~> 1.7)
43-
tins (<= 1.6.0)
44-
rubocop-rspec (1.3.0)
44+
unicode-display_width (~> 0.3)
45+
rubocop-rspec (1.3.1)
4546
ruby-progressbar (1.7.5)
4647
safe_yaml (1.0.4)
4748
slop (3.6.0)
4849
thread_safe (0.3.5)
49-
tins (1.6.0)
5050
tzinfo (1.2.2)
5151
thread_safe (~> 0.1)
52+
unicode-display_width (0.3.1)
5253

5354
PLATFORMS
5455
ruby
5556

5657
DEPENDENCIES
5758
activesupport
58-
minitest
59-
minitest-reporters
60-
mocha
6159
pry
6260
rake
63-
rubocop
61+
rspec
62+
rubocop (~> 0.37.2)
6463
rubocop-rspec
6564
safe_yaml
65+
66+
BUNDLED WITH
67+
1.11.2

Rakefile

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
require 'rake/testtask'
1+
require "rspec/core/rake_task"
22

3-
Rake.add_rakelib 'lib/tasks'
4-
5-
Rake::TestTask.new do |t|
6-
t.test_files = Dir.glob('spec/**/*_spec.rb')
7-
t.libs = %w[lib spec]
8-
end
9-
10-
task(default: :test)
3+
Rake.add_rakelib "lib/tasks"
4+
RSpec::Core::RakeTask.new(:spec)
5+
task default: :spec

config/cops.yml

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
Metrics/AbcSize:
22
base_points: 1_000_000
3-
violation_points: 70_000
3+
overage_points: 70_000
44
Metrics/BlockNesting:
55
base_points: 100_000
6+
overage_points: 50_000
67
Metrics/ClassLength:
78
base_points: 5_000_000
8-
violation_points: 35_000
9-
Metrics/CyclomaticComplexity: # This check is per method
9+
overage_points: 35_000
10+
Metrics/CyclomaticComplexity:
1011
base_points: 1_000_000
11-
violation_points: 70_000
12+
overage_points: 70_000
1213
Metrics/LineLength:
1314
base_points: 50_000
15+
overage_points: 0
1416
Metrics/MethodLength:
1517
base_points: 1_000_000
16-
violation_points: 70_000
18+
overage_points: 70_000
1719
Metrics/ModuleLength:
1820
base_points: 5_000_000
19-
violation_points: 35_000
21+
overage_points: 35_000
2022
Metrics/ParameterList:
2123
base_points: 500_000
22-
violation_points: 100_000
24+
overage_points: 100_000
2325
Metrics/PerceivedComplexity:
2426
base_points: 1_000_000
25-
violation_points: 70_000
27+
overage_points: 70_000

lib/cc/engine/file_list_resolver.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module CC
22
module Engine
33
class FileListResolver
4-
def initialize(code:, engine_config: {}, rubocop_config_store:)
5-
@code = code
4+
def initialize(root:, engine_config: {}, config_store:)
5+
@root = root
66
@exclude_paths = engine_config["exclude_paths"] || []
77
@include_paths = engine_config["include_paths"]
8-
@rubocop_config_store = rubocop_config_store
8+
@config_store = config_store
99
end
1010

1111
def expanded_list
@@ -39,21 +39,21 @@ def include_based_files_to_inspect
3939
end
4040

4141
def local_path(path)
42-
realpath = Pathname.new(@code).realpath.to_s
42+
realpath = Pathname.new(@root).realpath.to_s
4343
path.gsub(%r{^#{realpath}/}, '')
4444
end
4545

4646
def rubocop_file_to_include?(file)
4747
if file =~ /\.rb$/
4848
true
4949
else
50-
dir, basename = File.split(file)
51-
@rubocop_config_store.for(dir).file_to_include?(basename)
50+
root, basename = File.split(file)
51+
@config_store.for(root).file_to_include?(basename)
5252
end
5353
end
5454

5555
def rubocop_runner
56-
@rubocop_runner ||= RuboCop::Runner.new({}, @rubocop_config_store)
56+
@rubocop_runner ||= RuboCop::Runner.new({}, @config_store)
5757
end
5858
end
5959
end

lib/cc/engine/issue.rb

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
require 'safe_yaml'
2+
SafeYAML::OPTIONS[:default_mode] = :safe
3+
4+
module CC
5+
module Engine
6+
class Issue < SimpleDelegator
7+
MULTIPLIER_REGEX = %r{\[([\d\.]+)\/([\d\.]+)\]}
8+
DEFAULT_REMEDIATION_POINTS = 50_000
9+
DEFAULT_BASE_POINTS = 200_000
10+
DEFAULT_OVERAGE_POINTS = 50_000
11+
12+
def initialize(issue, path, cop_list: nil)
13+
@path = path
14+
@cop_list = cop_list
15+
16+
super(issue)
17+
end
18+
19+
# rubocop:disable Metrics/MethodLength
20+
def to_json
21+
hash = {
22+
type: "Issue",
23+
check_name: "Rubocop/#{cop_name}",
24+
description: message,
25+
categories: [category],
26+
remediation_points: remediation_points,
27+
location: {
28+
path: path,
29+
positions: positions,
30+
},
31+
}
32+
hash[:content] = { body: content_body } if content_body.present?
33+
hash.to_json
34+
end
35+
36+
def remediation_points
37+
if multiplier?
38+
base_points + overage_points
39+
else
40+
cop_definition.fetch("remediation_points", DEFAULT_REMEDIATION_POINTS)
41+
end
42+
end
43+
44+
private
45+
46+
attr_reader :path
47+
48+
def multiplier?
49+
message.match(MULTIPLIER_REGEX)
50+
end
51+
52+
def base_points
53+
cop_definition.fetch("base_points", DEFAULT_BASE_POINTS)
54+
end
55+
56+
def cop_definition
57+
@cop_definition ||= cop_list.fetch(cop_name, {})
58+
end
59+
60+
def cop_list
61+
@cop_list ||= YAML.load_file(expand_config_path("cops.yml"))
62+
end
63+
64+
def expand_config_path(path)
65+
File.expand_path("../../../../config/#{path}", __FILE__)
66+
end
67+
68+
def overage_points
69+
overage_points = cop_definition.
70+
fetch("overage_points", DEFAULT_OVERAGE_POINTS)
71+
72+
overage_points * multiplier
73+
end
74+
75+
def multiplier
76+
result = message.scan(MULTIPLIER_REGEX)
77+
score, threshold = result[0]
78+
score.to_i - threshold.to_i
79+
end
80+
81+
def category
82+
CategoryParser.new(cop_name).category
83+
end
84+
85+
def positions
86+
{
87+
begin: {
88+
column: columns.first,
89+
line: lines.first,
90+
},
91+
end: {
92+
column: columns.last,
93+
line: lines.last,
94+
}
95+
}
96+
end
97+
98+
# Increments column values as columns are 0-based in parser
99+
def columns
100+
return @columns if defined?(@columns)
101+
102+
end_column = location.try(:last_column) || location.column
103+
@columns = [location.column + 1, end_column + 1]
104+
end
105+
106+
def lines
107+
return @lines if defined?(@lines)
108+
109+
begin_line = location.try(:first_line) || location.line
110+
end_line = location.try(:last_line) || location.line
111+
@lines = [begin_line, end_line]
112+
end
113+
114+
def content_body
115+
return @content_body if defined?(@content_body)
116+
117+
content_path = expand_config_path("contents/#{cop_name.underscore}.md")
118+
@content_body = File.exist?(content_path) && File.read(content_path)
119+
end
120+
end
121+
end
122+
end

0 commit comments

Comments
 (0)