Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add support for lambda and block locals
Co-authored-by: Kevin Newton <kddnewton@users.noreply.github.com>
  • Loading branch information
vinistock and kddnewton committed Mar 2, 2023
commit 039c0874e08316e3f9a9c80f7838177ccad1cd6c
9 changes: 9 additions & 0 deletions lib/syntax_tree/with_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ def visit_blockarg(node)
super
end

def visit_block_var(node)
node.locals.each do |local|
current_scope.add_local_definition(local, :variable)
end

super
end
alias visit_lambda_var visit_block_var

# Visit for keeping track of local variable definitions
def visit_var_field(node)
value = node.value
Expand Down
21 changes: 21 additions & 0 deletions test/with_scope_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,27 @@ def test_double_nested_arguments
assert_argument(collector, "four", definitions: [1], usages: [5])
end

def test_block_locals
collector = Collector.collect(<<~RUBY)
[].each do |; a|
end
RUBY

assert_equal(1, collector.variables.length)

assert_variable(collector, "a", definitions: [1])
end

def test_lambda_locals
collector = Collector.collect(<<~RUBY)
->(;a) { }
RUBY

assert_equal(1, collector.variables.length)

assert_variable(collector, "a", definitions: [1])
end

def test_regex_named_capture_groups
collector = Collector.collect(<<~RUBY)
if /(?<one>\\w+)-(?<two>\\w+)/ =~ "something-else"
Expand Down