Skip to content

Commit

Permalink
feat: handle inline modules
Browse files Browse the repository at this point in the history
  • Loading branch information
3v0k4 committed Dec 20, 2024
1 parent 6b52be6 commit 9368b2e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/degem/parse_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def initialize
end

def requires = @requires.to_a
def consts = paths + classes + modules
def consts = (@paths.union(@classes).union(@modules)).to_a
def paths = @paths.to_a
def classes = @classes.to_a
def modules = @modules.to_a
Expand All @@ -52,8 +52,10 @@ def visit_module_node(node)
def visit_class_node(node)
@stack.push(node)
super
@classes.add(@stack.map(&:name).join("::"))
@stack.pop
*modules, klass = node.constant_path.full_name_parts rescue [[], node.name]
@modules.add((@stack.map(&:name) + modules).join("::")) if modules.any?
@classes.add((@stack.map(&:name) + modules + [klass]).join("::"))
end

def visit_constant_path_node(node)
Expand Down
32 changes: 32 additions & 0 deletions test/test_parse_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,36 @@ class Klass < SuperKlass
assert_array %w[Module], actual.modules
end
end

def test_it_parses_module_colon_colon_class
content = <<~CONTENT
class PG::Result
end
CONTENT

with_file(content: content) do |path|
actual = Degem::ParseRuby.new.call(path)
assert_empty actual.requires
assert_array %w[PG PG::Result], actual.consts
assert_array %w[PG::Result], actual.classes
assert_array %w[PG], actual.modules
end
end

def test_it_parses_module_module_colon_colon_class
content = <<~CONTENT
module Module1
class Module2::Klass
end
end
CONTENT

with_file(content: content) do |path|
actual = Degem::ParseRuby.new.call(path)
assert_empty actual.requires
assert_array %w[Module1 Module1::Module2 Module1::Module2::Klass Module2 Module2::Klass], actual.consts
assert_array %w[Module1::Module2::Klass], actual.classes
assert_array %w[Module1 Module1::Module2], actual.modules
end
end
end

0 comments on commit 9368b2e

Please sign in to comment.