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.



482
483
484
485
486
487
# File 'lib/rbs/inline/ast/declarations.rb', line 482

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?



475
476
477
# File 'lib/rbs/inline/ast/declarations.rb', line 475

def comments
  @comments
end

#nodeObject (readonly)

: Prism::ConstantWriteNode



473
474
475
# File 'lib/rbs/inline/ast/declarations.rb', line 473

def node
  @node
end

#struct_new_nodeObject (readonly)

: Prism::CallNode



479
480
481
# File 'lib/rbs/inline/ast/declarations.rb', line 479

def struct_new_node
  @struct_new_node
end

#type_declsObject (readonly)

: Hash[Integer, Annotations::TypeAssertion]



477
478
479
# File 'lib/rbs/inline/ast/declarations.rb', line 477

def type_decls
  @type_decls
end

Class Method Details

.struct_new?(node) ⇒ Boolean

Returns:

  • (Boolean)


512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/rbs/inline/ast/declarations.rb', line 512

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



495
496
497
# File 'lib/rbs/inline/ast/declarations.rb', line 495

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

#each_attribute_argument(&block) ⇒ Object



500
501
502
503
504
505
506
507
508
509
# File 'lib/rbs/inline/ast/declarations.rb', line 500

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)


527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/rbs/inline/ast/declarations.rb', line 527

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)


551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/rbs/inline/ast/declarations.rb', line 551

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)


579
580
581
582
583
# File 'lib/rbs/inline/ast/declarations.rb', line 579

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)


590
591
592
593
594
# File 'lib/rbs/inline/ast/declarations.rb', line 590

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

#start_lineObject

: Integer



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

def start_line #: Integer
  node.location.start_line
end