Skip to content

Commit

Permalink
Add Tempfile.create to ignored iterators list
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusdeap committed Oct 23, 2023
1 parent 79350d9 commit c1c7095
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/defaults.reek.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ detectors:
max_allowed_nesting: 1
ignore_iterators:
- tap
- Tempfile.create
NilCheck:
enabled: true
exclude: []
Expand Down
13 changes: 10 additions & 3 deletions lib/reek/smell_detectors/nested_iterators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def line
# The name of the config field that sets the names of any
# methods for which nesting should not be considered
IGNORE_ITERATORS_KEY = 'ignore_iterators'
DEFAULT_IGNORE_ITERATORS = ['tap'].freeze
DEFAULT_IGNORE_ITERATORS = ['tap', 'Tempfile.create'].freeze

def self.default_config
super.merge(
Expand Down Expand Up @@ -128,8 +128,15 @@ def max_allowed_nesting

# @quality :reek:FeatureEnvy
def ignored_iterator?(exp)
ignore_iterators.any? { |pattern| /#{pattern}/ =~ exp.call.name } ||
exp.without_block_arguments?
ignore_iterators.any? do |pattern|
call = exp.call
called_name = call.name
/#{pattern}/ =~ if call.respond_to? :receiver
"#{call.receiver&.name}.#{called_name}"
else
called_name
end
end || exp.without_block_arguments?
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/reek/smell_detectors/nested_iterators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ def alfa(bravo)
expect(src).not_to reek_of(:NestedIterators)
end

it 'does not report nested iterators for Tempfile#create' do
src = <<-RUBY
def alfa(bravo)
Tempfile.create('bravo', '.') do |charlie|
charlie.each { |delta| delta }
end
end
RUBY

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

it 'does not report method with successive iterators' do
src = <<-RUBY
def alfa
Expand Down

0 comments on commit c1c7095

Please sign in to comment.