Skip to content

Commit

Permalink
Tweak offense highlight range for Performance/ChainArrayAllocation
Browse files Browse the repository at this point in the history
  • Loading branch information
koic committed Feb 10, 2024
1 parent 4230dc0 commit ae0a3d4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
4 changes: 1 addition & 3 deletions lib/rubocop/cop/performance/chain_array_allocation.rb
Expand Up @@ -19,8 +19,6 @@ module Performance
# array.map! { |x| x.downcase }
# array
class ChainArrayAllocation < Base
include RangeHelp

# These methods return a new array but only sometimes. They must be
# called with an argument. For example:
#
Expand Down Expand Up @@ -64,7 +62,7 @@ def on_send(node)
return if node.each_descendant(:send).any? { |descendant| descendant.method?(:lazy) }
return if node.method?(:select) && !enumerable_select_method?(node.receiver)

range = range_between(node.loc.dot.begin_pos, node.source_range.end_pos)
range = node.loc.selector.begin.join(node.source_range.end)

add_offense(range, message: format(MSG, method: fm, second_method: sm))
end
Expand Down
12 changes: 6 additions & 6 deletions spec/rubocop/cop/performance/chain_array_allocation_spec.rb
Expand Up @@ -12,22 +12,22 @@
it 'registers an offense for `first(10).uniq`' do
expect_offense(<<~RUBY)
[1, 2, 3, 4].first(10).uniq
^^^^^ Use unchained `first` and `uniq!` (followed by `return array` if required) instead of chaining `first...uniq`.
^^^^ Use unchained `first` and `uniq!` (followed by `return array` if required) instead of chaining `first...uniq`.
RUBY
end

it 'registers an offense for `first(variable).uniq`' do
expect_offense(<<~RUBY)
variable = 42
[1, 2, 3, 4].first(variable).uniq
^^^^^ Use unchained `first` and `uniq!` (followed by `return array` if required) instead of chaining `first...uniq`.
^^^^ Use unchained `first` and `uniq!` (followed by `return array` if required) instead of chaining `first...uniq`.
RUBY
end

it 'registers an offense for `first(do_something).uniq`' do
expect_offense(<<~RUBY)
[1, 2, 3, 4].first(do_something).uniq
^^^^^ Use unchained `first` and `uniq!` (followed by `return array` if required) instead of chaining `first...uniq`.
^^^^ Use unchained `first` and `uniq!` (followed by `return array` if required) instead of chaining `first...uniq`.
RUBY
end
end
Expand All @@ -41,7 +41,7 @@

expect_offense(<<~RUBY)
[1, 2, 3, 4].zip {|f| }.zip.uniq
^^^^^ Use unchained `zip` and `uniq!` (followed by `return array` if required) instead of chaining `zip...uniq`.
^^^^ Use unchained `zip` and `uniq!` (followed by `return array` if required) instead of chaining `zip...uniq`.
RUBY
end
end
Expand All @@ -66,7 +66,7 @@
it 'registers an offense' do
expect_offense(<<~RUBY)
model.select { |item| item.foo }.select { |item| item.bar }
^^^^^^^ Use unchained `select` and `select!` (followed by `return array` if required) instead of chaining `select...select`.
^^^^^^ Use unchained `select` and `select!` (followed by `return array` if required) instead of chaining `select...select`.
RUBY
end
end
Expand All @@ -75,7 +75,7 @@
it 'registers an offense' do
expect_offense(<<~RUBY)
model.select { _1.foo }.select { |item| item.bar }
^^^^^^^ Use unchained `select` and `select!` (followed by `return array` if required) instead of chaining `select...select`.
^^^^^^ Use unchained `select` and `select!` (followed by `return array` if required) instead of chaining `select...select`.
RUBY
end
end
Expand Down

0 comments on commit ae0a3d4

Please sign in to comment.