Class: SyntaxTree::ClassDeclaration

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.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

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

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

Returns a new instance of ClassDeclaration.



3326
3327
3328
3329
3330
3331
3332
# File 'lib/syntax_tree/node.rb', line 3326

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

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to execute within the context of the class



3321
3322
3323
# File 'lib/syntax_tree/node.rb', line 3321

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3324
3325
3326
# File 'lib/syntax_tree/node.rb', line 3324

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the class being

defined



3315
3316
3317
# File 'lib/syntax_tree/node.rb', line 3315

def constant
  @constant
end

#superclassObject (readonly)

nil | Node

the optional superclass declaration



3318
3319
3320
# File 'lib/syntax_tree/node.rb', line 3318

def superclass
  @superclass
end

Instance Method Details

#===(other) ⇒ Object



3389
3390
3391
3392
# File 'lib/syntax_tree/node.rb', line 3389

def ===(other)
  other.is_a?(ClassDeclaration) && constant === other.constant &&
    superclass === other.superclass && bodystmt === other.bodystmt
end

#accept(visitor) ⇒ Object



3334
3335
3336
# File 'lib/syntax_tree/node.rb', line 3334

def accept(visitor)
  visitor.visit_class(self)
end

#child_nodesObject Also known as: deconstruct



3338
3339
3340
# File 'lib/syntax_tree/node.rb', line 3338

def child_nodes
  [constant, superclass, bodystmt]
end

#copy(constant: nil, superclass: nil, bodystmt: nil, location: nil) ⇒ Object



3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
# File 'lib/syntax_tree/node.rb', line 3342

def copy(constant: nil, superclass: nil, bodystmt: nil, location: nil)
  node =
    ClassDeclaration.new(
      constant: constant || self.constant,
      superclass: superclass || self.superclass,
      bodystmt: bodystmt || self.bodystmt,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



3357
3358
3359
3360
3361
3362
3363
3364
3365
# File 'lib/syntax_tree/node.rb', line 3357

def deconstruct_keys(_keys)
  {
    constant: constant,
    superclass: superclass,
    bodystmt: bodystmt,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
# File 'lib/syntax_tree/node.rb', line 3367

def format(q)
  if bodystmt.empty?
    q.group do
      format_declaration(q)
      q.breakable_force
      q.text("end")
    end
  else
    q.group do
      format_declaration(q)

      q.indent do
        q.breakable_force
        q.format(bodystmt)
      end

      q.breakable_force
      q.text("end")
    end
  end
end