Class: RBS::Definition

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

Defined Under Namespace

Modules: Ancestor Classes: InstanceAncestors, Method, SingletonAncestors, Variable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_name:, entry:, self_type:, ancestors:) ⇒ Definition

Returns a new instance of Definition.



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/rbs/definition.rb', line 287

def initialize(type_name:, entry:, self_type:, ancestors:)
  case entry
  when Environment::ClassEntry, Environment::ModuleEntry
    # ok
  else
    unless entry.decl.is_a?(AST::Declarations::Interface)
      raise "Declaration should be a class, module, or interface: #{type_name}"
    end
  end

  unless self_type.is_a?(Types::ClassSingleton) || self_type.is_a?(Types::Interface) || self_type.is_a?(Types::ClassInstance)
    raise "self_type should be the type of declaration: #{self_type}"
  end

  @type_name = type_name
  @self_type = self_type
  @entry = entry
  @methods = {}
  @instance_variables = {}
  @class_variables = {}
  @ancestors = ancestors
end

Instance Attribute Details

#ancestorsObject (readonly)

Returns the value of attribute ancestors.



281
282
283
# File 'lib/rbs/definition.rb', line 281

def ancestors
  @ancestors
end

#class_variablesObject (readonly)

Returns the value of attribute class_variables.



285
286
287
# File 'lib/rbs/definition.rb', line 285

def class_variables
  @class_variables
end

#entryObject (readonly)

Returns the value of attribute entry.



280
281
282
# File 'lib/rbs/definition.rb', line 280

def entry
  @entry
end

#instance_variablesObject (readonly)

Returns the value of attribute instance_variables.



284
285
286
# File 'lib/rbs/definition.rb', line 284

def instance_variables
  @instance_variables
end

#methodsObject (readonly)

Returns the value of attribute methods.



283
284
285
# File 'lib/rbs/definition.rb', line 283

def methods
  @methods
end

#self_typeObject (readonly)

Returns the value of attribute self_type.



282
283
284
# File 'lib/rbs/definition.rb', line 282

def self_type
  @self_type
end

#type_nameObject (readonly)

Returns the value of attribute type_name.



279
280
281
# File 'lib/rbs/definition.rb', line 279

def type_name
  @type_name
end

Instance Method Details

#class?Boolean

Returns:

  • (Boolean)


310
311
312
# File 'lib/rbs/definition.rb', line 310

def class?
  entry.is_a?(Environment::ClassEntry)
end

#class_type?Boolean

Returns:

  • (Boolean)


327
328
329
# File 'lib/rbs/definition.rb', line 327

def class_type?
  self_type.is_a?(Types::ClassSingleton)
end

#each_type(&block) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/rbs/definition.rb', line 372

def each_type(&block)
  if block
    methods.each_value do |method|
      if method.defined_in == type_name
        method.method_types.each do |method_type|
          method_type.each_type(&block)
        end
      end
    end

    instance_variables.each_value do |var|
      if var.declared_in == type_name
        yield var.type
      end
    end

    class_variables.each_value do |var|
      if var.declared_in == type_name
        yield var.type
      end
    end
  else
    enum_for :each_type
  end
end

#instance_type?Boolean

Returns:

  • (Boolean)


331
332
333
# File 'lib/rbs/definition.rb', line 331

def instance_type?
  self_type.is_a?(Types::ClassInstance)
end

#interface?Boolean

Returns:

  • (Boolean)


318
319
320
321
322
323
324
325
# File 'lib/rbs/definition.rb', line 318

def interface?
  case en = entry
  when Environment::SingleEntry
    en.decl.is_a?(AST::Declarations::Interface)
  else
    false
  end
end

#interface_type?Boolean

Returns:

  • (Boolean)


335
336
337
# File 'lib/rbs/definition.rb', line 335

def interface_type?
  self_type.is_a?(Types::Interface)
end

#map_method_type(&block) ⇒ Object



362
363
364
365
366
367
368
369
370
# File 'lib/rbs/definition.rb', line 362

def map_method_type(&block)
  definition = self.class.new(type_name: type_name, self_type: self_type, ancestors: ancestors, entry: entry)

  definition.methods.merge!(methods.transform_values {|method| method.map_method_type(&block) })
  definition.instance_variables.merge!(instance_variables)
  definition.class_variables.merge!(class_variables)

  definition
end

#module?Boolean

Returns:

  • (Boolean)


314
315
316
# File 'lib/rbs/definition.rb', line 314

def module?
  entry.is_a?(Environment::ModuleEntry)
end

#sub(s) ⇒ Object



352
353
354
355
356
357
358
359
360
# File 'lib/rbs/definition.rb', line 352

def sub(s)
  definition = self.class.new(type_name: type_name, self_type: _ = self_type.sub(s), ancestors: ancestors, entry: entry)

  definition.methods.merge!(methods.transform_values {|method| method.sub(s) })
  definition.instance_variables.merge!(instance_variables.transform_values {|v| v.sub(s) })
  definition.class_variables.merge!(class_variables.transform_values {|v| v.sub(s) })

  definition
end

#type_paramsObject



339
340
341
# File 'lib/rbs/definition.rb', line 339

def type_params
  type_params_decl.each.map(&:name)
end

#type_params_declObject



343
344
345
346
347
348
349
350
# File 'lib/rbs/definition.rb', line 343

def type_params_decl
  case en = entry
  when Environment::ClassEntry, Environment::ModuleEntry
    en.type_params
  when Environment::SingleEntry
    en.decl.type_params
  end
end