Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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 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
Next Next commit
Add support for regexp locals
Co-authored-by: Kevin Newton <kddnewton@users.noreply.github.com>
  • Loading branch information
vinistock and kddnewton committed Mar 2, 2023
commit ac63bef6bd59dbe93632b5acff5acc57bca6db26
57 changes: 57 additions & 0 deletions lib/syntax_tree/with_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,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
38 changes: 38 additions & 0 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 @@ -349,6 +356,37 @@ def test_double_nested_arguments
assert_argument(collector, "four", definitions: [1], usages: [5])
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