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.



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

Returns the value of attribute constant_name.



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

def constant_name
  @constant_name
end

#leading_commentObject (readonly)

Returns the value of attribute leading_comment.



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

def leading_comment
  @leading_comment
end

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

#type_annotationObject (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

#commentObject



219
220
221
# File 'lib/rbs/ast/ruby/declarations.rb', line 219

def comment
  leading_comment&.as_comment
end

#locationObject



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

def location
  rbs_location(node.location)
end

#name_locationObject



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

#typeObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rbs/ast/ruby/declarations.rb', line 198

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



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