Skip to content

Commit

Permalink
Process non-self singleton classes (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun authored Dec 8, 2022
1 parent ccacd43 commit a2dbacb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/solargraph/parser/rubyvm/node_processors/sclass_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ module Rubyvm
module NodeProcessors
class SclassNode < Parser::NodeProcessor::Base
def process
# @todo Temporarily skipping remote metaclasses
return unless node.children[0].is_a?(RubyVM::AbstractSyntaxTree::Node) && node.children[0].type == :SELF
sclass = node.children[0]
if sclass.is_a?(RubyVM::AbstractSyntaxTree::Node) && sclass.type == :SELF
closure = region.closure
elsif sclass.is_a?(RubyVM::AbstractSyntaxTree::Node) && %i[CDECL CONST].include?(sclass.type)
names = [region.closure.namespace, region.closure.name]
if names.last != sclass.children[0].to_s
names << sclass.children[0].to_s
end
name = names.reject(&:empty?).join('::')
closure = Solargraph::Pin::Namespace.new(name: name, location: region.closure.location)
else
return
end
pins.push Solargraph::Pin::Singleton.new(
location: get_node_location(node),
closure: region.closure
closure: closure
)
process_children region.update(visibility: :public, scope: :class, closure: pins.last)
end
Expand Down
38 changes: 38 additions & 0 deletions spec/api_map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,44 @@ class << self
expect(pins.map(&:name)).to include('foo')
end

it 'finds class methods in class << Example' do
source = Solargraph::Source.load_string(%(
class << Example = Class.new
def foo; end
end
class Example
class << Example
def bar; end
end
end
))
@api_map.map source
pins = @api_map.get_methods('Example', scope: :class).select do |pin|
pin.namespace == 'Example'
end
expect(pins.map(&:name).sort).to eq(['bar', 'foo'])
end

it 'finds class methods in nested class << Example' do
source = Solargraph::Source.load_string(%(
module Container
class << Example = Class.new
def foo; end
end
class Example
class << Example
def bar; end
end
end
end
))
@api_map.map source
pins = @api_map.get_methods('Container::Example', scope: :class).select do |pin|
pin.namespace == 'Container::Example'
end
expect(pins.map(&:name).sort).to eq(['bar', 'foo'])
end

it 'resolves aliases for YARD methods' do
dir = File.absolute_path(File.join('spec', 'fixtures', 'yard_map'))
yard_pins = Dir.chdir dir do
Expand Down

0 comments on commit a2dbacb

Please sign in to comment.