Class: RBS::Inline::AST::Declarations::StructAssignDecl

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConstantUtil

type_name, value_node

Methods included from DataStructUtil

#class_annotations, #each_attribute

Constructor Details

#initialize(node, struct_new_node, comments, type_decls) ⇒ StructAssignDecl

Returns a new instance of StructAssignDecl.



392
393
394
395
396
397
# File 'lib/rbs/inline/ast/declarations.rb', line 392

def initialize(node, struct_new_node, comments, type_decls)
  @node = node
  @comments = comments
  @type_decls = type_decls
  @struct_new_node = struct_new_node
end

Instance Attribute Details

#commentsObject (readonly)

: AnnotationParser::ParsingResult?



385
386
387
# File 'lib/rbs/inline/ast/declarations.rb', line 385

def comments
  @comments
end

#nodeObject (readonly)

: Prism::ConstantWriteNode



383
384
385
# File 'lib/rbs/inline/ast/declarations.rb', line 383

def node
  @node
end

#struct_new_nodeObject (readonly)

: Prism::CallNode



389
390
391
# File 'lib/rbs/inline/ast/declarations.rb', line 389

def struct_new_node
  @struct_new_node
end

#type_declsObject (readonly)

: Hash[Integer, Annotations::TypeAssertion]



387
388
389
# File 'lib/rbs/inline/ast/declarations.rb', line 387

def type_decls
  @type_decls
end

Class Method Details

.struct_new?(node) ⇒ Boolean

Returns:

  • (Boolean)


422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/rbs/inline/ast/declarations.rb', line 422

def self.struct_new?(node)
  value = value_node(node)

  if value.is_a?(Prism::CallNode)
    if value.receiver.is_a?(Prism::ConstantReadNode)
      if value.receiver.full_name.delete_prefix("::") == "Struct"
        if value.name == :new
          return value
        end
      end
    end
  end
end

Instance Method Details

#constant_nameObject



405
406
407
# File 'lib/rbs/inline/ast/declarations.rb', line 405

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

#each_attribute_argument(&block) ⇒ Object



410
411
412
413
414
415
416
417
418
419
# File 'lib/rbs/inline/ast/declarations.rb', line 410

def each_attribute_argument(&block)
  if args = struct_new_node.arguments
    args.arguments.each do |arg|
      next if arg.is_a?(Prism::KeywordHashNode)
      next if arg.is_a?(Prism::StringNode)

      yield arg
    end
  end
end

#keyword_init?Boolean

Returns:

  • (Boolean)


437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/rbs/inline/ast/declarations.rb', line 437

def keyword_init? #: bool
  if args = struct_new_node.arguments
    args.arguments.each do |arg|
      if arg.is_a?(Prism::KeywordHashNode)
        arg.elements.each do |assoc|
          if assoc.is_a?(Prism::AssocNode)
            if (key = assoc.key).is_a?(Prism::SymbolNode)
              if key.value == "keyword_init"
                value = assoc.value
                if value.is_a?(Prism::FalseNode)
                  return false
                end
              end
            end
          end
        end
      end
    end
  end

  true
end

#positional_init?Boolean

Returns:

  • (Boolean)


461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/rbs/inline/ast/declarations.rb', line 461

def positional_init? #: bool
  if args = struct_new_node.arguments
    args.arguments.each do |arg|
      if arg.is_a?(Prism::KeywordHashNode)
        arg.elements.each do |assoc|
          if assoc.is_a?(Prism::AssocNode)
            if (key = assoc.key).is_a?(Prism::SymbolNode)
              if key.value == "keyword_init"
                value = assoc.value
                if value.is_a?(Prism::TrueNode)
                  return false
                end
              end
            end
          end
        end
      end
    end
  end

  true
end

#readonly_attributes?Boolean

Returns true is annotation is given to make all attributes readonly

Add ‘# @rbs %arbs-inline:readonly-attributes=true` to the class to make all attributes attr_reader, instead of attr_accessor.

Returns:

  • (Boolean)


489
490
491
492
493
# File 'lib/rbs/inline/ast/declarations.rb', line 489

def readonly_attributes? #: bool
  class_annotations.any? do |annotation|
    annotation.string == "rbs-inline:readonly-attributes=true"
  end
end

#required_new_args?Boolean

Returns true if annotation is given to make all .new arguments required

Add ‘# @rbs %arbs-inline:new-args=required` to the class to make all of the parameters required.

Returns:

  • (Boolean)


500
501
502
503
504
# File 'lib/rbs/inline/ast/declarations.rb', line 500

def required_new_args? #: bool
  class_annotations.any? do |annotation|
    annotation.string == "rbs-inline:new-args=required"
  end
end

#start_lineObject

: Integer



399
400
401
# File 'lib/rbs/inline/ast/declarations.rb', line 399

def start_line #: Integer
  node.location.start_line
end