Skip to content

Commit

Permalink
Tweak offense message for Style/LambdaCall
Browse files Browse the repository at this point in the history
  • Loading branch information
koic committed Feb 7, 2022
1 parent 57abcc7 commit af609e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
32 changes: 12 additions & 20 deletions lib/rubocop/cop/style/lambda_call.rb
Expand Up @@ -22,45 +22,37 @@ class LambdaCall < Base
include ConfigurableEnforcedStyle
extend AutoCorrector

MSG = 'Prefer the use of `%<prefer>s` over `%<current>s`.'
RESTRICT_ON_SEND = %i[call].freeze

def on_send(node)
return unless node.receiver

if offense?(node)
add_offense(node) do |corrector|
prefer = prefer(node)
current = node.source

add_offense(node, message: format(MSG, prefer: prefer, current: current)) do |corrector|
opposite_style_detected
autocorrect(corrector, node)
corrector.replace(node, prefer)
end
else
correct_style_detected
end
end

def autocorrect(corrector, node)
if explicit_style?
receiver = node.receiver.source
replacement = node.source.sub("#{receiver}.", "#{receiver}.call")

corrector.replace(node, replacement)
else
add_parentheses(node, corrector) unless node.parenthesized?
corrector.remove(node.loc.selector)
end
end

private

def offense?(node)
(explicit_style? && node.implicit_call?) || (implicit_style? && !node.implicit_call?)
end

def message(_node)
if explicit_style?
'Prefer the use of `lambda.call(...)` over `lambda.(...)`.'
else
'Prefer the use of `lambda.(...)` over `lambda.call(...)`.'
end
def prefer(node)
receiver = node.receiver.source
arguments = node.arguments.map(&:source).join(', ')
method = explicit_style? ? "call(#{arguments})" : "(#{arguments})"

"#{receiver}.#{method}"
end

def implicit_style?
Expand Down
20 changes: 10 additions & 10 deletions spec/rubocop/cop/style/lambda_call_spec.rb
Expand Up @@ -7,7 +7,7 @@
it 'registers an offense for x.()' do
expect_offense(<<~RUBY)
x.(a, b)
^^^^^^^^ Prefer the use of `lambda.call(...)` over `lambda.(...)`.
^^^^^^^^ Prefer the use of `x.call(a, b)` over `x.(a, b)`.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -19,7 +19,7 @@
expect_offense(<<~RUBY)
x.call(a, b)
x.(a, b)
^^^^^^^^ Prefer the use of `lambda.call(...)` over `lambda.(...)`.
^^^^^^^^ Prefer the use of `x.call(a, b)` over `x.(a, b)`.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -32,9 +32,9 @@
expect_offense(<<~RUBY)
x.call(a, b)
x.(a, b)
^^^^^^^^ Prefer the use of `lambda.call(...)` over `lambda.(...)`.
^^^^^^^^ Prefer the use of `x.call(a, b)` over `x.(a, b)`.
x.(a, b)
^^^^^^^^ Prefer the use of `lambda.call(...)` over `lambda.(...)`.
^^^^^^^^ Prefer the use of `x.call(a, b)` over `x.(a, b)`.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -51,7 +51,7 @@
it 'registers an offense for x.call()' do
expect_offense(<<~RUBY)
x.call(a, b)
^^^^^^^^^^^^ Prefer the use of `lambda.(...)` over `lambda.call(...)`.
^^^^^^^^^^^^ Prefer the use of `x.(a, b)` over `x.call(a, b)`.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -62,7 +62,7 @@
it 'registers an offense for opposite + correct' do
expect_offense(<<~RUBY)
x.call(a, b)
^^^^^^^^^^^^ Prefer the use of `lambda.(...)` over `lambda.call(...)`.
^^^^^^^^^^^^ Prefer the use of `x.(a, b)` over `x.call(a, b)`.
x.(a, b)
RUBY

Expand All @@ -75,10 +75,10 @@
it 'registers an offense for correct + multiple opposite styles' do
expect_offense(<<~RUBY)
x.call(a, b)
^^^^^^^^^^^^ Prefer the use of `lambda.(...)` over `lambda.call(...)`.
^^^^^^^^^^^^ Prefer the use of `x.(a, b)` over `x.call(a, b)`.
x.(a, b)
x.call(a, b)
^^^^^^^^^^^^ Prefer the use of `lambda.(...)` over `lambda.call(...)`.
^^^^^^^^^^^^ Prefer the use of `x.(a, b)` over `x.call(a, b)`.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -95,7 +95,7 @@
it 'auto-corrects x.call to x.()' do
expect_offense(<<~RUBY)
a.call
^^^^^^ Prefer the use of `lambda.(...)` over `lambda.call(...)`.
^^^^^^ Prefer the use of `a.()` over `a.call`.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -106,7 +106,7 @@
it 'auto-corrects x.call asdf, x123 to x.(asdf, x123)' do
expect_offense(<<~RUBY)
a.call asdf, x123
^^^^^^^^^^^^^^^^^ Prefer the use of `lambda.(...)` over `lambda.call(...)`.
^^^^^^^^^^^^^^^^^ Prefer the use of `a.(asdf, x123)` over `a.call asdf, x123`.
RUBY

expect_correction(<<~RUBY)
Expand Down

0 comments on commit af609e5

Please sign in to comment.