diff --git a/CHANGELOG.md b/CHANGELOG.md index b6d378a5af3..652f1ebc227 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * [#8810](https://github.com/rubocop-hq/rubocop/pull/8810): Fix multiple offense detection for `Style/RaiseArgs`. ([@pbernays][]) * [#8809](https://github.com/rubocop-hq/rubocop/pull/8809): Fix multiple offense detection for `Style/For`. ([@pbernays][]) * [#8801](https://github.com/rubocop-hq/rubocop/issues/8801): Fix `Layout/SpaceAroundEqualsInParameterDefault` only registered once in a line. ([@rdunlop][]) +* [#8718](https://github.com/rubocop-hq/rubocop/issues/8718): Fix undefined methods of pseudo location. ([@ybiquitous][]) ### Changes diff --git a/lib/rubocop/cop/offense.rb b/lib/rubocop/cop/offense.rb index 0da21b0fe6d..bd8938b54d1 100644 --- a/lib/rubocop/cop/offense.rb +++ b/lib/rubocop/cop/offense.rb @@ -63,7 +63,20 @@ class Offense attr_reader :corrector PseudoSourceRange = Struct.new(:line, :column, :source_line, :begin_pos, - :end_pos) + :end_pos) do + alias_method :first_line, :line + alias_method :last_line, :line + alias_method :last_column, :column + + def column_range + column...last_column + end + + def size + end_pos - begin_pos + end + alias_method :length, :size + end private_constant :PseudoSourceRange NO_LOCATION = PseudoSourceRange.new(1, 0, '', 0, 1).freeze diff --git a/spec/rubocop/cop/offense_spec.rb b/spec/rubocop/cop/offense_spec.rb index d1fb624b0cd..293a4c491a8 100644 --- a/spec/rubocop/cop/offense_spec.rb +++ b/spec/rubocop/cop/offense_spec.rb @@ -180,4 +180,49 @@ def Foo expect(offense.highlighted_area.source).to eq('Foo') end end + + context 'when the location is pseudo' do + let(:location) { described_class::NO_LOCATION } + + it 'returns a location with valid size and length' do + expect(offense.location.size).to eq 1 + expect(offense.location.length).to eq 1 + end + + it 'returns a line' do + expect(offense.line).to eq 1 + end + + it 'returns a column' do + expect(offense.column).to eq 0 + end + + it 'returns a source line' do + expect(offense.source_line).to eq '' + end + + it 'returns a column length' do + expect(offense.column_length).to eq 0 + end + + it 'returns the first line' do + expect(offense.first_line).to eq 1 + end + + it 'returns the last line' do + expect(offense.last_line).to eq 1 + end + + it 'returns the last column' do + expect(offense.last_column).to eq 0 + end + + it 'returns a column range' do + expect(offense.column_range).to eq 0...0 + end + + it 'returns a real column' do + expect(offense.real_column).to eq 1 + end + end end diff --git a/spec/rubocop/formatter/json_formatter_spec.rb b/spec/rubocop/formatter/json_formatter_spec.rb index 6668b1bcfc1..c70ea332869 100644 --- a/spec/rubocop/formatter/json_formatter_spec.rb +++ b/spec/rubocop/formatter/json_formatter_spec.rb @@ -157,5 +157,21 @@ it 'sets length value for :length key' do expect(hash[:length]).to eq(8) end + + context 'when the location is pseudo' do + let(:location) { RuboCop::Cop::Offense::NO_LOCATION } + + it 'returns a valid hash' do + expect(hash).to eq({ + start_line: 1, + start_column: 1, + last_line: 1, + last_column: 0, + length: 1, + line: 1, + column: 1 + }) + end + end end end