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.



6634
6635
6636
6637
6638
6639
# File 'lib/syntax_tree/node.rb', line 6634

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



6629
6630
6631
# File 'lib/syntax_tree/node.rb', line 6629

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6632
6633
6634
# File 'lib/syntax_tree/node.rb', line 6632

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the module



6626
6627
6628
# File 'lib/syntax_tree/node.rb', line 6626

def constant
  @constant
end

Instance Method Details

#accept(visitor) ⇒ Object



6641
6642
6643
# File 'lib/syntax_tree/node.rb', line 6641

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

#child_nodesObject Also known as: deconstruct



6645
6646
6647
# File 'lib/syntax_tree/node.rb', line 6645

def child_nodes
  [constant, bodystmt]
end

#deconstruct_keys(_keys) ⇒ Object



6651
6652
6653
6654
6655
6656
6657
6658
# File 'lib/syntax_tree/node.rb', line 6651

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

#format(q) ⇒ Object



6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
# File 'lib/syntax_tree/node.rb', line 6660

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