Class: YARD::AppendixPlugin::NamespaceResolver

Inherits:
Object
  • Object
show all
Includes:
LegacyResolver
Defined in:
lib/yard-appendix/namespace_resolver.rb

Overview

This class is a necessary hack for locating the namespaces in which comments that are not tied to any CodeObjects were defined.

The issue has been reported as a bug in Ripper and until it is solved, this is required to work around it.

Defined Under Namespace

Modules: LegacyResolver

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LegacyResolver

included

Constructor Details

#initializeNamespaceResolver

Returns a new instance of NamespaceResolver.



34
35
36
37
38
# File 'lib/yard-appendix/namespace_resolver.rb', line 34

def initialize()
  @namespaces ||= {}

  super()
end

Instance Attribute Details

#namespacesObject (readonly)

Returns the value of attribute namespaces.



32
33
34
# File 'lib/yard-appendix/namespace_resolver.rb', line 32

def namespaces
  @namespaces
end

Instance Method Details

#namespace_for(statement, ns) ⇒ CodeObjects::NamespaceObject

Attempts to locate the enclosing namespace for the given statement. This is useful only for comment statements which are parsed by Ripper in a strange non-linear fashion that does not correlate with the namespace they’re defined in.

Parameters:

  • statement (YARD::Parser::Ruby::AstNode)

    the enclosed CommentNode (or any other type, really)

  • ns (YARD::CodeObjects::NamespaceObject)

    the handler’s current namespace

Returns:

  • (CodeObjects::NamespaceObject)

    the enclosing namespace object



78
79
80
# File 'lib/yard-appendix/namespace_resolver.rb', line 78

def namespace_for(statement, ns)
  YARD::Registry.at(locate_enclosing_ns(statement, ns))
end

#register_namespace(statement, ns) ⇒ void

Note:

Internally called by the AppendixHandler whenever a new namespace object is encountered.

This method returns an undefined value.

Tracks the namespace the namespace-defining statement (such as class or module) was registered in. This registry is used to map between free comment statements and the namespace they’re actually defined in.

Parameters:

  • statement (Parser::Ruby::ClassNode, Parser::Ruby::ModuleNode)

    the namespace defining statement

  • ns (CodeObjects::NamespaceObject)

    the namespace object the YARD handler processor was tracking when the statement was parsed

See Also:



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/yard-appendix/namespace_resolver.rb', line 56

def register_namespace(statement, ns)
  klass_name = path_from_ast(statement, ns)

  # already registered?
  return if @namespaces[klass_name]

  klass_path = path_from_ast(statement, ns, true)

  log.debug "yard-appendix: registering path for namespace '#{klass_name}' => #{klass_path}"

  @namespaces[klass_name] = klass_path
end