Skip to content

Commit

Permalink
Make class, module, and struct definitions aware of numblock
Browse files Browse the repository at this point in the history
This PR makes class, module, and struct definitions aware of numblock.
  • Loading branch information
koic authored and marcandre committed Aug 24, 2021
1 parent 3800682 commit ab83fd0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
@@ -0,0 +1 @@
* [#205](https://github.com/rubocop/rubocop-ast/pull/205): Make class, module, and struct definitions aware of numblock. ([@koic][])
6 changes: 3 additions & 3 deletions lib/rubocop/ast/node.rb
Expand Up @@ -507,20 +507,20 @@ def guard_clause?
# @deprecated Use `:class_constructor?`
# @!method struct_constructor?(node = self)
def_node_matcher :struct_constructor?, <<~PATTERN
(block (send #global_const?(:Struct) :new ...) _ $_)
({block numblock} (send #global_const?(:Struct) :new ...) _ $_)
PATTERN

# @!method class_definition?(node = self)
def_node_matcher :class_definition?, <<~PATTERN
{(class _ _ $_)
(sclass _ $_)
(block (send #global_const?({:Struct :Class}) :new ...) _ $_)}
({block numblock} (send #global_const?({:Struct :Class}) :new ...) _ $_)}
PATTERN

# @!method module_definition?(node = self)
def_node_matcher :module_definition?, <<~PATTERN
{(module _ $_)
(block (send #global_const?(:Module) :new ...) _ $_)}
({block numblock} (send #global_const?(:Module) :new ...) _ $_)}
PATTERN

# Some expressions are evaluated for their value, some for their side
Expand Down
45 changes: 45 additions & 0 deletions spec/rubocop/ast/node_spec.rb
Expand Up @@ -490,6 +490,21 @@ def details; end
class_node = node.children.last
expect(class_node.class_definition?).to eq(class_node.body)
end

context 'when using numbered parameter', :ruby27 do
let(:src) do
<<~RUBY
Person = Struct.new(:name, :age) do
do_something _1
end
RUBY
end

it 'matches' do
class_node = node.children.last
expect(class_node.class_definition?).to eq(class_node.body)
end
end
end

context 'constant defined as Struct without block' do
Expand All @@ -514,6 +529,21 @@ def details; end
class_node = node.children.last
expect(class_node.class_definition?).to eq(class_node.body)
end

context 'when using numbered parameter', :ruby27 do
let(:src) do
<<~RUBY
Person = Class.new do
do_something _1
end
RUBY
end

it 'matches' do
class_node = node.children.last
expect(class_node.class_definition?).to eq(class_node.body)
end
end
end

context 'namespaced class' do
Expand Down Expand Up @@ -593,6 +623,21 @@ def details; end
module_node = node.children.last
expect(module_node.module_definition?).to eq(module_node.body)
end

context 'when using numbered parameter', :ruby27 do
let(:src) do
<<~RUBY
Person = Module.new do
do_something _1
end
RUBY
end

it 'matches' do
module_node = node.children.last
expect(module_node.module_definition?).to eq(module_node.body)
end
end
end

context 'prepend Module.new' do
Expand Down

0 comments on commit ab83fd0

Please sign in to comment.