Module: RBS::Inline::AST::Annotations::Utils

Included in:
ClassDecl, Generic, ModuleDecl
Defined in:
lib/rbs/inline/ast/annotations.rb,
sig/generated/rbs/inline/ast/annotations.rbs

Overview

@rbs! type t = VarType | ReturnType | Use | Inherits | Generic | ModuleSelf | Skip | MethodTypeAssertion | TypeAssertion | SyntaxErrorAssertion | Dot3Assertion | Application | RBSAnnotation | Override | IvarType | Embedded | Method | SplatParamType | DoubleSplatParamType | BlockType | ModuleDecl | ClassDecl # | Def # | AttrReader | AttrWriter | AttrAccessor # | Include | Extend | Prepend # | Alias

Instance Method Summary collapse

Instance Method Details

#translate_super_class(type) ⇒ RBS::AST::Declarations::Class::Super?

Parameters:

  • (Types::t)

Returns:

  • (RBS::AST::Declarations::Class::Super, nil)


69
70
71
72
73
74
75
76
77
78
# File 'lib/rbs/inline/ast/annotations.rb', line 69

def translate_super_class(type)
  case type
  when Types::ClassInstance
    RBS::AST::Declarations::Class::Super.new(
      name: type.name,
      args: type.args,
      location: nil
    )
  end
end

#translate_type_name(tree) ⇒ TypeName?

Assumes the tree is generated through #parse_module_name

Returns a type name, or nil if the tree is something invalid.

Parameters:

  • tree

    -- A tree object that is generated through #parse_module_name

  • (AST::Tree)

Returns:

  • (TypeName, nil)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rbs/inline/ast/annotations.rb', line 87

def translate_type_name(tree)
  tokens = tree.non_trivia_trees

  absolute = tokens.shift
  path = [] #: Array[Symbol]

  tokens.each_slice(2) do |name, colon2|
    if colon2
      name or return nil
      name.is_a?(Array) or return nil

      path << name[1].to_sym
    end
  end

  last_token = tokens.pop
  last_token.is_a?(Array) or return nil
  name = last_token[1].to_sym

  namespace = Namespace.new(path: path, absolute: absolute ? true : false)
  TypeName.new(namespace: namespace, name: name)
end

#translate_type_param(tree) ⇒ RBS::AST::TypeParam?

Parameters:

Returns:

  • (RBS::AST::TypeParam, nil)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rbs/inline/ast/annotations.rb', line 34

def translate_type_param(tree)
  unchecked = tree.nth_token?(0) != nil
  inout =
    case tree.nth_token?(1)&.[](0)
    when nil
      :invariant
    when :kIN
      :contravariant
    when :kOUT
      :covariant
    end #: RBS::AST::TypeParam::variance

  name = tree.nth_token?(2)&.last

  if bound = tree.nth_tree?(3)
    if type = bound.nth_type?(1)
      case type
      when Types::ClassSingleton, Types::ClassInstance, Types::Interface
        upper_bound = type
      end
    end
  end

  if name
    RBS::AST::TypeParam.new(
      name: name.to_sym,
      variance: inout,
      upper_bound: upper_bound,
      lower_bound: nil,
      location: nil
    ).unchecked!(unchecked)
  end
end