Skip to content

Commit 1a84099

Browse files
ybiquitousbbatsov
andauthored
[Fix #8718] Fix undefined methods of pseudo location (#8823)
This change aims to fix the error due to undefined methods of `RuboCop::Cop::Offence::PseudoSourceRange`. The `RuboCop::Cop::Offence::PseudoSourceRange` class emulates the `Parser::Source::Range` class in the `parser` gem. (see <https://github.com/whitequark/parser/blob/v2.7.1.5/lib/parser/source/range.rb>) The `RuboCop::Cop::Offence#location` attribute can have an instance of both these classes. So, these classes need to have the same interface and semantics for the usage in RuboCop. Co-authored-by: Bozhidar Batsov <[email protected]>
1 parent 2f5a210 commit 1a84099

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [#8801](https://github.com/rubocop-hq/rubocop/issues/8801): Fix `Layout/SpaceAroundEqualsInParameterDefault` only registered once in a line. ([@rdunlop][])
1919
* [#8514](https://github.com/rubocop-hq/rubocop/issues/8514): Correct multiple `Style/MethodDefParentheses` per file. ([@rdunlop][])
2020
* [#8825](https://github.com/rubocop-hq/rubocop/issues/8825): Fix crash in `Style/ExplicitBlockArgument` when code is called outside of a method. ([@ghiculescu][])
21+
* [#8718](https://github.com/rubocop-hq/rubocop/issues/8718): Fix undefined methods of pseudo location. ([@ybiquitous][])
2122
* [#8354](https://github.com/rubocop-hq/rubocop/issues/8354): Detect regexp named captures in `Style/CaseLikeIf` cop. ([@dsavochkin][])
2223
* [#8834](https://github.com/rubocop-hq/rubocop/issues/8834): Fix a false positive for `Style/ParenthesesAsGroupedExpression` when method argument parentheses are omitted and hash argument key is enclosed in parentheses. ([@koic][])
2324
* [#8830](https://github.com/rubocop-hq/rubocop/issues/8830): Fix bad autocorrect of `Style/StringConcatenation` when string includes double quotes. ([@tleish][])

lib/rubocop/cop/offense.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,23 @@ class Offense
6363
attr_reader :corrector
6464

6565
PseudoSourceRange = Struct.new(:line, :column, :source_line, :begin_pos,
66-
:end_pos)
66+
:end_pos) do
67+
alias_method :first_line, :line
68+
alias_method :last_line, :line
69+
alias_method :last_column, :column
70+
71+
def column_range
72+
column...last_column
73+
end
74+
75+
def size
76+
end_pos - begin_pos
77+
end
78+
alias_method :length, :size
79+
end
6780
private_constant :PseudoSourceRange
6881

69-
NO_LOCATION = PseudoSourceRange.new(1, 0, '', 0, 1).freeze
82+
NO_LOCATION = PseudoSourceRange.new(1, 0, '', 0, 0).freeze
7083

7184
# @api private
7285
def initialize(severity, location, message, cop_name, # rubocop:disable Metrics/ParameterLists

spec/rubocop/cop/offense_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,49 @@ def Foo
180180
expect(offense.highlighted_area.source).to eq('Foo')
181181
end
182182
end
183+
184+
context 'when the location is pseudo' do
185+
let(:location) { described_class::NO_LOCATION }
186+
187+
it 'returns a location with valid size and length' do
188+
expect(offense.location.size).to eq 0
189+
expect(offense.location.length).to eq 0
190+
end
191+
192+
it 'returns a line' do
193+
expect(offense.line).to eq 1
194+
end
195+
196+
it 'returns a column' do
197+
expect(offense.column).to eq 0
198+
end
199+
200+
it 'returns a source line' do
201+
expect(offense.source_line).to eq ''
202+
end
203+
204+
it 'returns a column length' do
205+
expect(offense.column_length).to eq 0
206+
end
207+
208+
it 'returns the first line' do
209+
expect(offense.first_line).to eq 1
210+
end
211+
212+
it 'returns the last line' do
213+
expect(offense.last_line).to eq 1
214+
end
215+
216+
it 'returns the last column' do
217+
expect(offense.last_column).to eq 0
218+
end
219+
220+
it 'returns a column range' do
221+
expect(offense.column_range).to eq 0...0
222+
end
223+
224+
it 'returns a real column' do
225+
expect(offense.real_column).to eq 1
226+
end
227+
end
183228
end

spec/rubocop/formatter/json_formatter_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,21 @@
157157
it 'sets length value for :length key' do
158158
expect(hash[:length]).to eq(8)
159159
end
160+
161+
context 'when the location is pseudo' do
162+
let(:location) { RuboCop::Cop::Offense::NO_LOCATION }
163+
164+
it 'returns a valid hash' do
165+
expect(hash).to eq({
166+
start_line: 1,
167+
start_column: 1,
168+
last_line: 1,
169+
last_column: 0,
170+
length: 0,
171+
line: 1,
172+
column: 1
173+
})
174+
end
175+
end
160176
end
161177
end

0 commit comments

Comments
 (0)