Skip to content

Commit cc9139b

Browse files
committed
Handle alternate location class
1 parent f117330 commit cc9139b

File tree

5 files changed

+61
-10
lines changed

5 files changed

+61
-10
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ gem "rubocop", require: false
44
gem "pry", require: false
55

66
group :test do
7+
gem "mocha"
78
gem "rake"
89
gem "minitest"
910
gem "minitest-reporters"

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ GEM
77
parser (>= 2.2.0.pre.3, < 3.0)
88
builder (3.2.2)
99
coderay (1.1.0)
10+
metaclass (0.0.4)
1011
method_source (0.8.2)
1112
minitest (5.6.0)
1213
minitest-reporters (1.0.11)
1314
ansi
1415
builder
1516
minitest (>= 5.0)
1617
ruby-progressbar
18+
mocha (1.1.0)
19+
metaclass (~> 0.0.1)
1720
parser (2.2.2.5)
1821
ast (>= 1.1, < 3.0)
1922
powerpack (0.1.1)
@@ -38,6 +41,7 @@ PLATFORMS
3841
DEPENDENCIES
3942
minitest
4043
minitest-reporters
44+
mocha
4145
pry
4246
rake
4347
rubocop

lib/cc/engine/rubocop.rb

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,31 @@ def rubocop_team_for_path(path)
6464
RuboCop::Cop::Team.new(RuboCop::Cop::Cop.all, rubocop_config)
6565
end
6666

67+
def violation_positions(location)
68+
if location.is_a? RuboCop::Cop::Lint::Syntax::PseudoSourceRange
69+
first_line = location.line
70+
last_line = location.line
71+
first_column = location.column
72+
last_column = location.column
73+
else
74+
first_line = location.first_line
75+
last_line = location.last_line
76+
first_column = location.column
77+
last_column = location.last_column
78+
end
79+
80+
{
81+
begin: {
82+
column: first_column + 1, # columns are 0-based in Parser
83+
line: first_line,
84+
},
85+
end: {
86+
column: last_column + 1,
87+
line: last_line,
88+
}
89+
}
90+
end
91+
6792
def violation_json(violation, local_path)
6893
{
6994
type: "Issue",
@@ -73,16 +98,7 @@ def violation_json(violation, local_path)
7398
remediation_points: 50_000,
7499
location: {
75100
path: local_path,
76-
positions: {
77-
begin: {
78-
column: violation.location.column + 1, # columns are 0-based in Parser
79-
line: violation.location.first_line,
80-
},
81-
end: {
82-
column: violation.location.last_column + 1,
83-
line: violation.location.last_line,
84-
}
85-
},
101+
positions: violation_positions(violation.location),
86102
},
87103
}.to_json
88104
end

spec/cc/engine/rubocop_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,35 @@ def method
112112
assert !includes_check?(output, "Lint/UselessAssignment")
113113
end
114114

115+
it "handles different locations properly" do
116+
RuboCop::Cop::Team.any_instance.expects(:inspect_file).returns([OpenStruct.new(
117+
location: RuboCop::Cop::Lint::Syntax::PseudoSourceRange.new(1, 0, ''),
118+
cop_name: "fake",
119+
message: "message"
120+
)])
121+
create_source_file("my_script.rb", <<-EORUBY)
122+
#!/usr/bin/env ruby
123+
124+
def method
125+
unused = "x"
126+
127+
return false
128+
end
129+
EORUBY
130+
output = run_engine
131+
json = output.split("\u0000")
132+
133+
result = JSON.parse(json.first)
134+
location = {
135+
"path" => "my_script.rb",
136+
"positions" => {
137+
"begin" => { "column"=>1, "line"=>1 },
138+
"end" => { "column"=>1, "line"=>1 }
139+
}
140+
}
141+
assert_equal location, result["location"]
142+
end
143+
115144
def includes_check?(output, cop_name)
116145
issues = output.split("\0").map { |x| JSON.parse(x) }
117146

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'minitest/spec'
22
require 'minitest/autorun'
33
require "minitest/reporters"
4+
require "mocha/mini_test"
45
Minitest::Reporters.use! Minitest::Reporters::DefaultReporter.new

0 commit comments

Comments
 (0)