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

Fix check_symbol_to_proc false positives on lambda literals #74

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
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 }