Class: Decode::RBS::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/decode/rbs/generator.rb

Overview

Represents a generator for RBS type declarations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(include_private: false) ⇒ Generator

Initialize a new RBS generator. Sets up the RBS environment for type resolution.



18
19
20
21
22
23
# File 'lib/decode/rbs/generator.rb', line 18

def initialize(include_private: false)
	# Set up RBS environment for type resolution
	@loader = ::RBS::EnvironmentLoader.new()
	@environment = ::RBS::Environment.from_loader(@loader).resolve_type_names
	@include_private = include_private
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



29
30
31
# File 'lib/decode/rbs/generator.rb', line 29

def environment
  @environment
end

#include_privateObject (readonly)

Returns the value of attribute include_private.



32
33
34
# File 'lib/decode/rbs/generator.rb', line 32

def include_private
  @include_private
end

#loaderObject (readonly)

Returns the value of attribute loader.



26
27
28
# File 'lib/decode/rbs/generator.rb', line 26

def loader
  @loader
end

#The RBS environment loader.(RBSenvironmentloader.) ⇒ Object (readonly)



26
# File 'lib/decode/rbs/generator.rb', line 26

attr :loader

#The resolved RBS environment.(resolvedRBSenvironment.) ⇒ Object (readonly)



29
# File 'lib/decode/rbs/generator.rb', line 29

attr :environment

Instance Method Details

#generate(index, output: $stdout) ⇒ Object

Generate RBS declarations for the given index.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/decode/rbs/generator.rb', line 37

def generate(index, output: $stdout)
	# Build nested RBS AST structure using a hash for proper ||= behavior
	declarations = {} #: Hash[Array[Symbol], untyped]
	roots = {} #: Hash[Array[Symbol], untyped]
	
	# Efficiently traverse the trie to find containers and their methods
	index.trie.traverse do |lexical_path, node, descend|
		# Process container definitions at this node
		if node.values
			containers = node.values.select{|definition| definition.container? && definition.public?}
			containers.each do |definition|
				case definition
				when Decode::Language::Ruby::Class, Decode::Language::Ruby::Module
					if declaration = build_nested_declaration(definition, declarations, index)
						roots[definition.qualified_name] ||= declaration
					end
				end
			end
		end
		
		# Continue traversing children
		descend.call
	end
	
	# Write the RBS output
	writer = ::RBS::Writer.new(out: output)
	
	unless roots.empty?
		writer.write(roots.values)
	end
end

#Whether to include private methods.=(toincludeprivatemethods. = (value)) ⇒ Object



32
# File 'lib/decode/rbs/generator.rb', line 32

attr :include_private