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.



407
408
409
410
411
412
# File 'lib/rbs/inline/ast/declarations.rb', line 407

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?



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

def comments
  @comments
end

#nodeObject (readonly)

: Prism::ConstantWriteNode



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

def node
  @node
end

#struct_new_nodeObject (readonly)

: Prism::CallNode



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

def struct_new_node
  @struct_new_node
end

#type_declsObject (readonly)

: Hash[Integer, Annotations::TypeAssertion]



402
403
404
# File 'lib/rbs/inline/ast/declarations.rb', line 402

def type_decls
  @type_decls
end

Class Method Details

.struct_new?(node) ⇒ Boolean

Returns:

  • (Boolean)


437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/rbs/inline/ast/declarations.rb', line 437

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



420
421
422
# File 'lib/rbs/inline/ast/declarations.rb', line 420

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

#each_attribute_argument(&block) ⇒ Object



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

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)


452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/rbs/inline/ast/declarations.rb', line 452

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)


476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/rbs/inline/ast/declarations.rb', line 476

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)


504
505
506
507
508
# File 'lib/rbs/inline/ast/declarations.rb', line 504

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)


515
516
517
518
519
# File 'lib/rbs/inline/ast/declarations.rb', line 515

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

#start_lineObject

: Integer



414
415
416
# File 'lib/rbs/inline/ast/declarations.rb', line 414

def start_line #: Integer
  node.location.start_line
end