Skip to content

Commit 88552d7

Browse files
committed
Merge pull request #21 from andyw8/add-rubocop-config-and-corrections
Add RuboCop configuration and basic style corrections
2 parents 38c342e + c02670e commit 88552d7

File tree

7 files changed

+53
-22
lines changed

7 files changed

+53
-22
lines changed

.rubocop.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Metrics/ModuleLength:
2+
Exclude:
3+
- 'spec/**/*'
4+
Style/ClassAndModuleChildren:
5+
Exclude:
6+
- 'spec/**/*'
7+
Style/Documentation:
8+
Enabled: false
9+
Style/FileName:
10+
Exclude:
11+
- 'bin/codeclimate-rubocop'
12+
Style/PercentLiteralDelimiters:
13+
PreferredDelimiters:
14+
'%w': '[]'
15+
'%W': '[]'
16+
Style/StringLiterals:
17+
Enabled: false
18+
Style/TrailingComma:
19+
Enabled: false

Rakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ require 'rake/testtask'
22

33
Rake::TestTask.new do |t|
44
t.test_files = Dir.glob('spec/**/*_spec.rb')
5-
t.libs = ["lib", "spec"]
5+
t.libs = %w[lib spec]
66
end
77

88
task(default: :test)

bin/codeclimate-rubocop

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!/usr/bin/env ruby
2-
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), "../lib")))
2+
$LOAD_PATH.unshift(
3+
File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
4+
)
35

46
require 'cc/engine/rubocop'
57

6-
if File.exists?("/config.json")
8+
if File.exist?("/config.json")
79
engine_config = JSON.parse(File.read("/config.json"))
810
else
911
engine_config = {}

lib/cc/engine/file_list_resolver.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def include_based_files_to_inspect
4040

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

4646
def rubocop_file_to_include?(file)

lib/cc/engine/rubocop.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def inspect_file(path)
4949

5050
def local_path(path)
5151
realpath = Pathname.new(@code).realpath.to_s
52-
path.gsub(%r|^#{realpath}/|, '')
52+
path.gsub(%r{^#{realpath}/}, '')
5353
end
5454

5555
def rubocop_config_store
@@ -83,7 +83,7 @@ def violation_positions(location)
8383
end
8484

8585
{
86-
begin: {
86+
begin: {
8787
column: first_column + 1, # columns are 0-based in Parser
8888
line: first_line,
8989
},
@@ -112,7 +112,9 @@ def violation_json(violation, local_path)
112112
end
113113

114114
def content_body(cop_name)
115-
path = File.expand_path("../../../../config/contents/#{cop_name.underscore}.md", __FILE__)
115+
path = File.expand_path(
116+
"../../../../config/contents/#{cop_name.underscore}.md", __FILE__
117+
)
116118
File.exist?(path) ? File.read(path) : nil
117119
end
118120
end

lib/rubocop/cop/method_complexity_patch.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# original AST is no longer available. So it's easier to monkey-path
99
# this method on complexity checkers to send the location of the entire
1010
# method to the created `Offense`.
11-
def add_offense(node, loc, message = nil, severity = nil)
11+
def add_offense(node, _loc, message = nil, severity = nil)
1212
super(node, node.loc, message, severity)
1313
end
1414
end

spec/cc/engine/rubocop_spec.rb

+22-14
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,17 @@ def method
133133
end
134134

135135
it "handles different locations properly" do
136-
RuboCop::Cop::Team.any_instance.expects(:inspect_file).returns([OpenStruct.new(
137-
location: RuboCop::Cop::Lint::Syntax::PseudoSourceRange.new(1, 0, ""),
138-
cop_name: "fake",
139-
message: "message"
140-
)])
136+
RuboCop::Cop::Team.any_instance.expects(:inspect_file).returns(
137+
[
138+
OpenStruct.new(
139+
location: RuboCop::Cop::Lint::Syntax::PseudoSourceRange.new(
140+
1, 0, ""
141+
),
142+
cop_name: "fake",
143+
message: "message"
144+
)
145+
]
146+
)
141147
create_source_file("my_script.rb", <<-EORUBY)
142148
#!/usr/bin/env ruby
143149
@@ -154,8 +160,8 @@ def method
154160
location = {
155161
"path" => "my_script.rb",
156162
"positions" => {
157-
"begin" => { "column"=>1, "line"=>1 },
158-
"end" => { "column"=>1, "line"=>1 }
163+
"begin" => { "column" => 1, "line" => 1 },
164+
"end" => { "column" => 1, "line" => 1 }
159165
}
160166
}
161167
assert_equal location, result["location"]
@@ -190,12 +196,14 @@ def method(a,b,c,d,e,f,g)
190196

191197
json = JSON.parse('[' + output.split("\u0000").join(',') + ']')
192198

193-
result = json.select { |i| i && i["check_name"] =~ /Metrics\/CyclomaticComplexity/ }.first
199+
result = json.find do |i|
200+
i && i["check_name"] =~ %r{Metrics\/CyclomaticComplexity}
201+
end
194202
location = {
195203
"path" => "my_script",
196204
"positions" => {
197-
"begin" => { "column"=>11, "line"=>3 },
198-
"end" => { "column"=>14, "line"=>21 }
205+
"begin" => { "column" => 11, "line" => 3 },
206+
"end" => { "column" => 14, "line" => 21 }
199207
}
200208
}
201209
assert_equal location, result["location"]
@@ -233,7 +241,7 @@ def method
233241
end
234242
EORUBY
235243
output = run_engine(
236-
"include_paths" => %w(included_root_file.rb subdir/)
244+
"include_paths" => %w[included_root_file.rb subdir/]
237245
)
238246
assert !includes_check?(output, "Lint/UselessAssignment")
239247
assert !includes_check?(output, "Style/AndOr")
@@ -245,7 +253,7 @@ def method
245253
output = run_engine(
246254
"include_paths" => %w[config.yml]
247255
)
248-
refute (issues(output).detect do |i|
256+
refute(issues(output).detect do |i|
249257
i["description"] == "unexpected token tCOLON"
250258
end)
251259
end
@@ -258,12 +266,12 @@ def method
258266
return false
259267
end
260268
EORUBY
261-
output = run_engine("include_paths" => %w(Rakefile))
269+
output = run_engine("include_paths" => %w[Rakefile])
262270
assert includes_check?(output, "Lint/UselessAssignment")
263271
end
264272

265273
def includes_check?(output, cop_name)
266-
!!issues(output).detect { |i| i["check_name"] =~ /#{cop_name}$/ }
274+
issues(output).any? { |i| i["check_name"] =~ /#{cop_name}$/ }
267275
end
268276

269277
def includes_content_for?(output, cop_name)

0 commit comments

Comments
 (0)