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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(constant:, bodystmt:, location:) ⇒ ModuleDeclaration

Returns a new instance of ModuleDeclaration.



7748
7749
7750
7751
7752
7753
# File 'lib/syntax_tree/node.rb', line 7748

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

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to be executed in the context of the module



7743
7744
7745
# File 'lib/syntax_tree/node.rb', line 7743

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7746
7747
7748
# File 'lib/syntax_tree/node.rb', line 7746

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the module



7740
7741
7742
# File 'lib/syntax_tree/node.rb', line 7740

def constant
  @constant
end

Instance Method Details

#===(other) ⇒ Object



7808
7809
7810
7811
# File 'lib/syntax_tree/node.rb', line 7808

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

#accept(visitor) ⇒ Object



7755
7756
7757
# File 'lib/syntax_tree/node.rb', line 7755

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

#child_nodesObject Also known as: deconstruct



7759
7760
7761
# File 'lib/syntax_tree/node.rb', line 7759

def child_nodes
  [constant, bodystmt]
end

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



7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
# File 'lib/syntax_tree/node.rb', line 7763

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

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

#deconstruct_keys(_keys) ⇒ Object



7777
7778
7779
7780
7781
7782
7783
7784
# File 'lib/syntax_tree/node.rb', line 7777

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

#format(q) ⇒ Object



7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
# File 'lib/syntax_tree/node.rb', line 7786

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