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

Update #check_fetch_offense method #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions lib/fasterer/method_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class MethodCall

alias_method :name, :method_name

# Defines CONSTANT_TYPES constant with the list of constant
# types like String, Nil, Integer, Symbol, Boolean or a Contant
PRIMITIVE_DATA_TYPES = %i[nil lit str const true false].freeze

def initialize(element)
@element = element
set_call_element
Expand All @@ -24,6 +28,10 @@ def has_block?
@block_present || false
end

def has_block_with_primitive_data_types?
has_block? && PRIMITIVE_DATA_TYPES.include?(@block_body.first)
end

def receiver_element
call_element[1]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fasterer/offense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def explanation
'Enumerable#sort is slower than Enumerable#sort_by',

fetch_with_argument_vs_block:
'Hash#fetch with second argument is slower than Hash#fetch with block',
'Hash#fetch with block of primitive data types like: String, Integer, Symbol, Boolean or Nil is slower than Hash#fetch with second argument',

keys_each_vs_each_key:
'Hash#keys.each is slower than Hash#each_key. N.B. Hash#each_key cannot be used if the hash is modified during the each block',
Expand Down
2 changes: 1 addition & 1 deletion lib/fasterer/scanners/method_call_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def check_flatten_offense
end

def check_fetch_offense
if method_call.arguments.count == 2 && !method_call.has_block?
if method_call.arguments.count == 1 && method_call.has_block_with_primitive_data_types?
add_offense(:fetch_with_argument_vs_block)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
it 'should detect keys fetch with argument once' do
analyzer = Fasterer::Analyzer.new(test_file_path)
analyzer.scan
expect(analyzer.errors[:fetch_with_argument_vs_block].count).to eq(1)
expect(analyzer.errors[:fetch_with_argument_vs_block].count).to eq(4)
end
end
16 changes: 14 additions & 2 deletions spec/support/analyzer/14_fetch_with_argument_vs_block.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
HASH = { :writing => :fast_ruby }

def slow
HASH.fetch(:writing, [*1..100])
HASH.fetch(:writing) { nil }
end

def slow
HASH.fetch(:writing) { 1 }
end

def slow
HASH.fetch(:writing) { "1" }
end

def slow
HASH.fetch(:writing) { true }
end

def fast
HASH.fetch(:writing) { [*1..100] }
HASH.fetch(:writng, 1)
end

HASH.fetch(:writing)