Skip to content

Commit

Permalink
Treat for loops as closures (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
castwide committed Aug 11, 2020
1 parent fec9d59 commit d03e95d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/solargraph/parser/rubyvm/node_processors.rb
Expand Up @@ -54,6 +54,7 @@ module NodeProcessor
register :KW_ARG, Rubyvm::NodeProcessors::KwArgNode
register :ITER, Rubyvm::NodeProcessors::BlockNode
register :LAMBDA, Rubyvm::NodeProcessors::BlockNode
register :FOR, Rubyvm::NodeProcessors::BlockNode
register :OP_ASGN_OR, Rubyvm::NodeProcessors::OrasgnNode
register :LIT, Rubyvm::NodeProcessors::LitNode
end
Expand Down
44 changes: 34 additions & 10 deletions lib/solargraph/parser/rubyvm/node_processors/args_node.rb
Expand Up @@ -7,16 +7,30 @@ module NodeProcessors
class ArgsNode < Parser::NodeProcessor::Base
def process
if region.closure.is_a?(Pin::BaseMethod) || region.closure.is_a?(Pin::Block)
node.children[0].times do |i|
locals.push Solargraph::Pin::Parameter.new(
location: region.closure.location,
closure: region.closure,
comments: comments_for(node),
name: region.lvars[i].to_s,
presence: region.closure.location.range,
decl: :arg
)
region.closure.parameters.push locals.last
if region.lvars[0].nil?
node.children[0].times do |i|
locals.push Solargraph::Pin::Parameter.new(
location: region.closure.location,
closure: region.closure,
comments: comments_for(node),
name: extract_name(node.children[i + 1]),
presence: region.closure.location.range,
decl: :arg
)
region.closure.parameters.push locals.last
end
else
node.children[0].times do |i|
locals.push Solargraph::Pin::Parameter.new(
location: region.closure.location,
closure: region.closure,
comments: comments_for(node),
name: region.lvars[i].to_s,
presence: region.closure.location.range,
decl: :arg
)
region.closure.parameters.push locals.last
end
end
# @todo Optional args, keyword args, etc.
if node.children[6]
Expand Down Expand Up @@ -55,6 +69,16 @@ def process
end
process_children
end

private

def extract_name var
if Parser.is_ast_node?(var)
var.children[0].to_s
else
var.to_s
end
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/source_map/mapper_spec.rb
Expand Up @@ -1457,4 +1457,16 @@ def bar; end
bar = map.first_pin('Foo#bar')
expect(bar).to be_explicit
end

it 'separates parameters from local variables' do
map = Solargraph::SourceMap.load_string(%(
def foo(bar)
for i in (bar.length - 1).downto(0) do
puts bar[i]
end
end
))
pin = map.first_pin('#foo')
expect(pin.parameters.length).to eq(1)
end
end

0 comments on commit d03e95d

Please sign in to comment.