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 Node#class_constructor? aware of Data.define and numbered parameters #255

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#255](https://github.com/rubocop/rubocop-ast/pull/255): Make `Node#class_constructor?` aware of Ruby 3.2's `Data.define`. ([@koic][])
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#255](https://github.com/rubocop/rubocop-ast/pull/255): Make `Node#class_construcor?` aware of Ruby 2.7's numbered parameters. ([@koic][])
10 changes: 8 additions & 2 deletions lib/rubocop/ast/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,14 @@ def guard_clause?

# @!method class_constructor?(node = self)
def_node_matcher :class_constructor?, <<~PATTERN
{ (send #global_const?({:Class :Module :Struct}) :new ...)
(block (send #global_const?({:Class :Module :Struct}) :new ...) ...)}
{
(send #global_const?({:Class :Module :Struct}) :new ...)
(send #global_const?(:Data) :define ...)
({block numblock} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, but makes me wonder if we shouldn't have a pseudo type to include both of these... Maybe any_block or a_block instead of {block numblock}

(send #global_const?({:Class :Module :Struct}) :new ...)
(send #global_const?(:Data) :define ...)
} ...)
}
PATTERN

# @deprecated Use `:class_constructor?`
Expand Down
60 changes: 60 additions & 0 deletions spec/rubocop/ast/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,66 @@ def used?
expect(node).to be_class_constructor
end
end

context 'using Ruby >= 2.7', :ruby27 do
context 'class definition with a numblock' do
let(:src) { 'Class.new { do_something(_1) }' }

it 'matches' do
expect(node).to be_class_constructor
end
end

context 'module definition with a numblock' do
let(:src) { 'Module.new { do_something(_1) }' }

it 'matches' do
expect(node).to be_class_constructor
end
end

context 'Struct definition with a numblock' do
let(:src) { 'Struct.new(:foo, :bar) { do_something(_1) }' }

it 'matches' do
expect(node).to be_class_constructor
end
end
end

context 'using Ruby >= 3.2', :ruby32 do
context 'Data definition with a block' do
let(:src) { 'Data.define(:foo, :bar) { def a = 42 }' }

it 'matches' do
expect(node).to be_class_constructor
end
end

context 'Data definition with a numblock' do
let(:src) { 'Data.define(:foo, :bar) { do_something(_1) }' }

it 'matches' do
expect(node).to be_class_constructor
end
end

context 'Data definition without block' do
let(:src) { 'Data.define(:foo, :bar)' }

it 'matches' do
expect(node).to be_class_constructor
end
end

context '::Data' do
let(:src) { '::Data.define(:foo, :bar) { def a = 42 }' }

it 'matches' do
expect(node).to be_class_constructor
end
end
end
end

describe '#struct_constructor?' do
Expand Down