Class: SyntaxTree::ClassDeclaration

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

Overview

Class represents defining a class using the class keyword.

class Container
end

Classes can have path names as their class name in case it’s being nested under a namespace, as in:

class Namespace::Container
end

Classes can also be defined as a top-level path, in the case that it’s already in a namespace but you want to define it at the top-level instead, as in:

module OtherNamespace
  class ::Namespace::Container
  end
end

All of these declarations can also have an optional superclass reference, as in:

class Child < Parent
end

That superclass can actually be any Ruby expression, it doesn’t necessarily need to be a constant, as in:

class Child < method
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constant:, superclass:, bodystmt:, location:, comments: []) ⇒ ClassDeclaration

Returns a new instance of ClassDeclaration.



3664
3665
3666
3667
3668
3669
3670
# File 'lib/syntax_tree.rb', line 3664

def initialize(constant:, superclass:, bodystmt:, location:, comments: [])
  @constant = constant
  @superclass = superclass
  @bodystmt = bodystmt
  @location = location
  @comments = comments
end

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to execute within the context of the class



3656
3657
3658
# File 'lib/syntax_tree.rb', line 3656

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3662
3663
3664
# File 'lib/syntax_tree.rb', line 3662

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the class being

defined



3650
3651
3652
# File 'lib/syntax_tree.rb', line 3650

def constant
  @constant
end

#locationObject (readonly)

Location

the location of this node



3659
3660
3661
# File 'lib/syntax_tree.rb', line 3659

def location
  @location
end

#superclassObject (readonly)

nil | untyped

the optional superclass declaration



3653
3654
3655
# File 'lib/syntax_tree.rb', line 3653

def superclass
  @superclass
end

Instance Method Details

#child_nodesObject



3672
3673
3674
# File 'lib/syntax_tree.rb', line 3672

def child_nodes
  [constant, superclass, bodystmt]
end

#format(q) ⇒ Object



3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
# File 'lib/syntax_tree.rb', line 3676

def format(q)
  declaration = -> do
    q.group do
      q.text("class ")
      q.format(constant)

      if superclass
        q.text(" < ")
        q.format(superclass)
      end
    end
  end

  if bodystmt.empty?
    q.group do
      declaration.call
      q.breakable(force: true)
      q.text("end")
    end
  else
    q.group do
      declaration.call

      q.indent do
        q.breakable(force: true)
        q.format(bodystmt)
      end

      q.breakable(force: true)
      q.text("end")
    end
  end
end

#pretty_print(q) ⇒ Object



3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
# File 'lib/syntax_tree.rb', line 3710

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("class")

    q.breakable
    q.pp(constant)

    if superclass
      q.breakable
      q.pp(superclass)
    end

    q.breakable
    q.pp(bodystmt)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
# File 'lib/syntax_tree.rb', line 3729

def to_json(*opts)
  {
    type: :class,
    constant: constant,
    superclass: superclass,
    bodystmt: bodystmt,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end