Class: Decode::Comment::RBS

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

Overview

Represents an RBS type annotation following rbs-inline syntax.

Examples:

  • ‘@rbs generic T` - Declares a generic type parameter for a class

  • ‘@rbs [T] () { () -> T } -> Task` - Complete method type signature

Constant Summary

Constants inherited from Tag

Tag::PATTERN

Instance Attribute Summary collapse

Attributes inherited from Tag

#The directive that generated the tag., #directive

Attributes inherited from Node

#The children of this node., #children

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tag

bracketed_content, match

Methods inherited from Node

#add, #children?, #each, #filter, #traverse

Constructor Details

#initialize(directive, text) ⇒ RBS

Initialize a new RBS pragma.



38
39
40
41
# File 'lib/decode/comment/rbs.rb', line 38

def initialize(directive, text)
	super(directive)
	@text = text&.strip || ""
end

Instance Attribute Details

#textObject (readonly)

The RBS type annotation text.



45
46
47
# File 'lib/decode/comment/rbs.rb', line 45

def text
  @text
end

#The raw RBS text.(rawRBStext.) ⇒ Object (readonly)

The RBS type annotation text.



45
# File 'lib/decode/comment/rbs.rb', line 45

attr :text

Class Method Details

.build(directive, text) ⇒ Object

Build an RBS pragma from a directive and text.



30
31
32
33
# File 'lib/decode/comment/rbs.rb', line 30

def self.build(directive, text)
	node = self.new(directive, text)
	return node
end

.parse(directive, text, lines, tags, level = 0) ⇒ Object

Parse an RBS pragma from text.



23
24
25
# File 'lib/decode/comment/rbs.rb', line 23

def self.parse(directive, text, lines, tags, level = 0)
	self.build(directive, text)
end

Instance Method Details

#generic?Boolean

Check if this is a generic type declaration.

Returns:

  • (Boolean)


49
50
51
# File 'lib/decode/comment/rbs.rb', line 49

def generic?
	@text.start_with?("generic ")
end

#generic_parameterObject

Extract the generic type parameter name.



55
56
57
58
59
60
61
# File 'lib/decode/comment/rbs.rb', line 55

def generic_parameter
	if generic?
		# Extract the parameter name from "generic T" or "generic T, U"
		match = @text.match(/^generic\s+([A-Z][A-Za-z0-9_]*(?:\s*,\s*[A-Z][A-Za-z0-9_]*)*)/)
		return match[1] if match
	end
end

#method_signatureObject

Get the method type signature text.



71
72
73
# File 'lib/decode/comment/rbs.rb', line 71

def method_signature
	method_signature? ? @text : nil
end

#method_signature?Boolean

Check if this is a method type signature.

Returns:

  • (Boolean)


65
66
67
# File 'lib/decode/comment/rbs.rb', line 65

def method_signature?
	@text && !generic?
end