Class: SyntaxTree::ModuleDeclaration

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

Overview

ModuleDeclaration represents defining a module using the module keyword.

module Namespace
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of ModuleDeclaration.



6448
6449
6450
6451
6452
6453
# File 'lib/syntax_tree/node.rb', line 6448

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

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to be executed in the context of the module



6443
6444
6445
# File 'lib/syntax_tree/node.rb', line 6443

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6446
6447
6448
# File 'lib/syntax_tree/node.rb', line 6446

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the module



6440
6441
6442
# File 'lib/syntax_tree/node.rb', line 6440

def constant
  @constant
end

Instance Method Details

#accept(visitor) ⇒ Object



6455
6456
6457
# File 'lib/syntax_tree/node.rb', line 6455

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

#child_nodesObject Also known as: deconstruct



6459
6460
6461
# File 'lib/syntax_tree/node.rb', line 6459

def child_nodes
  [constant, bodystmt]
end

#deconstruct_keys(_keys) ⇒ Object



6465
6466
6467
6468
6469
6470
6471
6472
# File 'lib/syntax_tree/node.rb', line 6465

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

#format(q) ⇒ Object



6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
# File 'lib/syntax_tree/node.rb', line 6474

def format(q)
  declaration = -> do
    q.group do
      q.text("module ")
      q.format(constant)
    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