Skip to content

Add support for more locals in WithScope #329

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

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
66 changes: 66 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 Expand Up @@ -217,6 +226,63 @@ def visit_var_ref(node)
super
end

# When using regex named capture groups, vcalls might actually be a variable
def visit_vcall(node)
value = node.value
definition = current_scope.find_local(value.value)
current_scope.add_local_usage(value, definition.type) if definition

super
end

# Visit for capturing local variables defined in regex named capture groups
def visit_binary(node)
if node.operator == :=~
left = node.left

if left.is_a?(RegexpLiteral) && left.parts.length == 1 &&
left.parts.first.is_a?(TStringContent)
content = left.parts.first

value = content.value
location = content.location
start_line = location.start_line

Regexp
.new(value, Regexp::FIXEDENCODING)
.names
.each do |name|
offset = value.index(/\(\?<#{Regexp.escape(name)}>/)
line = start_line + value[0...offset].count("\n")

# We need to add 3 to account for these three characters
# prefixing a named capture (?<
column = location.start_column + offset + 3
if value[0...offset].include?("\n")
column =
value[0...offset].length - value[0...offset].rindex("\n") +
3 - 1
end

ident_location =
Location.new(
start_line: line,
start_char: location.start_char + offset,
start_column: column,
end_line: line,
end_char: location.start_char + offset + name.length,
end_column: column + name.length
)

identifier = Ident.new(value: name, location: ident_location)
current_scope.add_local_definition(identifier, :variable)
end
end
end

super
end

private

def add_argument_definitions(list)
Expand Down
65 changes: 60 additions & 5 deletions test/with_scope_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def visit_label(node)
arguments[[current_scope.id, value]] = node
end
end

def visit_vcall(node)
local = current_scope.find_local(node.value)
variables[[current_scope.id, value]] = local if local

super
end
end
end

Expand Down Expand Up @@ -110,11 +117,7 @@ def foo

assert_equal(2, collector.variables.length)
assert_variable(collector, "a", definitions: [2], usages: [4, 5])
assert_variable(collector, "rest", definitions: [4])

# Rest is considered a vcall by the parser instead of a var_ref
# assert_equal(1, variable_rest.usages.length)
# assert_equal(6, variable_rest.usages[0].start_line)
assert_variable(collector, "rest", definitions: [4], usages: [6])
end

if RUBY_VERSION >= "3.1"
Expand Down Expand Up @@ -349,6 +352,58 @@ 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"
one
two
end
RUBY

assert_equal(2, collector.variables.length)

assert_variable(collector, "one", definitions: [1], usages: [2])
assert_variable(collector, "two", definitions: [1], usages: [3])
end

def test_multiline_regex_named_capture_groups
collector = Collector.collect(<<~RUBY)
if %r{
(?<one>\\w+)-
(?<two>\\w+)
} =~ "something-else"
one
two
end
RUBY

assert_equal(2, collector.variables.length)

assert_variable(collector, "one", definitions: [2], usages: [5])
assert_variable(collector, "two", definitions: [3], usages: [6])
end

class Resolver < Visitor
prepend WithScope

Expand Down