Skip to content

Commit

Permalink
Index all missing global variable nodes (#2699)
Browse files Browse the repository at this point in the history
feat: handle all kind of global variable nodes in indexer
  • Loading branch information
snutij authored Oct 10, 2024
1 parent 4fd4be9 commit b6cfb22
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
55 changes: 55 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def initialize(index, dispatcher, parse_result, file_path, collect_comments: fal
:on_constant_or_write_node_enter,
:on_constant_and_write_node_enter,
:on_constant_operator_write_node_enter,
:on_global_variable_and_write_node_enter,
:on_global_variable_operator_write_node_enter,
:on_global_variable_or_write_node_enter,
:on_global_variable_target_node_enter,
:on_global_variable_write_node_enter,
:on_instance_variable_write_node_enter,
:on_instance_variable_and_write_node_enter,
:on_instance_variable_operator_write_node_enter,
Expand Down Expand Up @@ -382,6 +387,31 @@ def on_def_node_leave(node)
end
end

sig { params(node: Prism::GlobalVariableAndWriteNode).void }
def on_global_variable_and_write_node_enter(node)
handle_global_variable(node, node.name_loc)
end

sig { params(node: Prism::GlobalVariableOperatorWriteNode).void }
def on_global_variable_operator_write_node_enter(node)
handle_global_variable(node, node.name_loc)
end

sig { params(node: Prism::GlobalVariableOrWriteNode).void }
def on_global_variable_or_write_node_enter(node)
handle_global_variable(node, node.name_loc)
end

sig { params(node: Prism::GlobalVariableTargetNode).void }
def on_global_variable_target_node_enter(node)
handle_global_variable(node, node.location)
end

sig { params(node: Prism::GlobalVariableWriteNode).void }
def on_global_variable_write_node_enter(node)
handle_global_variable(node, node.name_loc)
end

sig { params(node: Prism::InstanceVariableWriteNode).void }
def on_instance_variable_write_node_enter(node)
handle_instance_variable(node, node.name_loc)
Expand Down Expand Up @@ -426,6 +456,31 @@ def on_alias_method_node_enter(node)

private

sig do
params(
node: T.any(
Prism::GlobalVariableAndWriteNode,
Prism::GlobalVariableOperatorWriteNode,
Prism::GlobalVariableOrWriteNode,
Prism::GlobalVariableTargetNode,
Prism::GlobalVariableWriteNode,
),
loc: Prism::Location,
).void
end
def handle_global_variable(node, loc)
name = node.name.to_s
comments = collect_comments(node)

@index.add(Entry::GlobalVariable.new(
name,
@file_path,
loc,
comments,
@index.configuration.encoding,
))
end

sig do
params(
node: T.any(
Expand Down
49 changes: 49 additions & 0 deletions lib/ruby_indexer/test/global_variable_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# typed: true
# frozen_string_literal: true

require_relative "test_case"

module RubyIndexer
class GlobalVariableTest < TestCase
def test_global_variable_and_write
index(<<~RUBY)
$foo &&= 1
RUBY

assert_entry("$foo", Entry::GlobalVariable, "/fake/path/foo.rb:0-0:0-4")
end

def test_global_variable_operator_write
index(<<~RUBY)
$foo += 1
RUBY

assert_entry("$foo", Entry::GlobalVariable, "/fake/path/foo.rb:0-0:0-4")
end

def test_global_variable_or_write
index(<<~RUBY)
$foo ||= 1
RUBY

assert_entry("$foo", Entry::GlobalVariable, "/fake/path/foo.rb:0-0:0-4")
end

def test_global_variable_target_node
index(<<~RUBY)
$foo, $bar = 1
RUBY

assert_entry("$foo", Entry::GlobalVariable, "/fake/path/foo.rb:0-0:0-4")
assert_entry("$bar", Entry::GlobalVariable, "/fake/path/foo.rb:0-6:0-10")
end

def test_global_variable_write
index(<<~RUBY)
$foo = 1
RUBY

assert_entry("$foo", Entry::GlobalVariable, "/fake/path/foo.rb:0-0:0-4")
end
end
end

0 comments on commit b6cfb22

Please sign in to comment.