diff --git a/CHANGELOG.md b/CHANGELOG.md index 215ca686860..86ff6b72686 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ * [#8801](https://github.com/rubocop-hq/rubocop/issues/8801): Fix `Layout/SpaceAroundEqualsInParameterDefault` only registered once in a line. ([@rdunlop][]) * [#8514](https://github.com/rubocop-hq/rubocop/issues/8514): Correct multiple `Style/MethodDefParentheses` per file. ([@rdunlop][]) * [#8825](https://github.com/rubocop-hq/rubocop/issues/8825): Fix crash in `Style/ExplicitBlockArgument` when code is called outside of a method. ([@ghiculescu][]) +* [#8718](https://github.com/rubocop-hq/rubocop/issues/8718): Fix undefined methods of pseudo location. ([@ybiquitous][]) * [#8354](https://github.com/rubocop-hq/rubocop/issues/8354): Detect regexp named captures in `Style/CaseLikeIf` cop. ([@dsavochkin][]) * [#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][]) * [#8830](https://github.com/rubocop-hq/rubocop/issues/8830): Fix bad autocorrect of `Style/StringConcatenation` when string includes double quotes. ([@tleish][]) diff --git a/lib/rubocop/cop/offense.rb b/lib/rubocop/cop/offense.rb index 0da21b0fe6d..491a3627cd9 100644 --- a/lib/rubocop/cop/offense.rb +++ b/lib/rubocop/cop/offense.rb @@ -63,10 +63,23 @@ 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 + NO_LOCATION = PseudoSourceRange.new(1, 0, '', 0, 0).freeze # @api private def initialize(severity, location, message, cop_name, # rubocop:disable Metrics/ParameterLists diff --git a/spec/rubocop/cop/offense_spec.rb b/spec/rubocop/cop/offense_spec.rb index d1fb624b0cd..e20d07b9762 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 0 + expect(offense.location.length).to eq 0 + 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..18b318bd82f 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: 0, + line: 1, + column: 1 + }) + end + end end end