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.



7805
7806
7807
7808
7809
7810
# File 'lib/syntax_tree/node.rb', line 7805

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



7800
7801
7802
# File 'lib/syntax_tree/node.rb', line 7800

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7803
7804
7805
# File 'lib/syntax_tree/node.rb', line 7803

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the module



7797
7798
7799
# File 'lib/syntax_tree/node.rb', line 7797

def constant
  @constant
end

Instance Method Details

#===(other) ⇒ Object



7865
7866
7867
7868
# File 'lib/syntax_tree/node.rb', line 7865

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

#accept(visitor) ⇒ Object



7812
7813
7814
# File 'lib/syntax_tree/node.rb', line 7812

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

#child_nodesObject Also known as: deconstruct



7816
7817
7818
# File 'lib/syntax_tree/node.rb', line 7816

def child_nodes
  [constant, bodystmt]
end

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



7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
# File 'lib/syntax_tree/node.rb', line 7820

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



7834
7835
7836
7837
7838
7839
7840
7841
# File 'lib/syntax_tree/node.rb', line 7834

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

#format(q) ⇒ Object



7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
# File 'lib/syntax_tree/node.rb', line 7843

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