Skip to content

Commit

Permalink
[Fix #8718] Fix undefined methods of pseudo location (#8823)
Browse files Browse the repository at this point in the history
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 <bozhidar@batsov.com>
  • Loading branch information
ybiquitous and bbatsov committed Oct 8, 2020
1 parent 2f5a210 commit 1a84099
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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][])
Expand Down
17 changes: 15 additions & 2 deletions lib/rubocop/cop/offense.rb
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions spec/rubocop/cop/offense_spec.rb
Expand Up @@ -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
16 changes: 16 additions & 0 deletions spec/rubocop/formatter/json_formatter_spec.rb
Expand Up @@ -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

0 comments on commit 1a84099

Please sign in to comment.