Skip to content

Commit

Permalink
Fix #macro? for numblock nodes
Browse files Browse the repository at this point in the history
We can have macros in numblocks. I found this issue while working on
broader `numblock` nodes support for cops in rubocop and found out that
the`#bare_access_modifier?` method was not giving proper results for
modifiers in numblocks. This applies to other methods that depend on the
`#macro?` methods.
  • Loading branch information
gsamokovarov committed Aug 5, 2022
1 parent 5acfb16 commit 3ba094c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#237](https://github.com/rubocop/rubocop-ast/pull/237) Fix `#macro?` for numblock nodes ([@gsamokovarov][])

## 1.19.1 (2022-07-10)

### New features
Expand Down Expand Up @@ -343,3 +347,4 @@
[@eugeneius]: https://github.com/eugeneius
[@wcmonty]: https://github.com/wcmonty
[@zverok]: https://github.com/zverok
[@gsamokovarov]: https://github.com/gsamokovarov
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node/mixin/method_dispatch_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def binary_operation?
^{ # or the parent is...
sclass class module class_constructor? # a class-like node
[ { # or some "wrapper"
kwbegin begin block
kwbegin begin block numblock
(if _condition <%0 _>) # note: we're excluding the condition of `if` nodes
}
#in_macro_scope? # that is itself in a macro scope
Expand Down
40 changes: 40 additions & 0 deletions spec/rubocop/ast/send_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,33 @@ module Foo

it { expect(send_node).not_to be_bare_access_modifier }
end

context 'Ruby 2.7', :ruby27 do
context 'when node is access modifier in block' do
let(:source) do
<<~RUBY
included do
>> module_function <<
end
RUBY
end

it { expect(send_node).to be_bare_access_modifier }
end

context 'when node is access modifier in numblock' do
let(:source) do
<<~RUBY
included do
_1
>> module_function <<
end
RUBY
end

it { expect(send_node).to be_bare_access_modifier }
end
end
end

describe '#non_bare_access_modifier?' do
Expand Down Expand Up @@ -275,6 +302,19 @@ module Foo
it { expect(send_node).to be_macro }
end

context 'Ruby 2.7', :ruby27 do
context 'when parent is a numblock in a macro scope' do
let(:source) do
['concern :Auth do',
'>>bar :baz<<',
' bar _1',
'end'].join("\n")
end

it { expect(send_node).to be_macro }
end
end

context 'when parent is a block not in a macro scope' do
let(:source) { <<~RUBY }
class Foo
Expand Down

0 comments on commit 3ba094c

Please sign in to comment.