Skip to content

Commit 039c087

Browse files
vinistockkddnewton
andcommitted
Add support for lambda and block locals
Co-authored-by: Kevin Newton <[email protected]>
1 parent ac63bef commit 039c087

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/syntax_tree/with_scope.rb

+9
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ def visit_blockarg(node)
189189
super
190190
end
191191

192+
def visit_block_var(node)
193+
node.locals.each do |local|
194+
current_scope.add_local_definition(local, :variable)
195+
end
196+
197+
super
198+
end
199+
alias visit_lambda_var visit_block_var
200+
192201
# Visit for keeping track of local variable definitions
193202
def visit_var_field(node)
194203
value = node.value

test/with_scope_test.rb

+21
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,27 @@ def test_double_nested_arguments
356356
assert_argument(collector, "four", definitions: [1], usages: [5])
357357
end
358358

359+
def test_block_locals
360+
collector = Collector.collect(<<~RUBY)
361+
[].each do |; a|
362+
end
363+
RUBY
364+
365+
assert_equal(1, collector.variables.length)
366+
367+
assert_variable(collector, "a", definitions: [1])
368+
end
369+
370+
def test_lambda_locals
371+
collector = Collector.collect(<<~RUBY)
372+
->(;a) { }
373+
RUBY
374+
375+
assert_equal(1, collector.variables.length)
376+
377+
assert_variable(collector, "a", definitions: [1])
378+
end
379+
359380
def test_regex_named_capture_groups
360381
collector = Collector.collect(<<~RUBY)
361382
if /(?<one>\\w+)-(?<two>\\w+)/ =~ "something-else"

0 commit comments

Comments
 (0)