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.



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

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



7774
7775
7776
# File 'lib/syntax_tree/node.rb', line 7774

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7777
7778
7779
# File 'lib/syntax_tree/node.rb', line 7777

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the module



7771
7772
7773
# File 'lib/syntax_tree/node.rb', line 7771

def constant
  @constant
end

Instance Method Details

#===(other) ⇒ Object



7839
7840
7841
7842
# File 'lib/syntax_tree/node.rb', line 7839

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

#accept(visitor) ⇒ Object



7786
7787
7788
# File 'lib/syntax_tree/node.rb', line 7786

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

#child_nodesObject Also known as: deconstruct



7790
7791
7792
# File 'lib/syntax_tree/node.rb', line 7790

def child_nodes
  [constant, bodystmt]
end

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



7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
# File 'lib/syntax_tree/node.rb', line 7794

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



7808
7809
7810
7811
7812
7813
7814
7815
# File 'lib/syntax_tree/node.rb', line 7808

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

#format(q) ⇒ Object



7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
# File 'lib/syntax_tree/node.rb', line 7817

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