Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support auto-correction for Performance/Caller #189

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [#136](https://github.com/rubocop-hq/rubocop-performance/issues/136): Add new `Performance/MethodObjectAsBlock` cop. ([@fatkodima][])
* [#151](https://github.com/rubocop-hq/rubocop-performance/issues/151): Add new `Performance/ConstantRegexp` cop. ([@fatkodima][])
* [#175](https://github.com/rubocop-hq/rubocop-performance/pull/175): Add new `Performance/ArraySemiInfiniteRangeSlice` cop. ([@fatkodima][])
* [#189](https://github.com/rubocop-hq/rubocop-performance/pull/189): Support auto-correction for `Performance/Caller`. ([@koic][])

### Changes

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Performance/Caller:
Use `caller(n..n)` instead of `caller`.
Enabled: true
VersionAdded: '0.49'
VersionChanged: '1.9'

Performance/CaseWhenSplat:
Description: >-
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ROOT/pages/cops_performance.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ end

| Enabled
| Yes
| No
| Yes
| 0.49
| -
| 1.9
|===

This cop identifies places where `caller[n]`
Expand Down
27 changes: 12 additions & 15 deletions lib/rubocop/cop/performance/caller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ module Performance
# caller_locations(2..2).first
# caller_locations(1..1).first
class Caller < Base
MSG_BRACE = 'Use `%<method>s(%<n>d..%<n>d).first`' \
' instead of `%<method>s[%<m>d]`.'
MSG_FIRST = 'Use `%<method>s(%<n>d..%<n>d).first`' \
' instead of `%<method>s.first`.'
extend AutoCorrector

MSG = 'Use `%<preferred_method>s` instead of `%<current_method>s`.'
RESTRICT_ON_SEND = %i[first []].freeze

def_node_matcher :slow_caller?, <<~PATTERN
Expand All @@ -42,26 +41,24 @@ class Caller < Base
def on_send(node)
return unless caller_with_scope_method?(node)

message = message(node)
add_offense(node, message: message)
end

private

def message(node)
method_name = node.receiver.method_name
caller_arg = node.receiver.first_argument
n = caller_arg ? int_value(caller_arg) : 1

if node.method?(:[])
m = int_value(node.first_argument)
n += m
format(MSG_BRACE, n: n, m: m, method: method_name)
else
format(MSG_FIRST, n: n, method: method_name)
end

preferred_method = "#{method_name}(#{n}..#{n}).first"

message = format(MSG, preferred_method: preferred_method, current_method: node.source)
add_offense(node, message: message) do |corrector|
corrector.replace(node, preferred_method)
end
end

private

def int_value(node)
node.children[0]
end
Expand Down
56 changes: 44 additions & 12 deletions spec/rubocop/cop/performance/caller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,99 @@
expect_no_offenses('caller_locations')
end

it 'registers an offense when :first is called on caller' do
it 'registers an offense and corrects when :first is called on caller' do
expect(caller.first).to eq(caller(1..1).first)
expect_offense(<<~RUBY)
caller.first
^^^^^^^^^^^^ Use `caller(1..1).first` instead of `caller.first`.
RUBY

expect_correction(<<~RUBY)
caller(1..1).first
RUBY
end

it 'registers an offense when :first is called on caller with 1' do
it 'registers an offense and corrects when :first is called on caller with 1' do
expect(caller(1).first).to eq(caller(1..1).first)
expect_offense(<<~RUBY)
caller(1).first
^^^^^^^^^^^^^^^ Use `caller(1..1).first` instead of `caller.first`.
^^^^^^^^^^^^^^^ Use `caller(1..1).first` instead of `caller(1).first`.
RUBY

expect_correction(<<~RUBY)
caller(1..1).first
RUBY
end

it 'registers an offense when :first is called on caller with 2' do
it 'registers an offense and corrects when :first is called on caller with 2' do
expect(caller(2).first).to eq(caller(2..2).first)
expect_offense(<<~RUBY)
caller(2).first
^^^^^^^^^^^^^^^ Use `caller(2..2).first` instead of `caller.first`.
^^^^^^^^^^^^^^^ Use `caller(2..2).first` instead of `caller(2).first`.
RUBY

expect_correction(<<~RUBY)
caller(2..2).first
RUBY
end

it 'registers an offense when :[] is called on caller' do
it 'registers an offense and corrects when :[] is called on caller' do
expect(caller[1]).to eq(caller(2..2).first)
expect_offense(<<~RUBY)
caller[1]
^^^^^^^^^ Use `caller(2..2).first` instead of `caller[1]`.
RUBY

expect_correction(<<~RUBY)
caller(2..2).first
RUBY
end

it 'registers an offense when :[] is called on caller with 1' do
it 'registers an offense and corrects when :[] is called on caller with 1' do
expect(caller(1)[1]).to eq(caller(2..2).first)
expect_offense(<<~RUBY)
caller(1)[1]
^^^^^^^^^^^^ Use `caller(2..2).first` instead of `caller[1]`.
^^^^^^^^^^^^ Use `caller(2..2).first` instead of `caller(1)[1]`.
RUBY

expect_correction(<<~RUBY)
caller(2..2).first
RUBY
end

it 'registers an offense when :[] is called on caller with 2' do
it 'registers an offense and corrects when :[] is called on caller with 2' do
expect(caller(2)[1]).to eq(caller(3..3).first)
expect_offense(<<~RUBY)
caller(2)[1]
^^^^^^^^^^^^ Use `caller(3..3).first` instead of `caller[1]`.
^^^^^^^^^^^^ Use `caller(3..3).first` instead of `caller(2)[1]`.
RUBY

expect_correction(<<~RUBY)
caller(3..3).first
RUBY
end

it 'registers an offense when :first is called on caller_locations also' do
it 'registers an offense and corrects when :first is called on caller_locations also' do
expect(caller_locations.first.to_s).to eq(caller_locations(1..1).first.to_s)
expect_offense(<<~RUBY)
caller_locations.first
^^^^^^^^^^^^^^^^^^^^^^ Use `caller_locations(1..1).first` instead of `caller_locations.first`.
RUBY

expect_correction(<<~RUBY)
caller_locations(1..1).first
RUBY
end

it 'registers an offense when :[] is called on caller_locations also' do
it 'registers an offense and corrects when :[] is called on caller_locations also' do
expect(caller_locations[1].to_s).to eq(caller_locations(2..2).first.to_s)
expect_offense(<<~RUBY)
caller_locations[1]
^^^^^^^^^^^^^^^^^^^ Use `caller_locations(2..2).first` instead of `caller_locations[1]`.
RUBY

expect_correction(<<~RUBY)
caller_locations(2..2).first
RUBY
end
end