From 57dd39246028b70ef493d8898675b92b19e01390 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 8 Aug 2023 01:00:02 +0900 Subject: [PATCH] Fix an error for `Style/LambdaCall` This PR fixes an error for `Style/LambdaCall` when using nested lambda call `x.().()`: ```console $ cat example.rb x.(a, b).(c) $ buncle exec rubocop example.rb --only Style/LambdaCall -a -d (snip) An error occurred while Style/LambdaCall cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/lambda_call/example.rb:1:0. Parser::Source::TreeRewriter detected clobbering /Users/koic/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/parser-3.2.2.3/lib/parser/source/tree_rewriter.rb:427:in `trigger_policy' ``` --- changelog/fix_an_error_for_style_lambda_call.md | 1 + lib/rubocop/cop/style/lambda_call.rb | 5 +++++ spec/rubocop/cop/style/lambda_call_spec.rb | 12 ++++++++++++ 3 files changed, 18 insertions(+) create mode 100644 changelog/fix_an_error_for_style_lambda_call.md diff --git a/changelog/fix_an_error_for_style_lambda_call.md b/changelog/fix_an_error_for_style_lambda_call.md new file mode 100644 index 000000000000..c5c354f59a70 --- /dev/null +++ b/changelog/fix_an_error_for_style_lambda_call.md @@ -0,0 +1 @@ +* [#12102](https://github.com/rubocop/rubocop/pull/12102): Fix an error for `Style/LambdaCall` when using nested lambda call `x.().()`. ([@koic][]) diff --git a/lib/rubocop/cop/style/lambda_call.rb b/lib/rubocop/cop/style/lambda_call.rb index 81abf811c943..1b2a8cafcb1b 100644 --- a/lib/rubocop/cop/style/lambda_call.rb +++ b/lib/rubocop/cop/style/lambda_call.rb @@ -20,6 +20,7 @@ module Style # lambda.(x, y) class LambdaCall < Base include ConfigurableEnforcedStyle + include IgnoredNode extend AutoCorrector MSG = 'Prefer the use of `%s` over `%s`.' @@ -33,8 +34,12 @@ def on_send(node) current = node.source add_offense(node, message: format(MSG, prefer: prefer, current: current)) do |corrector| + next if part_of_ignored_node?(node) + opposite_style_detected corrector.replace(node, prefer) + + ignore_node(node) end else correct_style_detected diff --git a/spec/rubocop/cop/style/lambda_call_spec.rb b/spec/rubocop/cop/style/lambda_call_spec.rb index 65404ec1b4eb..1a2c0d089675 100644 --- a/spec/rubocop/cop/style/lambda_call_spec.rb +++ b/spec/rubocop/cop/style/lambda_call_spec.rb @@ -15,6 +15,18 @@ RUBY end + it 'registers an offense for x.().()' do + expect_offense(<<~RUBY) + x.(a, b).(c) + ^^^^^^^^^^^^ Prefer the use of `x.(a, b).call(c)` over `x.(a, b).(c)`. + ^^^^^^^^ Prefer the use of `x.call(a, b)` over `x.(a, b)`. + RUBY + + expect_correction(<<~RUBY) + x.(a, b).call(c) + RUBY + end + it 'registers an offense for correct + opposite' do expect_offense(<<~RUBY) x.call(a, b)