Class: RBS::Inline::AST::Annotations::BlockType

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

Overview

‘@rbs &block: METHOD-TYPE` or `@rbs &block: ? METHOD-TYPE`

Instance Attribute Summary collapse

Attributes inherited from Base

#source, #tree

Instance Method Summary collapse

Constructor Details

#initialize(tree, source) ⇒ BlockType

Returns a new instance of BlockType.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/rbs/inline/ast/annotations.rb', line 213

def initialize(tree, source)
  @tree = tree
  @source = source

  annotation = tree.nth_tree!(1)

  if name = annotation.nth_token?(1)
    @name = name[1].to_sym
  end

  optional = annotation.nth_token?(3)

  if type_token = annotation.nth_token?(4)
    block_src = type_token[1]

    proc_type = ::RBS::Parser.parse_type("^" + block_src, require_eof: true) rescue RBS::ParsingError
    if proc_type.is_a?(Types::Proc)
      @type = Types::Block.new(
        type: proc_type.type,
        required: !optional,
        self_type: proc_type.self_type
      )
    end

    @type_source = block_src
  else
    @type = nil
    @type_source = ""
  end

  if comment = annotation.nth_tree(5)
    @comment = comment.to_s
  end
end

Instance Attribute Details

#commentObject (readonly)

: String?



208
209
210
# File 'lib/rbs/inline/ast/annotations.rb', line 208

def comment
  @comment
end

#nameObject (readonly)

: Symbol?



204
205
206
# File 'lib/rbs/inline/ast/annotations.rb', line 204

def name
  @name
end

#typeObject (readonly)

: Types::Block?



206
207
208
# File 'lib/rbs/inline/ast/annotations.rb', line 206

def type
  @type
end

#type_sourceObject (readonly)

: String



210
211
212
# File 'lib/rbs/inline/ast/annotations.rb', line 210

def type_source
  @type_source
end