From f9ff82b5ddf60f635565b9cc7d4417ff31396a97 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Wed, 15 Sep 2021 12:13:55 -0400 Subject: [PATCH] [Fix #208] Update `MethodDispatchNode#block_literal?` to return true for `numblock`s. --- changelog/fix_fix_208_update.md | 1 + .../ast/node/mixin/method_dispatch_node.rb | 6 +++--- spec/rubocop/ast/send_node_spec.rb | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 changelog/fix_fix_208_update.md diff --git a/changelog/fix_fix_208_update.md b/changelog/fix_fix_208_update.md new file mode 100644 index 000000000..09f5dba2e --- /dev/null +++ b/changelog/fix_fix_208_update.md @@ -0,0 +1 @@ +* [#208](https://github.com/rubocop-hq/rubocop-ast/issues/208): Update `MethodDispatchNode#block_literal?` to return true for `numblock`s. ([@dvandersluis][]) diff --git a/lib/rubocop/ast/node/mixin/method_dispatch_node.rb b/lib/rubocop/ast/node/mixin/method_dispatch_node.rb index bb5b9b2cd..aeb770d4a 100644 --- a/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +++ b/lib/rubocop/ast/node/mixin/method_dispatch_node.rb @@ -28,9 +28,9 @@ def method_name node_parts[1] end - # The `block` node associated with this method dispatch, if any. + # The `block` or `numblock` node associated with this method dispatch, if any. # - # @return [BlockNode, nil] the `block` node associated with this method + # @return [BlockNode, nil] the `block` or `numblock` node associated with this method # call or `nil` def block_node parent if block_literal? @@ -154,7 +154,7 @@ def implicit_call? # # @return [Boolean] whether the dispatched method has a block def block_literal? - parent&.block_type? && eql?(parent.send_node) + (parent&.block_type? || parent&.numblock_type?) && eql?(parent.send_node) end # Checks whether this node is an arithmetic operation diff --git a/spec/rubocop/ast/send_node_spec.rb b/spec/rubocop/ast/send_node_spec.rb index 871777f86..5317ff210 100644 --- a/spec/rubocop/ast/send_node_spec.rb +++ b/spec/rubocop/ast/send_node_spec.rb @@ -1034,6 +1034,14 @@ def bar it { expect(send_node).not_to be_block_literal } end + + context 'with Ruby >= 2.7', :ruby27 do + context 'with a numblock literal' do + let(:source) { '>> foo.bar << { baz(_1) }' } + + it { expect(send_node).to be_block_literal } + end + end end describe '#arithmetic_operation?' do @@ -1074,6 +1082,14 @@ def bar it { expect(send_node.block_node).to be_nil } end + + context 'with Ruby >= 2.7', :ruby27 do + context 'with a numblock literal' do + let(:source) { '>>foo.bar<< { baz(_1) }' } + + it { expect(send_node.block_node).to be_numblock_type } + end + end end describe '#splat_argument?' do