Skip to content
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

Index all missing global variable nodes #2699

Merged
merged 1 commit into from
Oct 10, 2024
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
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
Loading