Skip to content

Commit

Permalink
Merge pull request #1725 from troessner/handle-data-define
Browse files Browse the repository at this point in the history
Treat Data.define as a class definition
  • Loading branch information
mvz committed Aug 9, 2023
2 parents a4187b9 + 90ce058 commit 4f72149
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 13 deletions.
27 changes: 21 additions & 6 deletions lib/reek/ast/sexp_extensions/send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ def participants
end

def module_creation_call?
object_creation_call? && module_creation_receiver?
end
return true if object_creation_call? && module_creation_receiver?
return true if data_definition_call? && data_definition_receiver?

def module_creation_receiver?
receiver &&
receiver.type == :const &&
[:Class, :Struct].include?(receiver.simple_name)
false
end

def object_creation_call?
Expand All @@ -47,6 +44,24 @@ def attribute_writer?
def attr_with_writable_flag?
name == :attr && args.any? && args.last.type == :true
end

private

def module_creation_receiver?
const_receiver? && [:Class, :Struct].include?(receiver.simple_name)
end

def data_definition_call?
name == :define
end

def data_definition_receiver?
const_receiver? && receiver.simple_name == :Data
end

def const_receiver?
receiver && receiver.type == :const
end
end

Op_AsgnNode = SendNode
Expand Down
64 changes: 58 additions & 6 deletions spec/reek/ast/sexp_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,26 +363,78 @@
expect(bare_new).not_to be_module_creation_call
end

it 'is not considered to have a module creation receiver' do
expect(bare_new).not_to be_module_creation_receiver
it 'is considered to be an object creation call' do
expect(bare_new).to be_object_creation_call
end
end

context 'when it’s ‘define’ with no parameters and no receiver' do
let(:bare_new) { sexp(:send, nil, :define) }

it 'is not considered to be a module creation call' do
expect(bare_new).not_to be_module_creation_call
end

it 'is considered to be an object creation call' do
expect(bare_new).not_to be_object_creation_call
end
end

context 'when it’s ‘new’ with receiver ‘Class‘' do
let(:bare_new) { sexp(:send, sexp(:const, nil, :Class), :new) }

it 'is considered to be a module creation call' do
expect(bare_new).to be_module_creation_call
end

it 'is considered to be an object creation call' do
expect(bare_new).to be_object_creation_call
end
end

context 'when it’s ‘new’ with receiver ‘Struct‘' do
let(:bare_new) { sexp(:send, sexp(:const, nil, :Struct), :new) }

it 'is considered to be a module creation call' do
expect(bare_new).to be_module_creation_call
end

it 'is considered to be an object creation call' do
expect(bare_new).to be_object_creation_call
end
end

context 'when it’s ‘new’ with receiver ‘Data‘' do
let(:bare_new) { sexp(:send, sexp(:const, nil, :Data), :new) }

it 'is not considered to be a module creation call' do
expect(bare_new).not_to be_module_creation_call
end

it 'is considered to be an object creation call' do
expect(bare_new).to be_object_creation_call
end
end

context 'when it’s ‘define’ with receiver ‘Data‘' do
let(:bare_new) { sexp(:send, sexp(:const, nil, :Data), :define) }

it 'is considered to be a module creation call' do
expect(bare_new).to be_module_creation_call
end

it 'is not considered to be an object creation call' do
expect(bare_new).not_to be_object_creation_call
end
end

context 'when it’s ‘new’ with a complex receiver' do
let(:node) { Reek::Source::SourceCode.from('(foo ? bar : baz).new').syntax_tree }

it 'is not considered to be a module creation call' do
expect(node).not_to be_module_creation_call
end

it 'is not considered to have a module creation receiver' do
expect(node).not_to be_module_creation_receiver
end

it 'is considered to be an object creation call' do
expect(node).to be_object_creation_call
end
Expand Down
14 changes: 13 additions & 1 deletion spec/reek/smell_detectors/module_initialize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def initialize; end
expect(src).not_to reek_of(:ModuleInitialize)
end

it 'reports nothing for a method named initialize in a nested struct' do
it 'reports nothing for a method named initialize in a nested Struct class' do
src = <<-RUBY
module Alfa
Bravo = Struct.new(:charlie) do
Expand All @@ -64,6 +64,18 @@ def initialize; end
expect(src).not_to reek_of(:ModuleInitialize)
end

it 'reports nothing for a method named initialize in a nested Data class' do
src = <<-RUBY
module Alfa
Bravo = Data.define(:charlie) do
def initialize; end
end
end
RUBY

expect(src).not_to reek_of(:ModuleInitialize)
end

it 'can be disabled via comment' do
src = <<-RUBY
# :reek:ModuleInitialize
Expand Down

0 comments on commit 4f72149

Please sign in to comment.