Class: SyntaxTree::RBS::NameAndTypeParams

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/rbs/utils.rb

Overview

Prints out the name of a class, interface, or module declaration. Additionally loops through each type parameter if there are any and print them out joined by commas. Checks for validation and variance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ NameAndTypeParams

Returns a new instance of NameAndTypeParams.



129
130
131
# File 'lib/syntax_tree/rbs/utils.rb', line 129

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



127
128
129
# File 'lib/syntax_tree/rbs/utils.rb', line 127

def node
  @node
end

Instance Method Details

#format(q) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/syntax_tree/rbs/utils.rb', line 133

def format(q)
  node.name.format(q)
  return if node.type_params.empty?

  q.text("[")
  q.seplist(node.type_params, -> { q.text(", ") }) do |param|
    parts = []

    if param.unchecked?
      parts << "unchecked"
    end

    if param.variance == :covariant
      parts << "out"
    elsif param.variance == :contravariant
      parts << "in"
    end

    parts << param.name
    q.text(parts.join(" "))

    if param.upper_bound
      q.text(" < ")
      param.upper_bound.format(q)
    end
  end

  q.text("]")
end

#pretty_print(q) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/syntax_tree/rbs/utils.rb', line 163

def pretty_print(q)
  q.breakable
  q.pp(node.name)

  if node.type_params.any?
    q.breakable
    q.group(2, "type_params=[", "]") do
      q.seplist(node.type_params) do |param|
        q.group(2, "(type-param", ")") do
          if param.unchecked?
            q.breakable
            q.text("unchecked")
          end

          if param.variance == :covariant
            q.breakable
            q.text("covariant")
          elsif param.variance == :contravariant
            q.breakable
            q.text("contravariant")
          end

          q.breakable
          q.text("name=")
          q.pp(param.name)

          if param.upper_bound
            q.breakable
            q.text("upper_bound=")
            q.pp(param.upper_bound)
          end
        end
      end
    end
  end
end