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



3522
3523
3524
3525
3526
3527
3528
# File 'lib/syntax_tree.rb', line 3522

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



3514
3515
3516
# File 'lib/syntax_tree.rb', line 3514

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3520
3521
3522
# File 'lib/syntax_tree.rb', line 3520

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the class being

defined



3508
3509
3510
# File 'lib/syntax_tree.rb', line 3508

def constant
  @constant
end

#locationObject (readonly)

Location

the location of this node



3517
3518
3519
# File 'lib/syntax_tree.rb', line 3517

def location
  @location
end

#superclassObject (readonly)

nil | untyped

the optional superclass declaration



3511
3512
3513
# File 'lib/syntax_tree.rb', line 3511

def superclass
  @superclass
end

Instance Method Details

#child_nodesObject



3530
3531
3532
# File 'lib/syntax_tree.rb', line 3530

def child_nodes
  [constant, superclass, bodystmt]
end

#format(q) ⇒ Object



3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
# File 'lib/syntax_tree.rb', line 3534

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



3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
# File 'lib/syntax_tree.rb', line 3568

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



3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
# File 'lib/syntax_tree.rb', line 3587

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