Skip to content

Commit

Permalink
Merge pull request #74 from kiyot/check_symbol_to_proc_false_positive
Browse files Browse the repository at this point in the history
Fix check_symbol_to_proc false positives on lambda literals
  • Loading branch information
DamirSvrtan authored Nov 25, 2019
2 parents d8ed51e + d572421 commit 5d71ba6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/fasterer/method_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def arguments_element
call_element[3..-1] || []
end

def lambda_literal?
call_element.sexp_type == :lambda
end

private

attr_reader :call_element
Expand Down
1 change: 1 addition & 0 deletions lib/fasterer/scanners/method_call_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def check_symbol_to_proc
return if method_call.block_body.nil?
return unless method_call.block_body.sexp_type == :call
return if method_call.arguments.count > 0
return if method_call.lambda_literal?

body_method_call = MethodCall.new(method_call.block_body)

Expand Down
4 changes: 2 additions & 2 deletions spec/lib/fasterer/analyzer/18_block_vs_symbol_to_proc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
describe Fasterer::Analyzer do
let(:test_file_path) { RSpec.root.join('support', 'analyzer', '18_block_vs_symbol_to_proc.rb') }

it 'should block that could be called with symbol 7 times' do
it 'should block that could be called with symbol 9 times' do
analyzer = Fasterer::Analyzer.new(test_file_path)
analyzer.scan
expect(analyzer.errors[:block_vs_symbol_to_proc].count).to eq(7)
expect(analyzer.errors[:block_vs_symbol_to_proc].count).to eq(9)
end
end
32 changes: 32 additions & 0 deletions spec/lib/fasterer/method_call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,36 @@
# expect(method_call.receiver.name).to eq('hi')
end
end

describe '#lambda_literal?' do
describe 'lambda literal without arguments' do
let(:code) { '-> {}' }

let(:call_element) { ripper }

it 'should be true' do
expect(method_call).to be_lambda_literal
end
end

describe 'lambda literal with an argument' do
let(:code) { '->(_) {}' }

let(:call_element) { ripper }

it 'should be true' do
expect(method_call).to be_lambda_literal
end
end

describe 'lambda method' do
let(:code) { 'lambda {}' }

let(:call_element) { ripper }

it 'should be false' do
expect(method_call).not_to be_lambda_literal
end
end
end
end
4 changes: 4 additions & 0 deletions spec/support/analyzer/18_block_vs_symbol_to_proc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@

instance_eval { |_| method_call_without_receiver }
instance_eval { |object| object.to_s }

proc { |rule| rule.should_use_symbol }
lambda { |rule| rule.should_use_symbol }
->(obj) { obj.cannot_use_symbol }

0 comments on commit 5d71ba6

Please sign in to comment.