Skip to content
Draft
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
2 changes: 0 additions & 2 deletions lib/solargraph/parser/flow_sensitive_typing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,6 @@ def parse_isa isa_node
# @param variable_name [String]
# @param position [Position]
#
# @sg-ignore Solargraph::Parser::FlowSensitiveTyping#find_var
# return type could not be inferred
# @return [Solargraph::Pin::LocalVariable, Solargraph::Pin::InstanceVariable, nil]
def find_var variable_name, position
if variable_name.start_with?('@')
Expand Down
5 changes: 4 additions & 1 deletion lib/solargraph/parser/parser_gem/node_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,14 @@ def reduce_to_value_nodes nodes
result.concat reduce_to_value_nodes(node.children)
# @sg-ignore flow sensitive typing needs to narrow down type with an if is_a? check
elsif node.type == :block
# the block itself is a first class value that could be returned
result.push node
# a return inside the block returns from the enclosing method,
# so those values are possible here as well
# @sg-ignore flow sensitive typing needs to narrow down type with an if is_a? check
result.concat explicit_return_values_from_compound_statement(node.children[2])
# @sg-ignore flow sensitive typing needs to narrow down type with an if is_a? check
elsif node.type == :resbody
# @sg-ignore flow sensitive typing needs to narrow down type with an if is_a? check
result.concat reduce_to_value_nodes([node.children[2]])
else
result.push node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ def process
else
pins.find { |iv| iv.location == location && iv.is_a?(Pin::BaseVariable) }
end
# @todo in line below, nothing in typechecking alerts
# when a non-existant method is called on 'l'
if pin.nil?
Solargraph.logger.debug do
"Could not find local for masgn= value in location #{location.inspect} in #{lhs_arr} - masgn = #{masgn}, lhs.type = #{lhs.type}"
end
next
end
# @sg-ignore flow sensitive typing needs to infer Enumerable#find's block return type from an is_a? check
pin.mass_assignment = [mass_rhs, i]
end
end
Expand Down
31 changes: 31 additions & 0 deletions spec/parser/node_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,37 @@ def parse source
expect(rets.map(&:type)).to eq(%i[block lvar])
end

it 'collects a block value after an explicit return' do
node = parse(%(
return array.map { |item| item }
))
rets = described_class.returns_from_method_body(node)
expect(rets.map(&:type)).to eq(%i[block])
end

it 'collects a block value in a conditional branch' do
node = parse(%(
if foo
array.map { |item| item }
else
1
end
))
rets = described_class.returns_from_method_body(node)
expect(rets.map(&:type)).to eq(%i[block int])
end

it 'collects both a returned block value and returns inside it' do
node = parse(%(
return array.map { |item|
return item if foo
item
}
))
rets = described_class.returns_from_method_body(node)
expect(rets.map(&:type)).to eq(%i[block lvar])
end

it 'finds correct return node line in begin expressions' do
node = parse(%(
begin
Expand Down
Loading