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

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

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

Constructor Details

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

Returns a new instance of ConstantDecl.



167
168
169
170
171
172
173
# File 'lib/rbs/ast/ruby/declarations.rb', line 167

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_nameObject (readonly)

Returns the value of attribute constant_name.



163
164
165
# File 'lib/rbs/ast/ruby/declarations.rb', line 163

def constant_name
  @constant_name
end

#leading_commentObject (readonly)

Returns the value of attribute leading_comment.



162
163
164
# File 'lib/rbs/ast/ruby/declarations.rb', line 162

def leading_comment
  @leading_comment
end

#nodeObject (readonly)

Returns the value of attribute node.



164
165
166
# File 'lib/rbs/ast/ruby/declarations.rb', line 164

def node
  @node
end

#type_annotationObject (readonly)

Returns the value of attribute type_annotation.



165
166
167
# File 'lib/rbs/ast/ruby/declarations.rb', line 165

def type_annotation
  @type_annotation
end

Instance Method Details

#commentObject



209
210
211
# File 'lib/rbs/ast/ruby/declarations.rb', line 209

def comment
  leading_comment&.as_comment
end

#locationObject



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

def location
  rbs_location(node.location)
end

#name_locationObject



179
180
181
182
183
184
185
186
# File 'lib/rbs/ast/ruby/declarations.rb', line 179

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

#typeObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/rbs/ast/ruby/declarations.rb', line 188

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



213
214
215
216
217
218
219
220
# File 'lib/rbs/ast/ruby/declarations.rb', line 213

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