Class: RBS::AST::Ruby::Declarations::ConstantDecl

Inherits:
Base
  • Object
show all
Defined in:
lib/rbs/ast/ruby/declarations.rb,
sig/ast/ruby/declarations.rbs

Instance Attribute Summary collapse

Attributes inherited from Base

#buffer

Instance Method Summary collapse

Methods included from Helpers::LocationHelper

#rbs_location

Methods included from Helpers::ConstantHelper

constant_as_type_name, #self?.constant_as_type_name

Constructor Details

#initialize(buffer, constant_name, node, leading_comment, type_annotation) ⇒ ConstantDecl

Returns a new instance of ConstantDecl.



177
178
179
180
181
182
183
# File 'lib/rbs/ast/ruby/declarations.rb', line 177

def initialize(buffer, constant_name, node, leading_comment, type_annotation)
  super(buffer)
  @constant_name = constant_name
  @node = node
  @leading_comment = leading_comment
  @type_annotation = type_annotation
end

Instance Attribute Details

#constant_nameTypeName (readonly)

Returns the value of attribute constant_name.

Returns:



173
174
175
# File 'lib/rbs/ast/ruby/declarations.rb', line 173

def constant_name
  @constant_name
end

#leading_commentCommentBlock? (readonly)

Returns the value of attribute leading_comment.

Returns:



172
173
174
# File 'lib/rbs/ast/ruby/declarations.rb', line 172

def leading_comment
  @leading_comment
end

#nodenode (readonly)

Returns the value of attribute node.

Returns:



174
175
176
# File 'lib/rbs/ast/ruby/declarations.rb', line 174

def node
  @node
end

#type_annotationAnnotations::NodeTypeAssertion? (readonly)

Returns the value of attribute type_annotation.



175
176
177
# File 'lib/rbs/ast/ruby/declarations.rb', line 175

def type_annotation
  @type_annotation
end

Instance Method Details

#commentAST::Comment?

Returns the comment content extracted from the leading comment block

Returns:



117
118
119
# File 'sig/ast/ruby/declarations.rbs', line 117

def comment
  leading_comment&.as_comment
end

#locationLocation

Returns:



185
186
187
# File 'lib/rbs/ast/ruby/declarations.rb', line 185

def location
  rbs_location(node.location)
end

#name_locationLocation

Returns:



189
190
191
192
193
194
195
196
# File 'lib/rbs/ast/ruby/declarations.rb', line 189

def name_location
  case node
  when Prism::ConstantWriteNode
    rbs_location(node.name_loc)
  when Prism::ConstantPathWriteNode
    rbs_location(node.target.location)
  end
end

#typeTypes::t

Returns the type of the constant

  • When type_anntoation is given, it returns the type from the annotation.
  • When type_annotation is not given, it returns infered type from the right-hand-side of the constant assignment
  • Or it returns untyped type

Returns:

  • (Types::t)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'sig/ast/ruby/declarations.rbs', line 113

def type
  return type_annotation.type if type_annotation

  case node.value
  when Prism::IntegerNode
    BuiltinNames::Integer.instance_type
  when Prism::FloatNode
    BuiltinNames::Float.instance_type
  when Prism::StringNode
    BuiltinNames::String.instance_type
  when Prism::TrueNode, Prism::FalseNode
    Types::Bases::Bool.new(location: nil)
  when Prism::SymbolNode
    BuiltinNames::Symbol.instance_type
  when Prism::NilNode
    Types::Bases::Nil.new(location: nil)
  else
    Types::Bases::Any.new(location: nil)
  end
end

#type_fingerprintObject

Returns:

  • (Object)


223
224
225
226
227
228
229
230
# File 'lib/rbs/ast/ruby/declarations.rb', line 223

def type_fingerprint
  [
    "decls/constant",
    constant_name.to_s,
    type.to_s,
    leading_comment&.as_comment&.string
  ]
end