From af609e5d07f51661269b72f8561c85e4fbe66478 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 7 Feb 2022 22:35:46 +0900 Subject: [PATCH] Tweak offense message for `Style/LambdaCall` --- lib/rubocop/cop/style/lambda_call.rb | 32 ++++++++-------------- spec/rubocop/cop/style/lambda_call_spec.rb | 20 +++++++------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/lib/rubocop/cop/style/lambda_call.rb b/lib/rubocop/cop/style/lambda_call.rb index 6023272aeff..0f0a6adef64 100644 --- a/lib/rubocop/cop/style/lambda_call.rb +++ b/lib/rubocop/cop/style/lambda_call.rb @@ -22,45 +22,37 @@ class LambdaCall < Base include ConfigurableEnforcedStyle extend AutoCorrector + MSG = 'Prefer the use of `%s` over `%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? diff --git a/spec/rubocop/cop/style/lambda_call_spec.rb b/spec/rubocop/cop/style/lambda_call_spec.rb index 7b1c3343125..0cab807196e 100644 --- a/spec/rubocop/cop/style/lambda_call_spec.rb +++ b/spec/rubocop/cop/style/lambda_call_spec.rb @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 @@ -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) @@ -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) @@ -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)