Class: RBS::Inline::AST::Declarations::ConstantDecl

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConstantUtil

#type_name, #value_node

Constructor Details

#initialize(node, comments, assertion) ⇒ ConstantDecl

Returns a new instance of ConstantDecl.



184
185
186
187
188
# File 'lib/rbs/inline/ast/declarations.rb', line 184

def initialize(node, comments, assertion) #: void
  @node = node
  @comments = comments
  @assertion = assertion
end

Instance Attribute Details

#assertionObject (readonly)

: Annotations::TypeAssertion?



179
180
181
# File 'lib/rbs/inline/ast/declarations.rb', line 179

def assertion
  @assertion
end

#commentsObject (readonly)

: AnnotationParser::ParsingResult?



178
179
180
# File 'lib/rbs/inline/ast/declarations.rb', line 178

def comments
  @comments
end

#nodeObject (readonly)

: Prism::ConstantWriteNode



177
178
179
# File 'lib/rbs/inline/ast/declarations.rb', line 177

def node
  @node
end

Instance Method Details

#constant_nameObject



234
235
236
# File 'lib/rbs/inline/ast/declarations.rb', line 234

def constant_name
  TypeName.new(name: node.name, namespace: Namespace.empty)
end

#literal_typeObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rbs/inline/ast/declarations.rb', line 211

def literal_type
  case node.value
  when Prism::StringNode, Prism::InterpolatedStringNode
    BuiltinNames::String.instance_type
  when Prism::SymbolNode, Prism::InterpolatedSymbolNode
    BuiltinNames::Symbol.instance_type
  when Prism::RegularExpressionNode, Prism::InterpolatedRegularExpressionNode
    BuiltinNames::Regexp.instance_type
  when Prism::IntegerNode
    BuiltinNames::Integer.instance_type
  when Prism::FloatNode
    BuiltinNames::Float.instance_type
  when Prism::ArrayNode
    BuiltinNames::Array.instance_type
  when Prism::HashNode
    BuiltinNames::Hash.instance_type
  when Prism::TrueNode, Prism::FalseNode
    Types::Bases::Bool.new(location: nil)
  end
end

#start_lineObject

: Integer



238
239
240
# File 'lib/rbs/inline/ast/declarations.rb', line 238

def start_line #: Integer
  node.location.start_line
end

#typeObject



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

def type
  if assertion
    case assertion.type
    when MethodType, nil
      # skip
    else
      return assertion.type
    end
  end

  if literal = literal_type
    return literal
  end

  Types::Bases::Any.new(location: nil)
end