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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of ModuleDeclaration.



5651
5652
5653
5654
5655
5656
# File 'lib/syntax_tree/node.rb', line 5651

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



5646
5647
5648
# File 'lib/syntax_tree/node.rb', line 5646

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5649
5650
5651
# File 'lib/syntax_tree/node.rb', line 5649

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the module



5643
5644
5645
# File 'lib/syntax_tree/node.rb', line 5643

def constant
  @constant
end

Instance Method Details

#accept(visitor) ⇒ Object



5658
5659
5660
# File 'lib/syntax_tree/node.rb', line 5658

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

#child_nodesObject Also known as: deconstruct



5662
5663
5664
# File 'lib/syntax_tree/node.rb', line 5662

def child_nodes
  [constant, bodystmt]
end

#deconstruct_keys(keys) ⇒ Object



5668
5669
5670
5671
5672
5673
5674
5675
# File 'lib/syntax_tree/node.rb', line 5668

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

#format(q) ⇒ Object



5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
# File 'lib/syntax_tree/node.rb', line 5677

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