Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make class, module, and struct definitions aware of numblock #205

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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